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

charactertype = nil;

function setSpacerState()
	if defensiveon or activeon or effectson then
		spacer.setVisible(true);
	else
		spacer.setVisible(false);
	end
end


-- FoF State
function getFoF()
	return friendorfoe;
end

function setFoF(state)
	friendorfoe = state;
	token.updateUnderlay();
end

-- Activity state
function isActive()
	return active.getState();
end

function setActive(state)
	active.setState(state);
	
	if state and charactertype == "pc" then
		-- Turn notification
		local msg = {};
		msg.text = name.getValue();
		msg.font = "narratorfont";
		msg.icon = "indicator_flag";
		
		ChatManager.deliverMessage(msg);
		
		local usernode = link.getTargetDatabaseNode()
		if usernode then
			User.ringBell(usernode.getName());
		end
	end
end

-- Observers to support effects linked here

observers = {};

function addObserver(o)
	table.insert(observers, o);
end

function removeObserver(o)
	for i = 1, #observers do
		if observers[i] == o then
			table.remove(observers, i);
			break;
		end
	end
end



function onInit()
end

function onClose()
	for k, v in ipairs(observers) do
		v.observedClosed(self);
	end
end

function nameChanged()
	for k, v in ipairs(observers) do
		v.observedNameChanged(self);
	end
end

function setType(t)
	charactertype = t;
end

function getType()
	return charactertype;
end

function store()
	local entry = {};

	-- Name, type and link target
	entry.name = name.getValue();
	entry.type = charactertype;
	
	if link.getTargetDatabaseNode() then
		entry.dbnode = link.getTargetDatabaseNode().getNodeName();
	end

	-- Token and token instance
	entry.token = token.getPrototype();

	if token.ref and token.ref.getContainerNode() then
		entry.tokenrefnode = token.ref.getContainerNode().getNodeName();
		entry.tokenrefid = token.ref.getId();
	end
	
	-- FoF
	entry.fof = getFoF();

	-- Valeurs liées
	entry.sante = sante.getValue();
	entry.blessure_let = blessure_let.getValue();
	entry.blessure_nonlet = blessure_nonlet.getValue();
		
	entry.initresult = initresult.getValue();
	entry.space = space.getValue();
	entry.reach = reach.getValue();
	
	entry.defense = defense.getValue();
	entry.corps = corps.getValue();
	entry.dexterite = dexterite.getValue();
	entry.taille = taille.getValue();
	
	entry.deplacement = deplacement.getValue();
	entry.initiative = initiative.getValue();
	
	entry.arme1 = arme1.getValue();
	entry.arme2 = arme2.getValue();
	entry.arme3 = arme3.getValue();
	entry.arme4 = arme4.getValue();
	
	
	return entry;
end


function restore(entry)
	-- Name, type and linking
	name.setValue(entry.name);

	local targetnode = nil;
	if entry.dbnode then
		targetnode = DB.findNode(entry.dbnode);
	end
	
	if entry.type == "pc" and targetnode then
		charactertype = "pc";
		link.setValue("charsheet", entry.dbnode);
		link.setVisible(true);

		name.setReadOnly(true);
		name.setFrame(nil);
		
		-- Links to data
		name.setLink(targetnode.getChild("name"));
		sante.setLink(targetnode.getChild("points.sante"));
		blessure_let.setLink(targetnode.getChild("sante.blessure_let"));
		blessure_nonlet.setLink(targetnode.getChild("sante.blessure_nonlet"));		
		--
		defense.setLink(targetnode.getChild("attribut.defense"));
		corps.setLink(targetnode.getChild("attribut.corps"));
		dexterite.setLink(targetnode.getChild("attribut.dexterite"));
		taille.setLink(targetnode.getChild("attribut.taille"));
		deplacement.setLink(targetnode.getChild("attribut.deplacement"));
		initiative.setLink(targetnode.getChild("attribut.initiative"));

	
		--arme1
		local arme1_nom = targetnode.getChild("arme1.nom").getValue();
		local arme1_niveau = targetnode.getChild("arme1.niveau").getValue();
		local arme1_portee = targetnode.getChild("arme1.portee").getValue();
		local arme1_cadence = targetnode.getChild("arme1.cadence").getValue();	
		arme1.setValue(arme1_nom .. " -  Niv: " .. arme1_niveau .. " -  Cad: " .. arme1_cadence .. " - Port: " .. arme1_portee);
		--arme2
		local arme2_nom = targetnode.getChild("arme2.nom").getValue();
		local arme2_niveau = targetnode.getChild("arme2.niveau").getValue();
		local arme2_portee = targetnode.getChild("arme2.portee").getValue();
		local arme2_cadence = targetnode.getChild("arme2.cadence").getValue();	
		arme2.setValue(arme2_nom .. " -  Niv: " .. arme2_niveau .. " -  Cad: " .. arme2_cadence .. " - Port: " .. arme2_portee);	
		--arme3
		local arme3_nom = targetnode.getChild("arme3.nom").getValue();
		local arme3_niveau = targetnode.getChild("arme3.niveau").getValue();
		local arme3_portee = targetnode.getChild("arme3.portee").getValue();
		local arme3_cadence = targetnode.getChild("arme3.cadence").getValue();	
		arme3.setValue(arme3_nom .. " -  Niv: " .. arme3_niveau .. " -  Cad: " .. arme3_cadence .. " - Port: " .. arme3_portee);	
		--arme4
		local arme4_nom = targetnode.getChild("arme4.nom").getValue();
		local arme4_niveau = targetnode.getChild("arme4.niveau").getValue();
		local arme4_portee = targetnode.getChild("arme4.portee").getValue();
		local arme4_cadence = targetnode.getChild("arme4.cadence").getValue();	
		arme4.setValue(arme4_nom .. " -  Niv: " .. arme4_niveau .. " -  Cad: " .. arme4_cadence .. " - Port: " .. arme4_portee);
		
		
	else
		if entry.type == "npc" and targetnode then
			charactertype = "npc";
			link.setValue("npc", entry.dbnode);
			link.setVisible(true);
		end
		
		-- sante
		sante.setValue(entry.sante);
		blessure_let.setValue(entry.blessure_let);
		blessure_nonlet.setValue(entry.blessure_nonlet);		
	end
	
	-- Token
	if entry.tokenrefnode and entry.tokenrefid then
		token.acquireReference(token.populateFromImageNode(entry.tokenrefnode, entry.tokenrefid));
	elseif entry.token then
		token.setPrototype(entry.token);
	end
	
	-- FoF
	friendfoe.setState(entry.fof);
	
	-- Value fields
	initresult.setValue(entry.initresult);
	space.setValue(entry.space);
	reach.setValue(entry.reach);
	
	defense.setValue(entry.defense);
	corps.setValue(entry.corps);
	dexterite.setValue(entry.dexterite);
	taille.setValue(entry.taille);	
	
	deplacement.setValue(entry.deplacement);
	initiative.setValue(entry.initiative);
	
	arme1.setValue(entry.arme1);	
	arme2.setValue(entry.arme2);	
	arme3.setValue(entry.arme3);	
	arme4.setValue(entry.arme4);	
	
	
	token.updateUnderlay();
end

