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


-- Section visibility handling

forceactive = false;
forcedefensive = false;
defensiveon = false;
activeon = false;

charactertype = nil;

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

function setDefensiveVisible(status)
	if forcedefensive then
		status = true;
	end
	if not targeting.isEmpty() then
		status = true;
	end
	
	defensiveon = status;

	ac.setVisible(status);
	aclabel.setVisible(status);
	space.setVisible(status);
	reach.setVisible(status);
	targeting.setVisible(status);
	defensiveicon.setVisible(status);
	
	setSpacerState();
end
	
function setActiveVisible(status)
	if forceactive then
		status = true;
	end
	if active.getState() then
		status = true;
	end
	
	activeon = status;

	move.setVisible(status);
	movelabel.setVisible(status);
	talents.setVisible(status);
	talentslabel.setVisible(status);
	weapons.setVisible(status);
	weaponslabel.setVisible(status);
	attack.setVisible(status);
	attacklabel.setVisible(status);
	activeicon.setVisible(status);
	
	setSpacerState();
end

function toggleForceActive()
	forceactive = not forceactive;
	
	if forceactive then
		activateactive.setColor("ffffffff");
	else
		activateactive.setColor("7fffffff");
	end
end

function toggleForceDefensive()
	forcedefensive = not forcedefensive;
	
	if forcedefensive then
		activatedefensive.setColor("ffffffff");
	else
		activatedefensive.setColor("7fffffff");
	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()
	setDefensiveVisible(false);
	setActiveVisible(false);
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


-- New Code

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

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

--function restoreEffects(entry, idmap)
--	effects.clearAndDisableCheck();
--
--	if entry.effects then
--		for k,v in ipairs(entry.effects) do
--			local win = effects.createWindow();
--			win.label.setValue(v.label);
--			win.duration.setValue(v.duration);
--			win.adjustment.setValue(v.adjustment);
--			win.caster.setSource(idmap[v.sourceid]);
--		end
--	end	
--	effects.enableCheck();
--end