-- This file is provided under the Open Game License version 1.0a
-- For more information on OGL and related issues, see 
--   http://www.wizards.com/d20
--
-- For information on the Fantasy Grounds d20 Ruleset licensing and
-- the OGL license text, see the d20 ruleset license in the program
-- options.
--
-- All producers of work derived from this definition are adviced to
-- familiarize themselves with the above licenses, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- Copyright 2007 SmiteWorks Ltd.

-- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
--	Hollow Earth Expedition - COMBAT TRACKER
--	Version 1.0.0
--	05/05/2009
--	Grimmbart de Ferdock	
--/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->


function onInit()
	Interface.onHotkeyActivated = onHotkey;
	
	getWindows()[1].close();
	
	restoreFromRegistry();
	
	if #getWindows() == 0 then
		createWindow();
	end
end

function onClose()
	storeToRegistry();
end

function restoreFromRegistry()
	local trackertable = CampaignRegistry.combattracker;
	
	if trackertable then
		local idmap = {};		-- Mapping id -> window
	
		-- First pass, create entries
		for k, v in ipairs(trackertable) do
			local win = createWindow();
			win.restore(v);
			idmap[k] = win;
		end
	end
end

function storeToRegistry()
	local trackertable = {};	-- Table to receive entries to store
	local idmap = {};			-- Mapping window -> id

	-- First pass, store entries
	for k,v in ipairs(getWindows()) do
		table.insert(trackertable, v.store());
		idmap[v] = #trackertable;
	end
	CampaignRegistry.combattracker = trackertable;
end

function onHotkey(draginfo)
	if draginfo.isType("combattrackernextactor") then
		nextActor();
		return true;
	end
	if draginfo.isType("combattrackernextround") then
		nextRound();
		return true;
	end
end

function addPc(source, token)
	local newentry = createWindow();
	
	newentry.setType("pc");
	
	-- Shortcut
	newentry.name.setReadOnly(true);
	newentry.name.setFrame(nil);
	newentry.link.setValue("charsheet", source.getNodeName());
	newentry.link.setVisible(true);
	
	-- Token
	if token then
		newentry.token.setPrototype(token);
	end
	
	-- FoF
	newentry.friendfoe.setState("friend");
	
	-- Champs liés
	newentry.name.setLink(source.getChild("name"));
	newentry.sante.setLink(source.getChild("points.sante"));	
	newentry.blessure_let.setLink(source.getChild("points.blessure_let"));	
	newentry.blessure_nonlet.setLink(source.getChild("points.blessure_nonlet"));	
	
	
	
	newentry.defense.setLink(source.getChild("attribut.defense"));
	newentry.dexterite.setLink(source.getChild("attribut.dexterite"));
	newentry.corps.setLink(source.getChild("attribut.corps"));	
	newentry.taille.setLink(source.getChild("attribut.taille"));
	newentry.deplacement.setLink(source.getChild("attribut.deplacement"));	
	newentry.initiative.setLink(source.getChild("attribut.initiative"));	
	
	--arme1
	local arme1_nom = source.getChild("arme1.nom").getValue();
	local arme1_niveau = source.getChild("arme1.niveau").getValue();
	local arme1_portee = source.getChild("arme1.portee").getValue();
	local arme1_cadence = source.getChild("arme1.cadence").getValue();	
	newentry.arme1.setValue(arme1_nom .. " -  Niv: " .. arme1_niveau .. " -  Cad: " .. arme1_cadence .. " - Port: " .. arme1_portee);
	--arme2
	local arme2_nom = source.getChild("arme2.nom").getValue();
	local arme2_niveau = source.getChild("arme2.niveau").getValue();
	local arme2_portee = source.getChild("arme2.portee").getValue();
	local arme2_cadence = source.getChild("arme2.cadence").getValue();	
	newentry.arme2.setValue(arme2_nom .. " -  Niv: " .. arme2_niveau .. " -  Cad: " .. arme2_cadence .. " - Port: " .. arme2_portee);	
	--arme3
	local arme3_nom = source.getChild("arme3.nom").getValue();
	local arme3_niveau = source.getChild("arme3.niveau").getValue();
	local arme3_portee = source.getChild("arme3.portee").getValue();
	local arme3_cadence = source.getChild("arme3.cadence").getValue();	
	newentry.arme3.setValue(arme3_nom .. " -  Niv: " .. arme3_niveau .. " -  Cad: " .. arme3_cadence .. " - Port: " .. arme3_portee);	
	--arme4
	local arme4_nom = source.getChild("arme4.nom").getValue();
	local arme4_niveau = source.getChild("arme4.niveau").getValue();
	local arme4_portee = source.getChild("arme4.portee").getValue();
	local arme4_cadence = source.getChild("arme4.cadence").getValue();	
	newentry.arme4.setValue(arme4_nom .. " -  Niv: " .. arme4_niveau .. " -  Cad: " .. arme4_cadence .. " - Port: " .. arme4_portee);
	
	
	return newentry;
end

function addNpc(source)
	local newentry = createWindow();

	newentry.setType("npc");

	-- Name
	if source.getChild("name") then
		newentry.name.setValue(source.getChild("name").getValue());
	end
	
	-- Space/reach
	if source.getChild("spacereach") then
		local spacereachstr = source.getChild("spacereach").getValue();
		local space, reach = string.match(spacereachstr, "(%d+)%D*/?(%d+)%D*");
		if space then
			newentry.space.setValue(space);
			newentry.reach.setValue(reach);
		end
	end
	
	-- Token
	if source.getChild("token") then
		newentry.token.setPrototype(source.getChild("token").getValue());
	end
	
	-- FoF  Grimmbart 11/05/2009
	newentry.friendfoe.setState("foe");
	
	-- Santé
	newentry.sante.setValue(source.getChild("sante").getValue());
	--newentry.blessure_let.setValue(source.getChild("blessure_let").getValue());
	--newentry.blessure_nonlet.setValue(source.getChild("blessure_nonlet").getValue());
	
	-- Valeur de défense
	if source.getChild("defense") then newentry.defense.setValue(source.getChild("defense").getValue()) end;
	if source.getChild("corps") then newentry.corps.setValue(source.getChild("corps").getValue()) end;
	if source.getChild("dexterite") then newentry.dexterite.setValue(source.getChild("dexterite").getValue()) end;
	if source.getChild("taille") then newentry.taille.setValue(source.getChild("taille").getValue()) end;

	-- Active properties	
	if source.getChild("deplacement") then  newentry.deplacement.setValue(source.getChild("deplacement").getValue()) end;	
	if source.getChild("initiative") then newentry.initiative.setValue(source.getChild("initiative").getValue()) end;		
	
	-- Armes	
	if source.getChild("arme1") then newentry.arme1.setValue(source.getChild("arme1").getValue()) end;	
	if source.getChild("arme2") then newentry.arme2.setValue(source.getChild("arme2").getValue()) end;	
	if source.getChild("arme3") then newentry.arme3.setValue(source.getChild("arme3").getValue()) end;	
	if source.getChild("arme4") then newentry.arme4.setValue(source.getChild("arme4").getValue()) end;	
	
	-- Retour
	return newentry;
end

function onDrop(x, y, draginfo)
	if draginfo.isType("playercharacter")  then
		local source = draginfo.getDatabaseNode();
		local token = draginfo.getTokenData();

		if source then
			local newentry = addPc(source, token);
		end
		
		return true;
	elseif draginfo.isType("shortcut")  then
		local class, datasource = draginfo.getShortcutData();
		local source = draginfo.getDatabaseNode();

		if source and class == "npc" then
			local newentry = addNpc(source);
			newentry.link.setValue(class, datasource);
			newentry.link.setVisible(true);
		end

		return true;
	end
end

function onSortCompare(w1, w2)
	return w1.initresult.getValue() < w2.initresult.getValue();
end;

function getActiveEntry()
	for k, v in ipairs(getWindows()) do
		if v.isActive() then
			return v;
		end
	end
	
	return nil;
end

function requestActivation(entry)
	for k, v in ipairs(getWindows()) do
		v.setActive(false);
	end
	
	entry.setActive(true);
end

function nextActor()
	local entry = getNextWindow(getActiveEntry());
	if entry then
		requestActivation(entry);
	
		for k, v in ipairs(getWindows()) do
			--v.effects.progressEffects(entry);
		end
	else
		nextRound();
	end
end

function nextRound()
	
	local entry = getNextWindow(nil);
	if entry then
		requestActivation(entry);
		for k, v in ipairs(getWindows()) do
			--v.effects.progressEffects(entry);
		end
	end
	
	window.roundcounter.setValue(window.roundcounter.getValue() + 1);
	
	for k, v in ipairs(getWindows()) do
		--v.effects.progressEffects(nil);
	end
end