local E, L, V, P, G = unpack(ElvUI);
local mPlugin = "mMediaTag"
local mMT = E:GetModule(mPlugin);
local DT = E:GetModule("DataTexts");
local addon, ns = ...

--Lua functions
local format = format

--WoW API / Variables
local _G = _G
local GetFramerate = GetFramerate

--Variables
local mText = format("Dock %s", MAINMENU_BUTTON)
local mTextName = "mMainMenu"
local statusColors = {
	'|cff0CD809',
	'|cffE8DA0F',
	'|cffFF9000',
	'|cffD80909'
}

local function mDockCheckFrame()
	return ( GameMenuFrame and GameMenuFrame:IsShown() )
end

function mMT:CheckFrameMainMenu(self)
	mMT:DockTimerCheck(self, mDockCheckFrame())
end

local function OnEnter(self)
	local _, _, _, _, other, titel, tip = mMT:mColorDatatext()
	mMT:mOnEnter(self, MAINMENU_BUTTON)
	
	if E.db[mPlugin].mDock.tip.mode == "extend" then
		DT.tooltip:ClearLines()
		
		local framerate = GetFramerate()
		local _, _, latencyHome, latencyWorld = GetNetStats()
		local fps = framerate >= 30 and 1 or (framerate >= 20 and framerate < 30) and 2 or (framerate >= 10 and framerate < 20) and 3 or 4
		local pingHome = latencyHome < 150 and 1 or (latencyHome >= 150 and latencyHome < 300) and 2 or (latencyHome >= 300 and latencyHome < 500) and 3 or 4
		local pingWorld = latencyWorld < 150 and 1 or (latencyWorld >= 150 and latencyWorld < 300) and 2 or (latencyWorld >= 300 and latencyWorld < 500) and 3 or 4
		
		local bandwidthIn, bandwidthOut, latencyHome, latencyWorld = GetNetStats()
		DT.tooltip:AddDoubleLine(format("%s%s|r", L["FPS:"], titel), format("%s%d|r %sFPS", statusColors[fps], framerate, other))
		DT.tooltip:AddDoubleLine(format("%s%s|r", L["Home Latency:"], titel), format("%s%d|r %sms", statusColors[pingHome], latencyHome, other))
		DT.tooltip:AddDoubleLine(format("%s%s|r", L["World Latency:"], titel), format("%s%d|r %sms", statusColors[pingWorld], latencyWorld, other))
		DT.tooltip:AddLine(" ")
		DT.tooltip:AddDoubleLine(ns.mName, format("%sVer.|r %s%s|r" , titel, other, ns.mVersion))
		DT.tooltip:AddLine(" ")
		DT.tooltip:AddLine(format("%s %s%s|r", ns.LeftButtonIcon, tip, L["Click left to open the main menu."]))
		DT.tooltip:AddLine(format("%s %s%s|r", ns.RightButtonIcon, tip, L["Right click to open the ElvUI settings."]))
		
		DT.tooltip:Show()
	end
end

local function OnEvent(self, event, ...)
	mMT:mOnEvent(self, nil, nil, nil, mDockCheckFrame(), E.db[mPlugin].mDock.textures.mainmenu.path, "CheckFrameMainMenu")
end

local function OnLeave(self)
	mMT:mOnLeave(self, mDockCheckFrame())
end

local function OnClick(self, button)
	if mMT:mCheckCombat() then
		if button == "LeftButton" then
            mMT:mClick(self, "CheckFrameMainMenu")
			if ( not GameMenuFrame:IsShown() ) then
				if ( VideoOptionsFrame:IsShown() ) then
					VideoOptionsFrameCancel:Click();
				elseif ( AudioOptionsFrame:IsShown() ) then
					AudioOptionsFrameCancel:Click();
				elseif ( InterfaceOptionsFrame:IsShown() ) then
					InterfaceOptionsFrameCancel:Click();
				end
				CloseMenus();
				CloseAllWindows()
				ShowUIPanel(GameMenuFrame);
			else
				HideUIPanel(GameMenuFrame);
			end
		else
			E:ToggleOptionsUI()
		end
	end
end

DT:RegisterDatatext(mTextName, nil, nil, OnEvent, nil, OnClick, OnEnter, OnLeave, mText, nil)