--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
--|	GypsyMod HotBar - Gypsy_PerformanceBar.lua
--|	Performance bar functions
--|	Performance bar label functions
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--

--= Libraries =--

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

--= Variables =--

local saved = nil;

-- Latency thresholds
local lowLatency = 200;
local mediumLatency = 500;

-- Size
local frameX = 24;
local frameY = 50;
local barX = 20;
local barY = 66;

--= Performance Bar Functions =--

function Gypsy_PerformanceBar_Initialize (player)
	Gypsy_PerformanceBar_LoadConfig(player);
	
	local args = {};
	args.frame = "Gypsy_PerformanceBar_Capsule";
	if (saved.enabled == 0 or saved.performancebar_shown == 0 or saved.performancebar_docked == 0) then
		args.enabled = 0;
	else
		args.enabled = 1;
	end
	args.locked = saved.performancebar_locked;
	args.orient = "VERT";
	args.initFunc = "Gypsy_PerformanceBar_Update";
	args.ordFunc = "Gypsy_PerformanceBar_Update";
	args.orientFunc = "Gypsy_PerformanceBar_Update";
	args.menuFunc = "Gypsy_PerformanceBar_DropDown_Toggle";
	args.title = HOTBAR_TEXT:Get("performancebar");
	args.tooltipTitle = HOTBAR_TEXT:Get("performancebar");
	args.tooltipText = HOTBAR_TEXT:Get("capsuleTooltip");
	args.ratio = 0.5;
	args.width = 24;
	args.height = 50;
	capsule:Register(args);
	
	UIDropDownMenu_Initialize(Gypsy_PerformanceBar_DropDown, Gypsy_PerformanceBar_UpdateDropDown, "MENU");
end

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

function Gypsy_PerformanceBar_Update ()
	if (saved.enabled == 1 and saved.performancebar_shown == 1) then
		Gypsy_PerformanceBarFrame:Show();
		Gypsy_PerformanceBarLabel:Show();
		
		local orient;
		
		if (saved.performancebar_docked == 1) then
			MiniMapMailFrame:ClearAllPoints();
			MiniMapMailFrame:SetPoint("TOPLEFT", "Minimap", "TOPLEFT", -24, -32);
			if (MiniMapMeetingStoneFrame) then
				MiniMapMeetingStoneFrame:ClearAllPoints();
				MiniMapMeetingStoneFrame:SetPoint("TOP", "MiniMapMailFrame", "BOTTOM", -3, 5);
			end
			
			Gypsy_PerformanceBarHolder:Show();
			Gypsy_PerformanceBarFrame:SetBackdropColor(0.25, 0.25, 0.25, 0);
			Gypsy_PerformanceBarFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 0);
			
			orient = "VERT";
			
			capsule:Disable(Gypsy_PerformanceBar_Capsule);
			
			Gypsy_PerformanceBarFrame:ClearAllPoints();
			Gypsy_PerformanceBarFrame:SetPoint("LEFT", "MinimapBackdrop", "RIGHT", -22, 25);
		else
			MiniMapMailFrame:ClearAllPoints();
			MiniMapMailFrame:SetPoint("TOPRIGHT", "Minimap", "TOPRIGHT", 21, -38);
			if (MiniMapMeetingStoneFrame) then
				MiniMapMeetingStoneFrame:ClearAllPoints();
				MiniMapMeetingStoneFrame:SetPoint("TOPRIGHT", "Minimap", "TOPRIGHT", 23, -65);
			end
			
			Gypsy_PerformanceBarHolder:Hide();
			Gypsy_PerformanceBarFrame:SetBackdropColor(0.25, 0.25, 0.25, 1.0);
			Gypsy_PerformanceBarFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1.0);	
			
			orient = saved.performancebar_orient;
			
			capsule:Enable(Gypsy_PerformanceBar_Capsule);
			
			Gypsy_PerformanceBarFrame:ClearAllPoints();
			Gypsy_PerformanceBarFrame:SetPoint("BOTTOMLEFT", "Gypsy_PerformanceBar_Capsule", "BOTTOMLEFT", capsule.padding/2, capsule.padding/2);
			
			if (not Gypsy_PerformanceBar_Capsule:IsUserPlaced()) then
				Gypsy_PerformanceBar_Capsule:ClearAllPoints();
				Gypsy_PerformanceBar_Capsule:SetPoint("TOP", "UIParent", "TOP");
			end
		end
		
		if (orient == "VERT") then
			Gypsy_PerformanceBarFrame:SetWidth(frameX);
			Gypsy_PerformanceBarFrame:SetHeight(frameY);
			Gypsy_PerformanceBar:SetWidth(barX);
			Gypsy_PerformanceBar:SetHeight(barY);
			Gypsy_PerformanceBar:SetPoint("LEFT", "Gypsy_PerformanceBarFrame", "LEFT", 2, 0);
			Gypsy_PerformanceBarLabel:SetWidth(barX);
			Gypsy_PerformanceBarLabel:SetHeight(barY);
		else
			Gypsy_PerformanceBarFrame:SetWidth(frameY);
			Gypsy_PerformanceBarFrame:SetHeight(frameX);
			Gypsy_PerformanceBar:SetWidth(barY + 24);
			Gypsy_PerformanceBar:SetHeight(barX);
			Gypsy_PerformanceBar:SetPoint("LEFT", "Gypsy_PerformanceBarFrame", "LEFT", -17, 0);
			Gypsy_PerformanceBarLabel:SetWidth(barY);
			Gypsy_PerformanceBarLabel:SetHeight(barX);
		end
			
		if (Gypsy_PerformanceBar_Capsule.initialized and saved.performancebar_docked == 0 and Gypsy_PerformanceBar_Capsule.enabled == 1) then
			if (orient == "VERT") then
				Gypsy_PerformanceBar_Capsule.width = frameX;
				Gypsy_PerformanceBar_Capsule.height = frameY;
			else
				Gypsy_PerformanceBar_Capsule.width = frameY;
				Gypsy_PerformanceBar_Capsule.height = frameX;
			end
			capsule:Size(Gypsy_PerformanceBar_Capsule);
			capsule:UpdateOrientation(Gypsy_PerformanceBar_Capsule, orient);
		end
	else
		MiniMapMailFrame:ClearAllPoints();
		MiniMapMailFrame:SetPoint("TOPRIGHT", "Minimap", "TOPRIGHT", 21, -38);
		if (MiniMapMeetingStoneFrame) then
			MiniMapMeetingStoneFrame:ClearAllPoints();
			MiniMapMeetingStoneFrame:SetPoint("TOPRIGHT", "Minimap", "TOPRIGHT", 23, -65);
		end
		
		Gypsy_PerformanceBarFrame:Hide();
		Gypsy_PerformanceBarLabel:Hide();
		capsule:Disable(Gypsy_PerformanceBar_Capsule);
	end
end

function Gypsy_PerformanceBar_Reset ()
	Gypsy_PerformanceBar_Capsule:SetUserPlaced(0);
end

--= DropDown Functions =--

function Gypsy_PerformanceBar_DropDown_OnShow ()
	UIDropDownMenu_Initialize(this, Gypsy_PerformanceBar_UpdateDropDown, "MENU");
end

function Gypsy_PerformanceBar_DropDown_Toggle ()
	if (saved.performancebar_docked == 1) then
		ToggleDropDownMenu(1, nil, Gypsy_PerformanceBar_DropDown, "Gypsy_PerformanceBarFrame", 0, 0);
		DropDownList1:ClearAllPoints();
		DropDownList1:SetPoint("TOPRIGHT", "Gypsy_PerformanceBarFrame", "BOTTOMLEFT", 0, 0);
	else
		hotlib:ToggleDropDown("Gypsy_PerformanceBar");
	end
end

function Gypsy_PerformanceBar_UpdateDropDown ()
	local info = {};
	
	if (UIDROPDOWNMENU_MENU_LEVEL == 2) then
		info.text = HOTBAR_TEXT:Get("horizontal");
		if (saved.performancebar_orient == "HORI") then
			info.checked = 1;
		end
		info.func = function ()
			if (not this.checked) then
				saved.performancebar_orient = "HORI";
				Gypsy_PerformanceBar_Update();
			end
		end
		UIDropDownMenu_AddButton(info, 2);

		info = {};
		info.text = HOTBAR_TEXT:Get("vertical");
		if (saved.performancebar_orient == "VERT") then
			info.checked = 1;
		end
		info.func = function ()
			if (not this.checked) then
				saved.performancebar_orient = "VERT";
				Gypsy_PerformanceBar_Update();
			end
		end
		UIDropDownMenu_AddButton(info, 2);
		
		return;
	end

	info.text = HOTBAR_TEXT:Get("performancebar");
	info.isTitle = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	info = {};
	info.text = HOTBAR_TEXT:Get("show");
	if (saved.performancebar_shown == 1) then
		info.checked = 1;
	end
	info.func = function ()
		if (this.checked) then
			saved.performancebar_shown = 0;
		else
			saved.performancebar_shown = 1;
		end
		Gypsy_PerformanceBar_Update();
	end
	UIDropDownMenu_AddButton(info);
	info = {};
	info.text = HOTBAR_TEXT:Get("docked");
	if (saved.performancebar_docked == 1) then
		info.checked = 1;
	end
	info.func = function ()
		if (this.checked) then
			saved.performancebar_docked = 0;
		else
			saved.performancebar_docked = 1;
		end
		Gypsy_PerformanceBar_Update();
	end
	if (saved.performancebar_shown == 0) then
		info.disabled = 1;
	end
	UIDropDownMenu_AddButton(info);
	info = {};
	info.text = HOTBAR_TEXT:Get("orientation");
	info.hasArrow = 1;
	info.value = "performancebar_orientation";
	if (saved.performancebar_docked == 1 or saved.performancebar_shown == 0) then
		info.disabled = 1;
	end
	UIDropDownMenu_AddButton(info);
end

--= Performance Bar Frame Functions =--

local function setLatencyTooltip ()
	local _, _, latency = GetNetStats();
	utility:PositionTooltip(Gypsy_PerformanceBarFrame);
	GameTooltip:AddLine(MAINMENUBAR_LATENCY_LABEL.." "..color.white.code..latency..color.close..MILLISECONDS_ABBR);
	
	if (saved.performancebar_docked == 1) then
		GameTooltip:AddLine(color.white.code..HOTBAR_TEXT:Get("performanceTooltip")..color.close);
	end
	
	GameTooltip:Show();
end

function Gypsy_PerformanceBarFrame_OnLoad ()
	this:RegisterEvent("VARIABLES_LOADED");
	this.hover = nil;
	this.updateInterval = 0;
end

function Gypsy_PerformanceBarFrame_OnUpdate (elapsed)
	if (this.updateInterval > 0) then
		this.updateInterval = this.updateInterval - arg1;
	else
		this.updateInterval = PERFORMANCEBAR_UPDATE_INTERVAL;
		
		local bandwidthIn, bandwidthOut, latency = GetNetStats();
		
		if (latency > mediumLatency) then
			Gypsy_PerformanceBar:SetVertexColor(color:Parse("red"));
		elseif (latency > lowLatency) then
			Gypsy_PerformanceBar:SetVertexColor(color:Parse("yellow"));
		else
			Gypsy_PerformanceBar:SetVertexColor(color:Parse("green"));
		end
		
		if (this.hover) then
			setLatencyTooltip();
		end
	end
end

--= Performance Bar Label Functions =--

function Gypsy_PerformanceBarLabel_OnLoad ()
	this:RegisterForClicks("RightButtonUp");
end

function Gypsy_PerformanceBarLabel_OnMouseUp (button)
	if (button == "RightButton" and saved.performancebar_docked == 1) then
		Gypsy_PerformanceBar_DropDown_Toggle();
		if (GameTooltip:IsOwned(Gypsy_PerformanceBarFrame)) then
			Gypsy_PerformanceBarFrame.hover = nil;
			GameTooltip:Hide();
		end
	end
end
	
function Gypsy_PerformanceBarLabel_OnEnter ()
	Gypsy_PerformanceBarFrame.hover = 1;
	
	setLatencyTooltip();
end

function Gypsy_PerformanceBarLabel_OnLeave ()
	Gypsy_PerformanceBarFrame.hover = nil;
	
	if (GameTooltip:IsOwned(Gypsy_PerformanceBarFrame)) then
		GameTooltip:Hide();
	end
end