--[[
	BagnonItem
		Functions used by the item slots in Bagnon
	
		Most of this file is directly taken from the normal bag item code
--]]

local HEX = {
	["a"] = 10,
	["b"] = 11,
	["c"] = 12,
	["d"] = 13,
	["e"] = 14,
	["f"] = 15,
	["0"] = 0,
	["1"] = 1,
	["2"] = 2,
	["3"] = 3,
	["4"] = 4,
	["5"] = 5,
	["6"] = 6,
	["7"] = 7,
	["8"] = 8,
	["9"] = 9,
};

function BagnonItem_OnLoad()
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	this:RegisterForDrag("LeftButton");
end

function BagnonItem_OnClick(button, ignoreModifiers)
	if ( this.isLink ) then
		if(this.hasItem) then
			if ( button == "LeftButton" ) then
				if ( IsControlKeyDown() ) then
					DressUpItemLink( BagnonDB_GetItemLink(this) );
				elseif( IsShiftKeyDown() ) then
					ChatFrameEditBox:Insert( BagnonDB_GetFullItemLink(this) );
				end
			end
		end
	else
		this:GetParent():SetID(this.bag);
		ContainerFrameItemButton_OnClick(button, ignoreModifiers);
	end
end

--[[
	Show tooltip on hover
--]]

function BagnonItem_OnEnter(button)
	if ( not button ) then
		button = this;
	end
	
	--link case
	if(button.isLink) then
		if(button.hasItem)then
			GameTooltip:SetOwner(button);
			GameTooltip:SetHyperlink( BagnonDB_GetItemLink(button) );
			Bagnon_AnchorTooltip(button);
		end
	--normal bag case
	else	
		--blizzard totally needs to make the bank button not a special case
		if(button.bag == -1) then
			GameTooltip:SetOwner(button);
			GameTooltip:SetInventoryItem("player", BankButtonIDToInvSlotID(button:GetID()) );
		--normal item case
		else
			button:GetParent():SetID(button.bag);
			ContainerFrameItemButton_OnEnter(button);
		end
		
		--Don't reposition tooltips for things using EnhTooltip
		if( not (EnhTooltip) ) then
			Bagnon_AnchorTooltip(button);
		end
	end
end

function BagnonItem_OnUpdate()
	if ( GameTooltip:IsOwned(this) ) then
		BagnonItem_OnEnter();
	end
end

--[[
	Update tooltip info
--]]

--Adapted from OneBag.  The author had a clever idea of using the link to generate the color instead of getting the quality, then the color
function BagnonItem_UpdateBorder(button)

	local link = GetContainerItemLink(button.bag, button:GetID());
	
	if(link) then
		--item border coloring
		local _, _, hexString = strfind( link ,"|cff(%w+)|H");
		local red = ( 16 * HEX[string.sub(hexString, 1, 1)] + HEX[string.sub(hexString, 2, 2)] ) / 256;
		local green = ( 16 * HEX[string.sub(hexString, 3, 3)] + HEX[string.sub(hexString, 4, 4)] ) / 256;
		local blue = ( 16 * HEX[string.sub(hexString, 5, 5)] + HEX[string.sub(hexString, 6, 6)] ) / 256;
		
		if(getglobal(button:GetName() .. "Border") and (red ~= green and red ~= blue ) ) then
			getglobal(button:GetName() .. "Border"):SetVertexColor(red, green, blue, 0.4);
			getglobal(button:GetName() .. "Border"):Show();
		else
			getglobal(button:GetName() .. "Border"):Hide();
		end
	else
		--ammo slot coloring
		getglobal(button:GetName() .. "Border"):Hide();
		if( Bagnon_IsAmmoBag( button.bag) ) then
			getglobal(button:GetName() .. "NormalTexture"):SetVertexColor(1, 1, 0);
		else
			getglobal(button:GetName() .. "NormalTexture"):SetVertexColor(1, 1, 1);
		end
	end
end

function BagnonItem_UpdateLinkBorder(item)
	local id = BagnonDB_GetItemID(item);
	
	if(id) then
		local name, link, quality = GetItemInfo( id );
		local red, green, blue = GetItemQualityColor(quality);
		
		if(getglobal(item:GetName() .. "Border") and (red ~= green and red ~= blue ) ) then
			getglobal(item:GetName() .. "Border"):SetVertexColor(red, green, blue, 0.4);
			getglobal(item:GetName() .. "Border"):Show();
		else
			getglobal(item:GetName() .. "Border"):Hide();
		end
	else
		getglobal(item:GetName() .. "Border"):Hide();
	end
	
	getglobal(item:GetName() .. "NormalTexture"):SetVertexColor(1, 1, 1);
end

--[[ 
	Update cooldown
		Not using the default one because I can add cooldown count later
--]]

function BagnonItem_UpdateCooldown(container, button)
	local cooldown = getglobal(button:GetName().."Cooldown");
	local start, duration, enable = GetContainerItemCooldown(container, button:GetID());
	
	CooldownFrame_SetTimer(cooldown, start, duration, enable);
	
	if ( duration > 0 and enable == 0 ) then
		SetItemButtonTextureVertexColor(button, 0.4, 0.4, 0.4);
	end
end