GHI_ActionBarData = {};

--[[
Action bar ID's:
	Main action bar page 1
	buttom left = 6
	buttom right = 5
	right = 3
	left = 4;	

]]

function GHI_GetActionBarData(bar,slot)
	if not(bar) then return nil; end
	bar = tonumber(bar);
	if not(bar) then return nil; end
	
	if not(slot) then return nil; end
	slot = tonumber(slot);
	if not(slot) then return nil; end
	
	if bar > 11 or bar < 1 or slot > 12 or slot < 1 then
		return;
	end
	
	if not(type(GHI_ActionBarData)=="table") then
		GHI_ActionBarData = {};	
	end
	
	if not(type(GHI_ActionBarData[bar])=="table") then
		GHI_ActionBarData[bar] = {};	
	end
	
	return GHI_ActionBarData[bar][slot];
end

function GHI_SetActionBarData(bar,slot,id)
	if not(bar) then return nil; end
	bar = tonumber(bar);
	if not(bar) then return nil; end
	
	if not(slot) then return nil; end
	slot = tonumber(slot);
	if not(slot) then return nil; end
	
		
	if bar > 11 or bar < 1 or slot > 12 or slot < 1 then
		return;
	end
	
	if not(type(GHI_ActionBarData)=="table") then
		GHI_ActionBarData = {};	
	end
	
	if not(type(GHI_ActionBarData[bar])=="table") then
		GHI_ActionBarData[bar] = {};	
	end
	
	GHI_ActionBarData[bar][slot] = id;
	
end

function GHI_ActionbarHookings(self)
	
	

	
end


GHI_Barnames = {};
GHI_Barnames[6] = "GHIMultiBarBottomLeft";
GHI_Barnames[5] = "GHIMultiBarBottomRight";
GHI_Barnames[3] = "GHIMultiBarRight";
GHI_Barnames[4] = "GHIMultiBarLeft";

function GHI_ShowAllActionbars()
	for i=3,6 do
		local barName = GHI_Barnames[i];
		local bar = _G[barName];
		if not(bar) then
			break
		end
		local relatedBar = _G[gsub(barName,"GHI","")];
		
		if relatedBar:IsShown() then
			bar:Show(); 
			for j=1,12 do
				local id = GHI_GetActionBarData(i,j);
				local button = _G[barName.."Button"..j];
				if button then
					button:Show();
				end
			end
			bar:SetFrameLevel(relatedBar:GetFrameLevel()+1);
		else
			bar:Hide();
		end
	end
end

function GHI_HideUnusedActionbars()
		
	for i=3,6 do
		local barName = GHI_Barnames[i];
		local bar = _G[barName];
		if not(bar) then
			break
		end
		local relatedBar = _G[gsub(barName,"GHI","")];
		
		bar:Hide();
		if relatedBar:IsShown() then
			bar:Show(); 
		end
		
		
		for j=1,12 do
			local id = GHI_GetActionBarData(i,j);
			local button = _G[barName.."Button"..j];
			if button then
				if id then
					button.itemID = id;					
					button:Show();
				
				else
					button:Hide();
				
				end	
			end
		end
		bar:SetFrameLevel(relatedBar:GetFrameLevel()+1);
		--GHI_Message("Set level: "..relatedBar:GetFrameLevel());
	end
end



function GHI_ActionSlotButton_OnLoad(self)
	
	
	self:RegisterForDrag("LeftButton", "RightButton");
	if self.RegisterForClicks then
		self:RegisterForClicks("AnyUp");
	end

end

function GHI_ActionSlotButton_OnUpdate(self)
	
	local id = self.itemID;
	local icon = _G[self:GetName().."Icon"];
	local normalTexture = _G[self:GetName().."NormalTexture"];
	local name,texture = GHI_GetItemInfo(id);
	local barName = self:GetParent():GetName();
	local bar = _G[barName];
	local relatedBar = _G[gsub(barName,"GHI","")];
	local buttonCooldown = _G[self:GetName().."Cooldown"];
	local count = _G[self:GetName().."Count"];
	
	if not(InCombatLockdown()) then
		bar:SetFrameLevel(relatedBar:GetFrameLevel()+1);
	end
	
	if name then
		if ( texture ) then
			icon:SetTexture(texture);
			icon:Show();
			self.rangeTimer = -1;
			self:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
			
			
			local amount = GHI_CountItem(id);
			if GHP then
				amount = GHP:CountItem(id,true);
			end
			
			if amount == 1 then
				count:SetText("");
			else 
				count:SetText(amount);
			end
			
			if ( amount == 0 ) then
				icon:SetVertexColor(0.4, 0.4, 0.4);
				--normalTexture:SetVertexColor(1.0, 1.0, 1.0);
			else
				icon:SetVertexColor(1.0, 1.0, 1.0);
				--normalTexture:SetVertexColor(1.0, 1.0, 1.0);
			end
			
			GHI_ActionSlotButton_UpdateCooldown(id, self)
			
		else
			icon:Hide();
			buttonCooldown:Hide();
			self.rangeTimer = nil;
			self:SetNormalTexture("Interface\\Buttons\\UI-Quickslot");
			
			
			count:SetText("");
		end
	elseif id then -- remove the item from action bar if id is illigal
		local barNum = nil;
		for index,value in pairs(GHI_Barnames) do 
			if barName == value then
				barNum = index;
			end
		end
		GHI_SetActionBarData(barNum,self:GetID(),nil);
		self.itemID = nil;
		self:Hide();
	else
		icon:Hide();
		buttonCooldown:Hide();
		count:SetText("");

	end
	
	if ( self.updateTooltipTime ) then
		--self.updateTooltip = self.updateTooltip - elapsed;
		if ( time() - self.updateTooltipTime < 1 ) then
			--GHI_Message("C");
			return;
		end
	end
	self.UpdateTooltip = nil;
	
	--GHI_Message("A");
	if ( GameTooltip:IsOwned(self) ) then --GHI_Message("B");
		GHI_ActionSlotButton_OnEnter(self);
		self.updateTooltipTime = time();
	end
end

function GHI_ActionSlotButton_OnEnter(self)
	if self.itemID then
		if ( GetCVar("UberTooltips") == "1" ) then
			GameTooltip_SetDefaultAnchor(GameTooltip, self);
		else
			if ( self:GetParent() == GHIMultiBarBottomRight or self:GetParent() == GHIMultiBarRight or self:GetParent() == GHIMultiBarLeft ) then
				GameTooltip:SetOwner(self, "ANCHOR_LEFT");
			else
				GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
			end
		end
		GHI_ItemTooltip(self,self.itemID);
		self.UpdateTooltip = nil;
	else
		local n = self:GetName();
		n=gsub(n,"GHI","");
		ActionButton_SetTooltip(_G[n]);
	end





end



function GHI_ActionSlotButton_OnClick(self,button,drag)
	self:SetChecked(nil);
	local id = self.itemID;
	
	local cursor = GHI_GetCursor();
	
	
	
	local relatedSlot = _G[gsub(self:GetName(),"GHI","")];
	local barNum = relatedSlot:GetParent():GetAttribute("actionpage");
	local slotNum = (barNum-1)*12 + relatedSlot:GetID();
	
	
	
	if button == "right" and (( cursor == "item" or cursor == "item/ability" ) or GetCursorInfo() ) then
		if ( cursor == "item" or cursor == "item/ability" ) then 
			GHI_ResetCursor();
		end
	else
	
		 
		if not( cursor == "item" or cursor == "item/ability" ) and not(GetCursorInfo() ) and id then -- cursor is empty and slot got GHI
			if drag then
				-- pickup GHI
				local temp = {};
					
				local name,icon = GHI_GetItemInfo(id);
				
				temp.iconTexture = icon;
				temp.id = id;
				GHI_SetCursor("item/ability",temp);
				GHI_SetActionBarData(barNum,relatedSlot:GetID(),nil);
				self.itemID = nil;
				GHI_HideUnusedActionbars()
								
				
				GHI_ShowAllActionbars();
			else
				-- use item
				--GHI_Message("Use item");
				local bag,slot = GHI_FindItem(id)
				if GHP then
					bag,slot = GHP:FindItem(id,false,true);
				end
				if bag and slot then
					GHI_UseItem(bag,slot)
				end
			end
		elseif (GetCursorInfo() ) and id then --- curser has spell and slot got GHI
			-- place spell, pickup GHI   (give error if in combat)
			if InCombatLockdown()  then
				GHI_Message(GHI_IN_COMBAT_ERROR);
			else
				-- place the action
				PlaceAction(slotNum);
				-- pick up GHI
				local temp = {};
					
				local name,icon = GHI_GetItemInfo(id);
				
				temp.iconTexture = icon;
				temp.id = id;
				GHI_SetCursor("item/ability",temp);
				GHI_SetActionBarData(barNum,relatedSlot:GetID(),nil);
				self.itemID = nil;
				GHI_HideUnusedActionbars()
				
				GHI_ShowAllActionbars();
				
				
			end
		elseif ( cursor == "item" or cursor == "item/ability" ) and id then --- curser has GHI and slot got GHI
			-- place GHI pickup old GHI
			
			
			local _,details = GHI_GetCursor();
			local itemID = details.id;
			GHI_SetActionBarData(barNum,relatedSlot:GetID(),itemID);
			self.itemID = itemID;
			
			local temp = {};
				
			local name,icon = GHI_GetItemInfo(id);
			
			temp.iconTexture = icon;
			temp.id = id;
			GHI_SetCursor("item/ability",temp);
			
			GHI_HideUnusedActionbars();
			GHI_ShowAllActionbars();
		elseif ( cursor == "item" or cursor == "item/ability" ) and HasAction(slotNum) then --- curser has GHI and slot got Spell
			-- place GHI, pickup spell  (give error if in combat)
			if InCombatLockdown()  then
				GHI_Message(GHI_IN_COMBAT_ERROR);
			else
				-- place GHI
				local _,details = GHI_GetCursor();
				local itemID = details.id;
				GHI_SetActionBarData(barNum,relatedSlot:GetID(),itemID);
				self.itemID = itemID;
				
				GHI_HideUnusedActionbars();
				GHI_ResetCursor();
				
				PickupAction(slotNum);
			end
			
		elseif ( cursor == "item" or cursor == "item/ability" ) and not(id) and not(HasAction(slotNum)) then --- curser has GHI and slot is empty
			-- place GHI
			local _,details = GHI_GetCursor();
			local itemID = details.id;
			GHI_SetActionBarData(barNum,relatedSlot:GetID(),itemID);
			self.itemID = itemID;
			
			GHI_HideUnusedActionbars();
			GHI_ResetCursor();
		end
		
	
		
	
	end
	
	

end


function GHI_ActionSlotButton_UpdateCooldown(ID, button)
	
	local cooldown = _G[button:GetName().."Cooldown"];
	if not(button) then 
		return
	end
	
	
	local rightClick = GHI_GetRightClickInfo(ID)
	local start, duration, enable;
	start = GHI_CooldownData[ID];
	
	if start == nil then start = 0; end
	if start > GetTime() then start = 0 end
	
	if not(type(rightClick)=="table") or not(rightClick.CD) then
		duration = 0;
		enable = 0;
	else
		duration = rightClick.CD;
		enable = 1;
	end
	
	if start + duration < GetTime() then start = 0; end
	
	
	CooldownFrame_SetTimer(cooldown, start, duration, enable);
	
	if ( duration > 0 and enable == 0 ) then
		SetItemButtonTextureVertexColor(button, 0.4, 0.4, 0.4);
	end
end

--]]