--===================================================
--
--				GHI_CreateItemAPI
--  			GHI_CreateItemAPI.lua
--
--	          (description)
--
-- 	  (c)2011 The Gryphonheart Team
--			All rights reserved
--===================================================

local class;
function GHI_CreateItemAPI()
	if class then
		return class;
	end
	class = GHClass("GHI_CreateItemAPI");
	local itemList = GHI_ItemInfoList();
	local containerList = GHI_ContainerList();

	local GetItemAPI = function(item)
		local api = {};

		-- Access api
		api.IsEditable = item.IsEditable;
		api.IsCopyable = item.IsCopyable;
		api.SetCopyable = item.SetCopyable;
		api.SetEditable = item.SetEditable;
		api.GetAuthorInfo = item.GetAuthorInfo;

		-- Basic
		api.DisplayItemTooltip = item.DisplayItemTooltip;
		api.GetFlavorText = item.GetFlavorText;
		api.GetGUID = item.GetGUID;
		api.GetItemInfo = item.GetItemInfo;
		api.IsConsumed = item.IsConsumed;
		api.SetConsumed = item.SetConsumed;
		api.SetComment = item.SetComment;
		api.SetIcon = item.SetIcon;
		api.SetName = item.SetName;
		api.SetQuality = item.SetQuality;
		api.SetUseText = item.SetUseText;
		api.SetStackSize = item.SetStackSize;
		api.SetWhite1 = item.SetWhite1;
		api.SetWhite2 = item.SetWhite2;

		-- Cooldown
		api.GetCooldown = item.GetCooldown;
		api.GetLastCastTime = item.GetLastCastTime;
		api.IsOnCooldown = item.IsOnCooldown;
		api.SetCooldown = item.SetCooldown;
		api.SetLastCastTime = item.SetLastCastTime;

		-- Version
		api.GetVersions = item.GetVersions;
		api.IncreaseVersion = item.IncreaseVersion;
		api.GetItemComplexity = item.GetItemComplexity;

		-- Standard
		api.AddSimpleAction = function(actionInfo)
			if not(type(actionInfo)=="table") then
				error("Usage: item.AddSimpleAction(table)",2);
			end
			item.AddSimpleAction(GHI_SimpleAction(actionInfo));
		end;

		api.GetSimpleAction = function(index)
			local action = item.GetSimpleAction(index);
			if action then
				return action.GetInfo();
			end
		end;
		api.GetSimpleActionCount = item.GetSimpleActionCount;
		api.RemoveSimpleAction = function(index)
			item.RemoveSimpleAction(item.GetSimpleAction(index));
		end;

		return api;
	end

	local EditItem = function(env, guid)
		if not(type(guid) == "string") then
			error("Usage: GHI_EditItem(guid)",2);
		end

		local item = itemList.GetItemInfo(guid);
		if not(item) then
			error("GHI_EditItem: Item not found.",2);
		end

		local authorGuid = item.GetAuthorInfo();
		local ownerGuid = env.GetOwner();
		if not(item.IsEditable() or authorGuid == ownerGuid) then
			error("GHI_EditItem: Access to item denied",2);
		end

		local api = GetItemAPI(item);
		api.Save = function()
			item.IncreaseVersion();
			itemList.UpdateItem(item);
		end
		return api;
	end

	local NewItem = function(env)
		local ownerGuid = env.GetOwner();
		local item = GHI_ItemInfo({
			authorGuid = ownerGuid,
			authorName = "GHI Script",
		});

		local api = GetItemAPI(item);
		api.Save = function(targetContainerGuid)
			itemList.UpdateItem(item);
			-- targetContainerGuid
			if not(targetContainerGuid) then
				containerList.InsertItemInMainBag(item.GetGUID());
			else
			 	containerList.InsertItemInBag(item.GetGUID(), nil, targetContainerGuid);
			end
		end
		return api;
	end

	class.GetAPI = function(env)
		local a = {};
		a.GHI_EditItem = function(guid)
			return EditItem(env, guid);
		end;
		a.GHI_CreateItem = function()
			return NewItem(env);
		end;
		return a;
	end

	return class;
end

