--[[
	Bags.lua
		Functions used by Bagnon Bags
--]]


function BagnonBag_OnLoad()
	this:RegisterEvent("BAG_UPDATE");
	this:RegisterEvent("ITEM_LOCK_CHANGED");
	
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("PLAYER_LEAVING_WORLD");
	
	this:RegisterEvent("UNIT_INVENTORY_CHANGED");
	this:RegisterEvent("CURSOR_UPDATE");
	this:RegisterEvent("BAG_UPDATE_COOLDOWN");
	this:RegisterEvent("UPDATE_INVENTORY_ALERTS");
	
	this:RegisterForDrag("LeftButton");
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	
	local textureName = GetInventoryItemTexture("player", ContainerIDToInventoryID(this:GetID()) );
	
	getglobal(this:GetName() .. "IconTexture"):SetTexture(textureName);
	this.backgroundTextureName = textureName;
	BagnonBag_Update();
end

function BagnonBag_OnEvent(event)
	if ( not this:IsVisible() or Bagnon_IsCachedBag(this:GetParent():GetParent().player, this:GetID()) ) then
		return;
	end

	if ( event == "UNIT_INVENTORY_CHANGED" ) then
		if ( arg1 == "player" ) then
			BagnonBag_Update();
		end
		return;
	end
	if ( event == "ITEM_LOCK_CHANGED" ) then
		BagnonBag_UpdateLock();
		return;
	end
	if ( event == "BAG_UPDATE_COOLDOWN" ) then
		BagnonBag_Update(1);
		return;
	end
	if ( event == "CURSOR_UPDATE" ) then
		if ( CursorCanGoInSlot( ContainerIDToInventoryID(this:GetID()) ) ) then
			this:LockHighlight();
		else
			this:UnlockHighlight();
		end
		return;
	end
	if ( event == "BAG_UPDATE" ) then
		BagnonBag_Update();
		return;
	end
	if ( event == "UPDATE_INVENTORY_ALERTS" ) then
		BagnonBag_Update();
	end
	
	if (event == "PLAYER_LEAVING_WORLD") then
		this:UnregisterEvent("BAG_UPDATE");
		this:UnregisterEvent("ITEM_LOCK_CHANGED");
		return;
	end
	
	if (event == "PLAYER_ENTERING_WORLD") then
		this:RegisterEvent("BAG_UPDATE");
		this:RegisterEvent("ITEM_LOCK_CHANGED");
		BagnonBag_Update();
		return;
	end
end

function BagnonBag_Update(cooldownOnly)
	local invID = ContainerIDToInventoryID(this:GetID());
	
	local textureName = GetInventoryItemTexture("player", invID);
	local cooldown = getglobal(this:GetName().."Cooldown");
	if ( textureName ) then
		SetItemButtonTexture(this, textureName);
		SetItemButtonCount(this, GetInventoryItemCount("player", invID));
		
		if ( not IsInventoryItemLocked(invID) ) then 
			SetItemButtonTextureVertexColor(this, 1.0, 1.0, 1.0);
			SetItemButtonNormalTextureVertexColor(this, 1.0, 1.0, 1.0);
		end
		this.hasItem = 1;
	else
		SetItemButtonTexture(this, this.backgroundTextureName);
		SetItemButtonCount(this, 0);
		SetItemButtonTextureVertexColor(this, 1.0, 1.0, 1.0);
		SetItemButtonNormalTextureVertexColor(this, 1.0, 1.0, 1.0);
		cooldown:Hide();
		this.hasItem = nil;
	end

	local start, duration, enable = GetInventoryItemCooldown("player", invID);
	CooldownFrame_SetTimer(cooldown, start, duration, enable);


	if ( GameTooltip:IsOwned(this) ) then
		if ( textureName or cooldownOnly ) then
			BagnonBag_OnEnter();
		else
			GameTooltip:Hide();
			ResetCursor();
		end
	end
	BagnonBag_UpdateLock();

	-- Update repair all button status
	if ( MerchantRepairAllIcon ) then
		local repairAllCost, canRepair = GetRepairAllCost();
		if ( canRepair ) then
			SetDesaturation(MerchantRepairAllIcon, nil);
			MerchantRepairAllButton:Enable();
		else
			SetDesaturation(MerchantRepairAllIcon, 1);
			MerchantRepairAllButton:Disable();
		end	
	end
end

function BagnonBag_UpdateLock()
	if ( IsInventoryItemLocked( ContainerIDToInventoryID(this:GetID()) ) ) then
		SetItemButtonDesaturated(this, 1, 0.5, 0.5, 0.5);
	else 
		SetItemButtonDesaturated(this, nil);
	end
end

function BagnonBag_OnUpdate(elapsed)
	if ( GameTooltip:IsOwned(this) ) then
		BagnonBag_OnEnter();
	end
end

function BagnonBag_OnClick()
	if ( Bagnon_IsCachedBag(this:GetParent():GetParent().player, this:GetID() ) ) then
		return;
	end

	if(not IsShiftKeyDown() ) then
		PutItemInBag( ContainerIDToInventoryID(this:GetID()) );
	else
		BagnonCore_ToggleBagForFrame(this:GetParent():GetParent(), this:GetID());
	end
end

function BagnonBag_OnDrag()
	if (Bagnon_IsCachedBag(this:GetParent():GetParent().player, this:GetID() ) ) then 
		return;
	end
	
	PickupBagFromSlot( ContainerIDToInventoryID(this:GetID()) );
	PlaySound("BAGMENUBUTTONPRESS");
end

function BagnonBag_OnEnter()
	local frame = this:GetParent():GetParent();
	BagnonCore_HighlightSlots(this:GetParent():GetParent(), this:GetID());
	
	
	if( this:GetLeft() < ( UIParent:GetRight() / 2) ) then
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
	else
		GameTooltip:SetOwner(this, "ANCHOR_LEFT");
	end
	
	if( Bagnon_IsCachedBag(frame.player, this:GetID()) ) then
		local size, link = BagnonDB_GetBagData(frame.player, this:GetID());
		
		if(link) then
			GameTooltip:SetHyperlink( link );
			GameTooltip:Show();
			return;
		end
	elseif ( GameTooltip:SetInventoryItem("player", ContainerIDToInventoryID(this:GetID()) ) ) then
		return;
	end
	GameTooltip:SetText(TEXT(EQUIP_CONTAINER), 1.0, 1.0, 1.0);
end


function BagnonBag_OnShow()
	BagnonBag_UpdateTexture(this:GetParent():GetParent(), this:GetID());
end


--[[
	Update the texture and count of the bag
		Used mainly for cached bags
--]]

function BagnonBag_UpdateTexture(frame, bagID)
	local bag = getglobal(frame:GetName() .. "Bags" .. bagID);
	
	if(not bag) then
		return;
	end
	
	if( Bagnon_IsCachedBag(frame.player, bagID) ) then
		local size, link, count = BagnonDB_GetBagData(frame.player, bagID);
		if(link) then
			local _, _, _, _, _, _, _, _, texture = GetItemInfo(link);
			SetItemButtonTexture(bag, texture);
		else
			SetItemButtonTexture(bag, bag.backgroundTextureName);
		end
		if(count) then
			SetItemButtonCount(bag, count);
		end
	else
		local texture = GetInventoryItemTexture("player", ContainerIDToInventoryID(bagID));
		if(texture) then
			SetItemButtonTexture(bag, texture);
		else
			SetItemButtonTexture(bag, bag.backgroundTextureName);
		end
		SetItemButtonCount(bag, GetInventoryItemCount("player", ContainerIDToInventoryID(bagID)));
	end
end