local addon, ns = ...
local C = ns.shared.color
local func = ns.shared
local L = ns.L

local tooltip = nil
local nameplate_events = {
	"VARIABLES_LOADED"
}
local nameplateStatus={
		[L["Friendly Players"]] = "UnitNameFriendlyPlayerName", 
		[L["Friendly Pets"]] = "UnitNameFriendlyPetName",
		[L["Friendly Creations"]] = "UnitNameFriendlyCreationName",
		[L["Friendly Everyone"]] = "nameplateShowFriends",
		[L["Hostile Players"]] = "UnitNameEnemyPlayerName",
		[L["Hostile Pets"]] = "UnitNameEnemyPetName",
		[L["Hostile Creations"]] = "UnitNameEnemyCreationName",
		[L["Hostile Everyone"]] = "nameplateShowEnemies",
		[L["Companion Names"]] = "UnitNameCompanionName",
		[L["Your Own Name"]] = "UnitNameOwn",
		[L["Player Titles"]] = "UnitNamePlayerPVPTitle",
		[L["Guild Names"]]	 = 	"UnitNamePlayerGuild",
		[L["NPC Names"]]	=	"UnitNameNPC"
}

local function updateLDB()
	local dataobj = LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Nameplates"])
	local allFriends, friends = GetCVar("nameplateShowFriends"), GetCVar("UnitNameFriendlyPlayerName")
	local allEnemies, enemy = GetCVar("nameplateShowEnemies"), GetCVar("UnitNameEnemyPlayerName")

	if (friends == "1" or allFriends == "1") and (enemy == "1" or allEnemies == "1") then
		dataobj.text = L["Friend"] .. " & " .. L["Enemy"]
	elseif (friends == "1" or allFriends == "1") then
		dataobj.text = L["Friend"]
	elseif (enemy == "1" or allEnemies == "1") then
		dataobj.text = L["Enemy"]
	else
		dataobj.text = L["None"]
	end
end

local function nameplate_onevent ()
	updateLDB()
end

-- Function to get the Nameplate CVar status
local function getCVarSettings(cVarName)
		local shown
		local toggle
		if GetCVar(cVarName) == "1" then
			shown = C.white(L["Shown"])
			toggle = 0
		elseif GetCVar(cVarName) == nil then
			RegisterCVar(cVarName, 0)
			shown = C.grey(L["Hidden"])
		else
			shown = C.grey(L["Hidden"])
			toggle = 1
		end
		return shown, toggle
end

-- Function to draw the guild tooltip
local function drawNameplateTip()
	local line, column = tooltip:AddHeader(C.dkyellow(L["Nameplates"]))
	
	line, column = tooltip:AddLine(" ")
		
	line, column = tooltip:AddLine(C.ltyellow(L["Nameplates"]), C.ltyellow(L["Current Status"]))
	tooltip:AddSeparator()

	for key, value in func.pairsByKeys(nameplateStatus) do
		local shown, toggle = getCVarSettings(value)
		line, column = tooltip:AddLine(L[key] .. ":", shown)
		tooltip:SetLineScript(line, "OnMouseUp", function(self) 
			SetCVar(value, toggle, true) 
			tooltip:Clear()
			drawNameplateTip()
		end)			
		tooltip:SetLineScript(line, "OnEnter", function(self) tooltip:SetLineColor(line, 1,192/255, 90/255, 0.3) end )
		tooltip:SetLineScript(line, "OnLeave", function(self) tooltip:SetLineColor(line, 0,0,0,0) end)
	end
	tooltip:AddLine(" ")

	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Click on an option to toggle the Nameplates on or off."]), nil, nil, 2)

	line, column = nil, nil

end



local nameplate_init = function(obj)
	if not obj then return end

	obj = obj.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Nameplates"])
	

	
	obj.OnLeave = function()
		if tooltip then 
			if MouseIsOver(tooltip) then
					tooltip:SetScript('OnLeave', func.hideTooltip)
			else
					func.hideTooltip(tooltip)
			end
		end
	end

	obj.OnEnter = function(self)
		tooltip = LibQTip:Acquire("NameplateTT", 2, "LEFT", "RIGHT") 
		drawNameplateTip()
		func.createTooltip(self,tooltip)
	end
end




hooksecurefunc("SetCVar", function(cvar, value)
	if Broker_EverythingDB[L["Nameplates"]].enabled ~= nil or Broker_EverythingDB[L["Nameplates"]].enabled == true then
		return
	end

	for k, v in pairs(nameplateStatus) do
		if cvar == v then
			updateLDB(self)
			break
		end
	end
    
end)


Broker_Everything:Register(L["Nameplates"],
	"Interface\\Addons\\Broker_Everything\\media\\nameplates",
	nameplate_init,
	nameplate_onevent,
	nameplate_events,
	0,
	0, --nameplate_tooltip,
	nil,
	nil)
	
