-- 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 2006 SmiteWorks Ltd.


function onInit()
	Interface.onHotkeyActivated = onHotkey;
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(name, source, token)
	local newentry = createWindow();
	
	newentry.setType("pc");
	
	-- Name and shortcut
	-- Name

	newentry.name.setValue(name);
	newentry.name.setReadOnly(true);
	newentry.name.setFrame(nil);
	
	newentry.link.setValue("charsheet", source.getNodeName());
	newentry.link.setVisible(true);
	
	newentry.name.setValue(source.getChild("name").getValue());

	-- Token
	if token then
		newentry.token.setPrototype(token);
	end
	
	-- FoF
	newentry.friendfoe.setState("friend");

	-- Wounds
	if source.getChild("wc") then
		newentry.wound.setValue(source.getChild("wc").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

	
	return newentry;
end

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

	newentry.setType("npc");

	-- Name
	newentry.name.setValue(source.getChild("name").getValue());
	
	-- Space/reach

	
	-- Token
	if source.getChild("token") then
		newentry.token.setPrototype(source.getChild("token").getValue());
	end
	
	-- FoF
	newentry.friendfoe.setState("foe");

	-- Wounds
	newentry.wound.setValue(source.getChild("wound").getValue());
	-- Weaponskill
	newentry.weaponskill.setValue(source.getChild("weaponskill").getValue());
	-- Ballistic Skill
	newentry.ballisticskill.setValue(source.getChild("ballisticskill").getValue())
	-- Strength
	newentry.strength.setValue(source.getChild("strength").getValue())
	-- Toughness
	newentry.toughness.setValue(source.getChild("toughness").getValue())
	-- AC
	newentry.ac.setValue(source.getChild("ac").getValue())
	-- Agility
	newentry.agility.setValue(source.getChild("agility").getValue())
	-- Attack
	newentry.attack.setValue(source.getChild("attack").getValue())
	-- Move
	newentry.move.setValue(source.getChild("move").getValue())
	-- Weapons
	newentry.weapons.setValue(source.getChild("weapons").getValue())
	-- Talents
	newentry.talents.setValue(source.getChild("talents").getValue())
	
	return newentry;
end

function onDrop(x, y, draginfo)
	if draginfo.isType("playercharacter")  then
		local name = draginfo.getStringData();
		local source = draginfo.getDatabaseNode();
		local token = draginfo.getTokenData();
		
		local newentry = addPc(name, source, token);
		
		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