--===================================================
--									
--							GHI Backpack Button
--								ghi_buttonUI.lua
--
--	Moveable button for opening of the main GHI bag
--
-- 						(c)2012 The Gryphonheart Team
--								All rights reserved
--===================================================
 local class;
function GHI_ButtonUI()
	if class then
		return class;
	end
	class = GHClass("GHI_Button");

	local loc = GHI_Loc();

	local OnMove, OnEnter, OnClick, OnLeave, PositionChangeCallbackFunc;
	local sideFrames = {};
	local sideButtons = {};


	local squaredButton = CreateFrame("CheckButton", "GHI_ButtonSquared", UIParent, "ItemButtonTemplate");
	squaredButton:Hide();

	squaredButton:SetScript("OnUpdate", function(b) if b.iconDrag then OnMove(b) end end)
	squaredButton:SetScript("OnDragStart", function(b) b.iconDrag = true end)
	squaredButton:SetScript("OnDragStop", function(b) b.iconDrag = false end)
	squaredButton:SetScript("OnEnter", function(b) OnEnter(b) end)
	squaredButton:SetScript("OnLeave", function(b) OnLeave(b) end)
	squaredButton:SetScript("OnClick", function(b) OnClick(b) end)
	squaredButton:RegisterForDrag("LeftButton")
	squaredButton:SetMovable();
	squaredButton:SetFrameStrata("MEDIUM");
	squaredButton:SetFrameLevel(8);
	squaredButton:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight");

	local roundButton = CreateFrame("Button", "GHI_ButtonRound", UIParent)
	roundButton:SetHeight(33);
	roundButton:SetWidth(33);

	local overlay = roundButton:CreateTexture(nil, "OVERLAY");
	overlay:SetWidth(56);
	overlay:SetHeight(56);
	overlay:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder");
	overlay:SetPoint("TOPLEFT", 0, 0);
	roundButton.overlay = overlay;

	local icon = roundButton:CreateTexture(nil, "BACKGROUND");
	icon:SetWidth(18);
	icon:SetHeight(18);
	icon:SetTexture("Interface\\AddOns\\GHI\\Textures\\GH_RoundIcon")
	icon:SetPoint("TOPLEFT", 8, -8);
	icon:SetTexCoord(.075, .925, .075, .925)
	roundButton.icon = icon;

	roundButton:SetScript("PreClick", function() PlaySound("igMainMenuOptionCheckBoxOn") end);

	roundButton:SetFrameStrata("MEDIUM");
	roundButton:SetFrameLevel(8);
	roundButton:Hide();
	roundButton:RegisterForClicks("AnyUp")

	--roundButton:SetScript("OnMouseDown", function(b) b.icon:SetTexCoord(0,1,0,1) end);
	--roundButton:SetScript("OnMouseUp", function(b) b.icon:SetTexCoord(.075,.925,.075,.925) end);
	roundButton:Hide();

	roundButton:SetScript("OnUpdate", function(b) if b.iconDrag then OnMove(b) end end)
	roundButton:SetScript("OnDragStart", function(b) b.iconDrag = true end)
	roundButton:SetScript("OnDragStop", function(b) b.iconDrag = false end)
	roundButton:RegisterForDrag("LeftButton")
	roundButton:SetMovable();
	roundButton:SetScript("OnEnter", function(b) OnEnter(b) end)
	roundButton:SetScript("OnLeave", function(b) OnLeave(b) end)
	roundButton:SetScript("OnClick", function(b) OnClick(b) end)

	OnMove = function(b)
		if IsShiftKeyDown() then
			local _x, _y = GetCursorPosition();
			local s = b:GetEffectiveScale();

			local x, y = _x / s, _y / s;
			roundButton:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y);
			squaredButton:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y);
			PositionChangeCallbackFunc(x, y);
		end
	end

	OnEnter = function(b)
		GameTooltip:SetOwner(b, "ANCHOR_LEFT");
		GameTooltip:ClearLines()
		GameTooltip:AddLine(loc.BAG_BNT, 1.0, 1.0, 1.0);
		GameTooltip:AddLine(loc.BAG_DRAG);

		GameTooltip:Show();

		for _,frame in pairs(sideButtons) do
			frame:Show();
		end
	end

	OnClick = function(b)
		GHI_ToggleBackpack();
	end

	OnLeave = function(b)
		GameTooltip:Hide()
		for _,frame in pairs(sideButtons) do
			if not(frame.frame:IsShown()) then
				frame:Hide();
			end
		end
	end

	local currentButton;
	class.UseSquared = function()
		roundButton:Hide();
		squaredButton:Show();
		currentButton = squaredButton;
	end
	class.UseRound = function()
		squaredButton:Hide();
		roundButton:Show();
		currentButton = roundButton;
	end

	class.ResetPosition = function()
		local x, y = UIParent:GetWidth() / 2, UIParent:GetHeight() / 2;
		roundButton:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y);
		squaredButton:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y);
		PositionChangeCallbackFunc(x, y);
	end

	class.SetPosition = function(x, y)
		roundButton:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y);
		squaredButton:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x, y);
		PositionChangeCallbackFunc(x, y);
	end

	class.SetChangePositionCallbackFunction = function(func)
		PositionChangeCallbackFunc = func;
	end

	class.SetScale = function(scale)
		squaredButton:SetWidth(scale * 37);
		squaredButton:SetHeight(scale * 37);
		_G[squaredButton:GetName() .. "NormalTexture"]:SetHeight(64 * scale);
		_G[squaredButton:GetName() .. "NormalTexture"]:SetWidth(64 * scale);

		roundButton:SetWidth(scale * 33);
		roundButton:SetHeight(scale * 33);
		roundButton.icon:SetWidth(scale * 18);
		roundButton.icon:SetHeight(scale * 18);
		roundButton.overlay:SetWidth(scale * 56);
		roundButton.overlay:SetHeight(scale * 56);
		roundButton.icon:SetPoint("TOPLEFT", scale * 8, scale * (-7));
		local s = (.075 * scale);
		roundButton.texCoor = s;
		roundButton.icon:SetTexCoord(s, 1 - s, s, 1 - s)
	end

	class.SetTexture = function(texture)
		SetItemButtonTexture(squaredButton, texture);
		roundButton.icon:SetTexture(texture);
	end

	local GenerateSideButton = function(placement,parent,frame)
		local btn = CreateFrame("Button",nil,parent);
		btn:SetWidth(18);
		btn:SetHeight(52);

		btn:SetHighlightTexture("Interface/Buttons/UI-Common-MouseHilight","ADD");
		if placement == "LEFT" then
			btn:SetNormalTexture("Interface/BUTTONS/UI-SpellbookIcon-PrevPage-Up");
			btn:SetPushedTexture("Interface/BUTTONS/UI-SpellbookIcon-PrevPage-Down");
			btn:SetPoint("RIGHT",parent,"LEFT",0,0);
		elseif placement == "RIGHT" then
			btn:SetNormalTexture("Interface/BUTTONS/UI-SpellbookIcon-NextPage-Up");
			btn:SetPushedTexture("Interface/BUTTONS/UI-SpellbookIcon-NextPage-Down");
			btn:SetPoint("LEFT",parent,"RIGHT",0,0);
		end
		btn.frame = frame;
		btn:Hide();
		btn:SetScript("OnEnter",function()
			btn:Show();
		end);
		btn:SetScript("OnLeave",function()
			if not(frame:IsShown()) then
				btn:Hide();
			end
		end);
		btn:SetScript("OnClick",function()
			if frame:IsShown() then
				frame:Hide();
			else
				frame:Show();
			end
		end);


		return btn;
	end

	local PLACEMENT = {"LEFT","RIGHT"}
	class.AddSideFrame = function(frame)
		table.insert(sideFrames,frame);
		local num = #(sideFrames);

		if PLACEMENT[num] and not(sideButtons[num]) then
			sideButtons[num] = GenerateSideButton(PLACEMENT[num],currentButton,frame);

			return sideButtons[num];
		end
	end

	class.SetScale(1);
	class.UseSquared();



	return class;
end