--===================================================
--
--				GHI_ItemInfo_Standard
--  			GHI_ItemInfo_Standard.lua
--
--	          (description)
--
-- 	  (c)2011 The Gryphonheart Team
--			All rights reserved
--===================================================
local COMPLEXITY = "standard";

function GHI_ItemInfo_Standard(info)
	local class = GHClass("GHI_ItemInfo_Standard");
	GHInheritNext("GHI_ItemInfo_Basic",class);
	GHI_ItemInfo_Basic(info);

	-- Declaration and default values
	local simpleActions = {};
	local errorThrower = GHI_ErrorThrower();
	local event = GHI_Event();
	local envList = GHI_ScriptEnvList();

	-- Public functions
	class.AddSimpleAction = function(_action)
		if not (tContains(simpleActions, _action)) then
			table.insert(simpleActions, _action);
		end
	end

	class.CloneItem = function()
		return GHI_ItemInfo_Standard(class.Serialize());
	end

	class.GetActionData = function()
		return class.Serialize();
	end

	class.GetDependingItems = function()
		local depending = {}
		for _, action in pairs(simpleActions) do
			local itemsDependingOnAction = action.GetDependingItems();

			for _, itemGuid in pairs(itemsDependingOnAction) do
				if not (tContains(depending, itemGuid)) then
					tinsert(depending, itemGuid)
				end
			end
		end

		return depending;
	end

	class.GetInspectionLines = function()
		local lines = {};

		table.insert(lines, {
			order = 120,
			text = string.format("ID: %s", class.GetGUID()),
			r = 0.4,
			g = 0.8,
			b = 1.0,
		});

		table.insert(lines, {
			order = 121,
			text = string.format("%s simple actions.", #(simpleActions)),
			r = 0.4,
			g = 0.8,
			b = 1.0,
		});

		local differentActions = {};
		for _, action in pairs(simpleActions) do
			local n = action.GetActionInfo();
			differentActions[n] = (differentActions[n] or 0) + 1;
		end

		local i = 122;
		for name, count in pairs(differentActions) do
			table.insert(lines, {
				order = i,
				text = string.format("   %s: %s", name, count),
				r = 0.4,
				g = 0.8,
				b = 1.0,
			});
			i = i + 1;
		end
		return lines;
     end

	class.GetItemComplexity = function()
		return COMPLEXITY;
	end

	class.GetSimpleAction = function(index)
		return simpleActions[index]
	end

	class.GetSimpleActionCount = function()
		return #(simpleActions)
	end

	class.RemoveSimpleAction = function(_action)
		for i, action in pairs(simpleActions) do
			if action == _action then
				table.remove(simpleActions, i)
				return;
			end
		end
	end

	class.UseItem = function(stack)
		if (class.IsOnCooldown()) then
			errorThrower.CantUseItem();
			return
		end

		local authorGuid = class.GetAuthorInfo();
		local env = envList.GetEnv(authorGuid);

		-- Execute all actions to run before requirement
		for i, action in pairs(simpleActions) do
			if action.GetExecutionOrder() == 4 and not(action.GetActionType() == "requirement") then
				action.Execute(env, stack)
			end
		end

		-- Execute all requirements
		local fulfilled = true;
		for i, action in pairs(simpleActions) do
			if action.GetActionType() == "requirement" then
				fulfilled = fulfilled and action.Execute(env, stack)
			end
		end

		for i, action in pairs(simpleActions) do
			if action.GetExecutionOrder() == 1 or (action.GetExecutionOrder() == 2 and fulfilled) or  (action.GetExecutionOrder() == 3 and not(fulfilled)) then
				if not(action.GetActionType() == "requirement") then
					action.Execute(env, stack)
				end
			end
		end

		local parentContainerGuid = stack.GetParentContainer().GetGUID();
		if class.IsConsumed() then
			stack.DeleteItem(1);
		end

		class.SetLastCastTime(time());
		event.TriggerEvent("GHI_BAG_UPDATE_COOLDOWN",parentContainerGuid , class.GetGUID());
	end;

	-- Serialization
	local OtherSerialize = class.Serialize;
	class.Serialize = function(stype,t)
		t = t or {};
		if not(stype) or stype == "link" then
			t.itemComplexity = COMPLEXITY;
		end
		if not(stype) or stype == "action" or stype == "oldAction" then
			t.rightClick = t.rightClick or {};
			t.rightClick.Type = "multible";

			local bagIndex;
			for i, action in pairs(simpleActions) do
				t.rightClick[i] = action.Serialize(stype);
				if action.GetActionType() == "bag" then
					bagIndex = i;
				end
			end
			if bagIndex and stype == "oldAction" then
				t.rightClick.bag_index = bagIndex;
			end
		end

		if OtherSerialize then
			t = OtherSerialize(stype,t)
		end
		return t;
	end

	-- Initialize
	info = info or {};
	info.rightClick = info.rightClick or {};
	for i, simpleActionInfo in pairs(info.rightClick) do
		if type(simpleActionInfo) == "table" and type(i) == "number" then
			local simpleAction = GHI_SimpleAction(simpleActionInfo);
			table.insert(simpleActions, simpleAction);
		end
	end

	return class;
end

