local addon, ns = ...
local C = ns.shared.color
local L = ns.L
local GetNetStats = GetNetStats
local latency_update_interval = 5
local suffix = "ms"
local addonLoaded = false
local latency = {
	Home = 0, 
	World = 0,
}

local latency_init = function(self)
	if Broker_EverythingDB[L["Latency"]] == nil then
		Broker_EverythingDB[L["Latency"]] = {
			enabled = true,
			showHome = true,
			showWorld = true,
		}
	elseif Broker_EverythingDB[L["Latency"]].showWorld == nil then
			Broker_EverythingDB[L["Latency"]].showWorld = true	
	
	elseif Broker_EverythingDB[L["Latency"]].showHome == nil then
			Broker_EverythingDB[L["Latency"]].showHome = true	
	end
end

local latency_onupdate = function(self)
		local _, _, lHome, lWorld = GetNetStats()
		local text = ""
		local _, playerClass = UnitClass("player")
		local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Latency"])
		
		if Broker_EverythingDB.suffixColour  == true then
			suffix = C.class(suffix, playerClass)
		else
			suffix = C.white(suffix)
		end

		latency.Home = lHome
		latency.World = lWorld
		
		 -- Colour the latencies
		for k, v in pairs(latency) do
			if v <= 250 then 
				latency[k] = C.dkgreen(v)
			elseif v > 250 and v <= 400 then
				latency[k] = C.dkyellow(v)
			elseif v > 400 then
				latency[k] = C.red(v)
			end		
		end

		local showHome, showWorld  = Broker_EverythingDB[L["Latency"]].showHome, Broker_EverythingDB[L["Latency"]].showWorld
		if (showWorld and not showHome) or not (showWorld and showHome) then
			text = string.format("%s%s", latency.World, suffix)
		elseif showHome and not showWorld then
			text = string.format("%s%s", latency.Home, suffix)
		elseif showHome and showWorld then
			text = string.format("%s%s%s %s%s%s", C.white("H:"), latency.Home, suffix, C.white("W:"), latency.World, suffix)
		end

--[=[		if Broker_EverythingDB[L["Latency"]].showHome  == true then
			text = string.format("%s%s %s", C.white("H:"), latency.Home, suffix)
		end
		
		if Broker_EverythingDB[L["Latency"]].showWorld  == true then
			text = string.format("%s %s%s %s", text, C.white("W:"), latency.World, suffix)
		end
]=]
		dataobj.text = text
		
		suffix = "ms"
end 

local latency_onclick = function(self, btn)
	if btn == "RightButton" then
		if IsControlKeyDown() then
			if Broker_EverythingDB[L["Latency"]].showWorld then
				Broker_EverythingDB[L["Latency"]].showWorld = false
			else
				Broker_EverythingDB[L["Latency"]].showWorld = true
			end
			latency_onupdate(self)
			
		elseif IsAltKeyDown() then
			if Broker_EverythingDB[L["Latency"]].showHome then
				Broker_EverythingDB[L["Latency"]].showHome = false
			else
				Broker_EverythingDB[L["Latency"]].showHome = true
			end		
			latency_onupdate(self)			
		else
			if Broker_EverythingDB.suffixColour then
				Broker_EverythingDB.suffixColour = false
			else
				Broker_EverythingDB.suffixColour = true
			end
			suffix = "ms"
			latency_onupdate(self)
		end
	end
end

local latency_tooltip = function(t)
	t:AddLine(L["Latency"])
	t:AddLine(" ")
	t:AddDoubleLine(C.white(L["Home"] .. " :"), latency.Home .. suffix)
	t:AddDoubleLine(C.white(L["World"] .. " :"), latency.World .. suffix)
	t:AddLine(" ")
	t:AddLine(C.dkgreen(string.format("%s", L["Hint: Right-click to toggle class coloring of the suffix."])))
	t:AddLine(C.dkgreen(string.format("%s", L["Hint: Alt + Right-click to toggle showing Home Latency."])))
	t:AddLine(C.dkgreen(string.format("%s", L["Hint: Cntrl + Right-click to toggle showing World Latency."])))
end

Broker_Everything:Register(L["Latency"],
	"Interface\\Addons\\Broker_Everything\\media\\latency", 
	latency_init,
	0,
	0,
	latency_onclick,
	latency_tooltip,
	latency_onupdate,
	latency_update_interval)

