-- 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.

-- Grimmbart 		: V1.0.3


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

function onClose()
	-- storeToRegistry();		Suppression, Bug en Sortie
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

		-- Second pass, create effects with ids
		-- for k, v in ipairs(trackertable) do
		-- 	idmap[k].restoreEffects(v, idmap);
		--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
	-- Second pass, store effects
	-- for k,v in pairs(idmap) do
	--	k.storeEffects(trackertable[v], idmap);
	--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");
	-- Wounds
	if source.getChild("Blessure_ValCourante_Valeur") then
		newentry.wound.setValue(source.getChild("Blessure_ValCourante_Valeur").getValue())
	else
		newentry.wound.setValue("0")
	end
	-- Weaponskill
	if source.getChild("weaponskill") then
		newentry.weaponskill.setValue(source.getChild("weaponskill").getValue())
	else
		newentry.weaponskill.setValue("0")
	end
	-- Ballistic Skill
	if source.getChild("bsc") then
		newentry.ballisticskill.setValue(source.getChild("bsc").getValue())
	else
		newentry.ballisticskill.setValue("0")
	end
	-- Strength
	if source.getChild("sb") then
		newentry.strength.setValue(source.getChild("sb").getValue())
	else
		newentry.strength.setValue("0")
	end
	-- Toughness
	if source.getChild("tb") then
		newentry.toughness.setValue(source.getChild("tb").getValue())
	else
		newentry.toughness.setValue("0")
	end
	-- Agility
	if source.getChild("agc") then
		newentry.agility.setValue(source.getChild("agc").getValue())
	else
		newentry.agility.setValue("0")
	end
	-- Attack
	if source.getChild("ac") then
		newentry.attack.setValue(source.getChild("ac").getValue())
	else
		newentry.attack.setValue("0")
	end
	-- Move
	if source.getChild("mc") then
		newentry.move.setValue(source.getChild("mc").getValue())
	else
		newentry.move.setValue("0")
	end
	-- Armor Synopsis
	if source.getChild("combatarmor") then
		newentry.ac.setValue(source.getChild("combatarmor").getValue())
	else
		newentry.ac.setValue("")
	end
	-- Weapons Synopsis
	if source.getChild("combatweapons") then
		newentry.weapons.setValue(source.getChild("combatweapons").getValue())
	else
		newentry.weapons.setValue("")
	end
	-- Talents Synopsis
	if source.getChild("combattalents") then
		newentry.talents.setValue(source.getChild("combattalents").getValue())
	else
		newentry.talents.setValue("")
	end
	-- Retour
	return newentry;
end

function addNpc(source)
	local newentry = createWindow();
	-- Affectation du type NPC
	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
	newentry.friendfoe.setState("foe");
	-- Blessure
	if source.getChild("wound") then
		newentry.wound.setValue(source.getChild("wound").getValue());
	end
	-- Weaponskill
	if source.getChild("weaponskill") then
		newentry.weaponskill.setValue(source.getChild("weaponskill").getValue());
	end
	-- Ballistic Skill
	if source.getChild("ballisticskill") then
		newentry.ballisticskill.setValue(source.getChild("ballisticskill").getValue())
	end
	-- Strength
	if source.getChild("strbonus") then
		newentry.strength.setValue(source.getChild("strbonus").getValue())
	end
	-- Toughness
	if source.getChild("toughbonus") then
		newentry.toughness.setValue(source.getChild("toughbonus").getValue())
	end
	-- AC	
	if source.getChild("ac") then
		newentry.ac.setValue(source.getChild("ac").getValue())
	end
	-- Agility
	if source.getChild("agility") then
		newentry.agility.setValue(source.getChild("agility").getValue())
	end
	-- Attack
	if source.getChild("attack") then
		newentry.attack.setValue(source.getChild("attack").getValue())
	end
	-- Move
	if source.getChild("move") then
		newentry.move.setValue(source.getChild("move").getValue())
	end
	-- Weapons
	if source.getChild("weapons") then
		newentry.weapons.setValue(source.getChild("weapons").getValue())
	end
	-- Talents
	if source.getChild("talents") then
		newentry.talents.setValue(source.getChild("talents").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);
	else
		nextRound();
	end
end

function nextRound()
	local entry = getNextWindow(nil);
	if entry then
		requestActivation(entry);		
	end
	
	window.roundcounter.setValue(window.roundcounter.getValue() + 1);	
end