--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
--|	GypsyMod HotBar - Gypsy_MenuBar.lua
--|	MenuBar functions
--|	Menu button functions
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--

--= Libraries =--

local color = Gypsy_Color:New();
local utility = Gypsy_Utility:New();
local capsule = Gypsy_Capsule:New();
local hotlib = Gypsy_HotLib:New();

--= Variables =--

-- Saved variables shortcut table
local saved = nil;

-- Vertical adjustment for blank space at the top of buttons
local vertSpace = 18;

-- Texture path prefix
local pathPrefix = "Interface\\Buttons\\UI-MicroButton-";

-- List of menu buttons
local menuButtons = {};
menuButtons[1] = "Character";
menuButtons[2] = "Spellbook";
menuButtons[3] = "Talent";
menuButtons[4] = "QuestLog";
menuButtons[5] = "Socials";
menuButtons[6] = "WorldMap";
menuButtons[7] = "Menu";
menuButtons[8] = "Help";

-- Docked data
local dockedData = {
	["HORI"] = {
		["point"] = "BOTTOM";
		["relPoint"] = "TOP";
	};
	["VERT"] = {
		["point"] = "LEFT";
		["relPoint"] = "RIGHT";
	};
};

--= Helper Functions =--

-- Returns a scaling ratio used for determining capsule size
local function getRatio ()
	if (saved.setScale == 1) then
		return hotlib:GetScale(saved.size) / UIParent:GetScale();
	else
		return hotlib:GetScale(saved.menubar_buttonSize) / UIParent:GetScale();
	end
end

-- Quick function to adjust the vertical space by scaling ratio
local function getVerticalAdjustment ()
	return vertSpace * getRatio();
end

-- Quick check to determine whether to use the saved size property
local function getButtonSize ()
	if (saved.setScale == 1) then
		return saved.size;
	else
		return saved.menubar_buttonSize;
	end
end

-- Hide all buttons in menuButtons
local function hideAll ()
	for i,v in menuButtons do
		getglobal("Gypsy_"..v.."MenuButton"):Hide();
	end
end

-- Hook function for menu button updating
local function updateMenuButtonsHook ()
	if (CharacterFrame:IsVisible()) then
		Gypsy_CharacterMenuButton:SetButtonState("PUSHED", 1);
		Gypsy_CharacterMenuButton_SetPushed();
	else
		Gypsy_CharacterMenuButton:SetButtonState("NORMAL");
		Gypsy_CharacterMenuButton_SetNormal();
	end
	
	if (SpellBookFrame:IsVisible()) then
		Gypsy_SpellbookMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_SpellbookMenuButton:SetButtonState("NORMAL");
	end

	if (TalentFrame:IsVisible()) then
		Gypsy_TalentMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_TalentMenuButton:SetButtonState("NORMAL");
	end

	if (QuestLogFrame:IsVisible()) then
		Gypsy_QuestLogMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_QuestLogMenuButton:SetButtonState("NORMAL");
	end
	
	if ((GameMenuFrame:IsVisible()) or (OptionsFrame:IsVisible())) then
		Gypsy_MenuMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_MenuMenuButton:SetButtonState("NORMAL");
	end

	if (FriendsFrame:IsVisible()) then
		Gypsy_SocialsMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_SocialsMenuButton:SetButtonState("NORMAL");
	end

	if (WorldMapFrame:IsVisible()) then
		Gypsy_WorldMapMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_WorldMapMenuButton:SetButtonState("NORMAL");
	end

	if (HelpFrame:IsVisible()) then
		Gypsy_HelpMenuButton:SetButtonState("PUSHED", 1);
	else
		Gypsy_HelpMenuButton:SetButtonState("NORMAL");
	end
end

--= MenuBar Functions =--

-- Load config and register capsule
function Gypsy_MenuBar_Initialize (player)
	Gypsy_MenuBar_LoadConfig(player);
	
	local args = {};
	args.frame = "Gypsy_MenuBar_Capsule";
	if (saved.enabled == 0 or saved.menubar_shown == 0) then
		args.enabled = 0;
	else
		args.enabled = 1;
	end
	args.locked = saved.menubar_locked;
	args.doDir = 0;
	args.orient = saved.menubar_orient;
	args.initFunc = "Gypsy_MenuBar_Update";
	args.orientFunc = "Gypsy_MenuBar_Update";
	args.ordFunc = "Gypsy_MenuBar_Update";
	args.menuFunc = "Gypsy_MenuBar_DropDown_Toggle";
	args.title = HOTBAR_TEXT:Get("menubar");
	args.tooltipTitle = HOTBAR_TEXT:Get("menubar");
	args.tooltipText = HOTBAR_TEXT:Get("capsuleTooltip");		
	args.ratio = getRatio();
	args.width = hotlib:GetGroupWidth(args.orient, getn(menuButtons), Gypsy_CharacterMenuButton:GetWidth() * getRatio(), getButtonSize(), 0);
	args.height = hotlib:GetGroupHeight(args.orient, getn(menuButtons), Gypsy_CharacterMenuButton:GetWidth() * getRatio(), getButtonSize(), 0);
	capsule:Register(args);
	
	UIDropDownMenu_Initialize(Gypsy_MenuBar_DropDown, Gypsy_MenuBar_UpdateDropDown, "MENU");
end

function Gypsy_MenuBar_LoadConfig (player)
	saved = GYPSY_SAVED_HOTBAR[player];
end

function Gypsy_MenuBar_ValidateSettings ()

end

function Gypsy_MenuBar_Update ()
	if (saved.enabled == 1 and saved.menubar_shown == 1) then
		capsule:Enable(Gypsy_MenuBar_Capsule);
		utility:Hook("after", "UpdateMenuButtons", updateMenuButtonsHook);
		
		local orient = saved.menubar_orient;
		
		local size = getButtonSize();
		
		if (not Gypsy_MenuBar_Capsule:IsUserPlaced()) then
			local count;
			
			if (saved.actionbar1_shown == 1) then
				count = 1;
			else
				count = 0;
			end
			
			for i=6, 2, -1 do
				if (saved["actionbar"..i.."_shown"] == 1 and saved["actionbar"..i.."_docked"] == 1) then
					count = count + 1;
				end
			end
			
			local y = (count * size) + ((capsule.padding * getRatio()) * (count * 2));
			Gypsy_MenuBar_Capsule:ClearAllPoints();
			Gypsy_MenuBar_Capsule:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, y);
		end
		
		local x = capsule.padding;
		local y = -capsule.padding + vertSpace;
		
		Gypsy_CharacterMenuButton:ClearAllPoints();
		Gypsy_CharacterMenuButton:SetPoint("TOPLEFT", "Gypsy_MenuBar_Capsule", "TOPLEFT", x, y);
					
		hotlib:ShowButton(Gypsy_CharacterMenuButton);
		
		for i=2, getn(menuButtons) do			
			hotlib:ShowButton(getglobal("Gypsy_"..menuButtons[i].."MenuButton"), orient, 0, "Gypsy_"..menuButtons[i-1].."MenuButton");
			if (orient == "VERT") then
				local button = getglobal("Gypsy_"..menuButtons[i].."MenuButton");
				button:ClearAllPoints();
				button:SetPoint("TOPLEFT", "Gypsy_"..menuButtons[i-1].."MenuButton", "BOTTOMLEFT", 0, vertSpace);
			end
		end
		
		Gypsy_MenuBar_Scale();
		
		if (Gypsy_MenuBar_Capsule.initialized and Gypsy_MenuBar_Capsule.enabled == 1) then
			Gypsy_MenuBar_Capsule.ratio = getRatio();
			Gypsy_MenuBar_Capsule.width = hotlib:GetGroupWidth(orient, getn(menuButtons), Gypsy_CharacterMenuButton:GetWidth() * getRatio(), size, 0);
			Gypsy_MenuBar_Capsule.height = hotlib:GetGroupHeight(orient, getn(menuButtons), Gypsy_CharacterMenuButton:GetWidth() * getRatio(), size, 0);
			capsule:Size(Gypsy_MenuBar_Capsule);
			if (Gypsy_MenuBar_Capsule.orient ~= orient) then
				capsule:UpdateOrientation(Gypsy_MenuBar_Capsule, orient);
			end
		end			
	else
		utility:UnHook("UpdateMenuButtons");		
		hideAll();
		capsule:Disable(Gypsy_MenuBar_Capsule);
	end
end

function Gypsy_MenuBar_Scale ()
	local size = getButtonSize();
	
	for i=1, getn(menuButtons) do
		hotlib:ScaleButton(getglobal("Gypsy_"..menuButtons[i].."MenuButton"), size);
	end
end

function Gypsy_MenuBar_Reset ()
	Gypsy_MenuBar_Capsule:SetUserPlaced(0);
end

--= DropDown Functions =--

function Gypsy_MenuBar_DropDown_OnShow ()
	UIDropDownMenu_Initialize(this, Gypsy_MenuBar_UpdateDropDown, "MENU");
end

function Gypsy_MenuBar_DropDown_Toggle ()
	hotlib:ToggleDropDown("Gypsy_MenuBar");
end

function Gypsy_MenuBar_LoadMenu (level)
	local info = {};
	info.text = HOTBAR_TEXT:Get("show");
	if (saved.menubar_shown == 1) then
		info.checked = 1;
	end
	info.func = function ()
		if (this.checked) then
			saved.menubar_shown = 0;
		else
			saved.menubar_shown = 1;
		end
		Gypsy_MenuBar_Update();
	end
	UIDropDownMenu_AddButton(info, level);
	info = {};
	info.text = HOTBAR_TEXT:Get("orientation");
	info.hasArrow = 1;
	info.notCheckable = 1;
	info.value = "menubar_orientation";
	if (saved.menubar_shown == 0) then
		info.disabled = 1;
	end
	UIDropDownMenu_AddButton(info, level);
	info = {};
	info.text = HOTBAR_TEXT:Get("buttonSize");
	info.hasArrow = 1;
	info.notCheckable = 1;
	info.value = "menubar_buttonsize";
	if (saved.menubar_shown == 0) then
		info.disabled = 1;
	end
	UIDropDownMenu_AddButton(info, level);
end

function Gypsy_MenuBar_UpdateDropDown ()
	local info = {};
	
	if (UIDROPDOWNMENU_MENU_LEVEL == 2) then
		if (strfind(UIDROPDOWNMENU_MENU_VALUE, "orientation")) then
			info = {};
			info.text = HOTBAR_TEXT:Get("horizontal");
			if (saved.menubar_orient == "HORI") then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_orient = "HORI";
					capsule:UpdateOrientation(Gypsy_MenuBar_Capsule, "HORI");
				end
			end
			UIDropDownMenu_AddButton(info, 2);
			info = {};
			info.text = HOTBAR_TEXT:Get("vertical");
			if (saved.menubar_orient == "VERT") then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_orient = "VERT";
					capsule:UpdateOrientation(Gypsy_MenuBar_Capsule, "VERT");
				end
			end
			UIDropDownMenu_AddButton(info, 2);
		else
			info = {};
			info.text = "18"..HOTBAR_TEXT:Get("px");
			if (saved.menubar_buttonSize == 18) then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_buttonSize = 18;
					Gypsy_MenuBar_Update();
				end
			end
			UIDropDownMenu_AddButton(info, 2);
			info = {};
			info.text = "24"..HOTBAR_TEXT:Get("px");
			if (saved.menubar_buttonSize == 24) then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_buttonSize = 24;
					Gypsy_MenuBar_Update();
				end
			end
			UIDropDownMenu_AddButton(info, 2);
			info = {};
			info.text = "30"..HOTBAR_TEXT:Get("px");
			if (saved.menubar_buttonSize == 30) then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_buttonSize = 30;
					Gypsy_MenuBar_Update();
				end
			end
			UIDropDownMenu_AddButton(info, 2);
			info = {};
			info.text = "36"..HOTBAR_TEXT:Get("px");
			if (saved.menubar_buttonSize == 36) then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_buttonSize = 36;
					Gypsy_MenuBar_Update();
				end
			end
			UIDropDownMenu_AddButton(info, 2);
			info = {};
			info.text = "42"..HOTBAR_TEXT:Get("px");
			if (saved.menubar_buttonSize == 42) then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_buttonSize = 42;
					Gypsy_MenuBar_Update();
				end				
			end
			UIDropDownMenu_AddButton(info, 2);
			info = {};
			info.text = "48"..HOTBAR_TEXT:Get("px");
			if (saved.menubar_buttonSize == 48) then
				info.checked = 1;
			end
			info.func = function ()
				if (not this.checked) then
					saved.menubar_buttonSize = 48;
					Gypsy_MenuBar_Update();					
				end
			end
			UIDropDownMenu_AddButton(info, 2);	
		end
		
		return;
	end
	
	info = {};
	info.text = HOTBAR_TEXT:Get("menubar");
	info.isTitle = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	Gypsy_MenuBar_LoadMenu();
end

--= MenuButton Functions =--

function Gypsy_MenuButton_Load (name)
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	this:RegisterEvent("UPDATE_BINDINGS");
	
	this:SetNormalTexture(pathPrefix..name.."-Up");
	this:SetPushedTexture(pathPrefix..name.."-Down");
	this:SetDisabledTexture(pathPrefix..name.."-Disabled");
	this:SetHighlightTexture("Interface\\Buttons\\UI-MicroButton-Hilight");
end

function Gypsy_MenuButton_GetTooltipText(text, action)
	if (GetBindingKey(action)) then
		return text.." "..color.normal.code.."("..GetBindingKey(action)..")"..color.close;
	else
		return text;
	end
	
end

function Gypsy_MenuButton_OnEnter ()
	GameTooltip_AddNewbieTip(this.tooltipText, 1.0, 1.0, 1.0, this.newbieText);
end

function Gypsy_MenuButton_OnLeave ()
	if (GameTooltip:IsOwned(this)) then
		GameTooltip:Hide();
	end
end

--= Character Button Functions =--

function Gypsy_CharacterMenuButton_OnLoad ()
	SetPortraitTexture(Gypsy_CharacterMenuButtonPortrait, "player");
	
	this:SetNormalTexture("Interface\\Buttons\\UI-MicroButtonCharacter-Up");
	this:SetPushedTexture("Interface\\Buttons\\UI-MicroButtonCharacter-Down");
	this:SetHighlightTexture("Interface\\Buttons\\UI-MicroButton-Hilight");
	
	this:RegisterEvent("UNIT_PORTRAIT_UPDATE");
	this:RegisterEvent("UPDATE_BINDINGS");
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(CHARACTER_BUTTON), "TOGGLECHARACTER0");
	this.newbieText = NEWBIE_TOOLTIP_CHARACTER;
end

function Gypsy_CharacterMenuButton_OnClick ()
	ToggleCharacter("PaperDollFrame");
end

function Gypsy_CharacterMenuButton_OnEvent (event)
	if (event == "UNIT_PORTRAIT_UPDATE" and arg1 == "player") then
		SetPortraitTexture(Gypsy_CharacterMenuButtonPortrait, arg1);		
		return;
	end
	if (event == "UPDATE_BINDINGS") then
		this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(CHARACTER_BUTTON), "TOGGLECHARACTER0");
		return;
	end
end

function Gypsy_CharacterMenuButton_OnUpdate ()
	if (this:GetButtonState() == "PUSHED") then
		Gypsy_CharacterMenuButton_SetPushed();
	else
		Gypsy_CharacterMenuButton_SetNormal();
	end
end

function Gypsy_CharacterMenuButton_SetPushed()
	Gypsy_CharacterMenuButtonPortrait:SetTexCoord(0.2666, 0.8666, 0, 0.8333);
	Gypsy_CharacterMenuButtonPortrait:SetAlpha(0.5);
end

function Gypsy_CharacterMenuButton_SetNormal()
	Gypsy_CharacterMenuButtonPortrait:SetTexCoord(0.2, 0.8, 0.0666, 0.9);
	Gypsy_CharacterMenuButtonPortrait:SetAlpha(1.0);
end

--= Spellbook Button Functions =--

function Gypsy_SpellbookMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("Spellbook");
end

function Gypsy_SpellbookMenuButton_OnClick ()
	ToggleSpellBook(BOOKTYPE_SPELL);
end

function Gypsy_SpellbookMenuButton_OnEnter ()
	if (PlayerHasSpells()) then
		this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(SPELLBOOK_ABILITIES_BUTTON), "TOGGLESPELLBOOK");
	else
		this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(ABILITYBOOK_BUTTON), "TOGGLEABILITYBOOK");
	end
	GameTooltip_AddNewbieTip(this.tooltipText, 1.0, 1.0, 1.0, NEWBIE_TOOLTIP_SPELLBOOK);
end

--= Talent Button Functions =--

function Gypsy_TalentMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("Talents");
	
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(TALENTS_BUTTON), "TOGGLETALENTS");
	this.newbieText = NEWBIE_TOOLTIP_TALENTS;
	
	this:RegisterEvent("PLAYER_LEVEL_UP");
	this:RegisterEvent("UPDATE_BINDINGS");
	this:RegisterEvent("UNIT_LEVEL");
end

function Gypsy_TalentMenuButton_OnClick ()
	ToggleTalentFrame();
end

function Gypsy_TalentMenuButton_OnEvent (event)
	if (event == "PLAYER_LEVEL_UP") then
		Gypsy_TalentMenuButton_Update();
		if (not CharacterFrame:IsVisible() and this:IsEnabled() ~= 1) then
			SetButtonPulse(this, 60, 1);
		end
		return;
	end
	if (event == "UNIT_LEVEL") then
		Gypsy_TalentMenuButton_Update();
		return;
	end
	if (event == "UPDATE_BINDINGS") then
		this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(TALENTS_BUTTON), "TOGGLETALENTS");
		return;
	end
end

function Gypsy_TalentMenuButton_Update ()
	if (saved and saved.enabled == 1) then
		if (UnitLevel("player") < 10) then
			Gypsy_TalentMenuButton:Disable();
		else
			Gypsy_TalentMenuButton:Enable();
		end
	else
		Gypsy_TalentMenuButton:Disable();
	end
end

--= Quest Log Button Functions =--

function Gypsy_QuestLogMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("Quest");
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(QUESTLOG_BUTTON), "TOGGLEQUESTLOG");
	this.newbieText = NEWBIE_TOOLTIP_QUESTLOG;
end

function Gypsy_QuestLogMenuButton_OnEvent (event)
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(QUESTLOG_BUTTON), "TOGGLEQUESTLOG");
end

function Gypsy_QuestLogMenuButton_OnClick ()
	ToggleQuestLog();
end

--= Socials Button Functions =--

function Gypsy_SocialsMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("Socials");
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(QUESTLOG_BUTTON), "TOGGLESOCIAL");
	this.newbieText = NEWBIE_TOOLTIP_SOCIAL;
end

function Gypsy_SocialsMenuButton_OnEvent (event)
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(SOCIAL_BUTTON), "TOGGLESOCIAL");
end

function Gypsy_SocialsMenuButton_OnClick ()
	ToggleFriendsFrame();
end

--= World Map Button Functions =--

function Gypsy_WorldMapMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("World");
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(WORLDMAP_BUTTON), "TOGGLEWORLDMAP");
end

function Gypsy_WorldMapMenuButton_OnEvent (event)
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(WORLDMAP_BUTTON), "TOGGLEWORLDMAP");
	this.newbieText = NEWBIE_TOOLTIP_WORLDMAP;
end

function Gypsy_WorldMapMenuButton_OnClick ()
	ToggleWorldMap();
end

--= Main Menu Button Functions =--

function Gypsy_MenuMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("MainMenu");
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(MAINMENU_BUTTON), "TOGGLEGAMEMENU");
	this.newbieText = NEWBIE_TOOLTIP_MAINMENU;
end

function Gypsy_MenuMenuButton_OnEvent (event)
	this.tooltipText = Gypsy_MenuButton_GetTooltipText(TEXT(MAINMENU_BUTTON), "TOGGLEGAMEMENU");
end

function Gypsy_MenuMenuButton_OnClick ()
	ToggleGameMenu(1);
end

--= Help Button Functions =--

function Gypsy_HelpMenuButton_OnLoad ()
	Gypsy_MenuButton_Load("Help");
	this.tooltipText = TEXT(HELP_BUTTON);
	this.newbieText = NEWBIE_TOOLTIP_HELP;
end

function Gypsy_HelpMenuButton_OnClick ()
	ToggleHelpFrame();
end