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

local tooltip
local last_click = 0

StaticPopupDialogs["CONFIRM"] = {
	text = L["Are you sure you want to Reload the UI?"],
	button1 = ACCEPT,
	button2 = CANCEL,
	OnAccept = function()
		ReloadUI()
	end,
	timeout = 20,
	whileDead = true,
	hideOnEscape = true,
	preferredIndex = 5,
}

local function drawStuffTip()
	local line, column
	
	tooltip:AddHeader(C.dkyellow(L["Stuff"]))
	tooltip:AddLine (" ")
	
	line, column = tooltip:AddLine(L["Toggle Windowed Mode"])
	tooltip:SetLineScript(line, "OnMouseUp", function(self) 
		SetCVar("gxWindow", 1 - tonumber(GetCVar("gxWindow")))
		RestartGx()
	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)
	
	line, column = tooltip:AddLine(L["Reload UI"])
	--tooltip:SetLineScript(line, "OnMouseUp", function(self) ReloadUI(); end)			
	tooltip:SetLineScript(line, "OnMouseUp", function(self)	StaticPopup_Show("CONFIRM")	end) -- Use static Popup to avoid taint.
	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)
	
	line, column = tooltip:AddLine(L["Logout"])
	tooltip:SetLineScript(line, "OnMouseUp", function(self) Logout() 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)
	
	line, column = tooltip:AddLine(L["Quit Game"])
	tooltip:SetLineScript(line, "OnMouseUp", function(self) Quit() 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)
	
	tooltip:AddLine(" ")
	line, column = nil, nil

	tooltip:AddLine(C.dkgreen(L["Shift + Left-click to switch between windowed and fullscreen mode."]))	
	tooltip:AddLine(C.dkgreen(L["Shift + Right-click to reload UI."]))
	tooltip:AddLine(C.dkgreen(L["Hint: Left-click to logout."]))
	tooltip:AddLine(C.dkgreen(L["Right-click to quit WoW."]))
end

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

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

	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("StuffTT", 1, "LEFT") 
		drawStuffTip()
		func.createTooltip(self,tooltip)
	end
end


local stuff_onclick = function(self, btn)
	local shift = IsShiftKeyDown() or -1
	local cv = 0

	if btn == "RightButton" then
		cv = 1 * shift
	elseif btn == "LeftButton" then
		cv = 2 * shift
	end

	if last_click ~= cv then
		last_click = cv
		return
	end

	-- only for windowed mode switching...
	last_click = 0

	if btn == "RightButton" and shift > 0 then
		ReloadUI()
	elseif btn == "RightButton" and shift < 0 then
		Quit()
	elseif btn == "LeftButton" and shift > 0 then
		SetCVar("gxWindow", 1 - GetCVar("gxWindow"))
		RestartGx()
	elseif btn == "LeftButton" and shift < 0 then
		Logout()
	end
end


local stuff_tooltip = function(t)
	t:AddLine(L["Stuff"])
	t:AddLine(" ")
	t:AddLine(C.white(L["You have to do the same click TWICE for anything to happen!"]))
	t:AddLine(" ")
	t:AddLine(C.dkgreen(L["Hint: Left-click to logout."]))
	t:AddLine(C.dkgreen(L["Right-click to quit WoW."]))
	t:AddLine(C.dkgreen(L["Shift + Left-click to switch between windowed and fullscreen mode."]))
	t:AddLine(C.dkgreen(L["Shift + Right-click to reload UI."]))
end


Broker_Everything:Register(L["Stuff"],
	"Interface\\Addons\\Broker_Everything\\media\\stuff",
	stuff_init,
	0,
	0,
	stuff_onclick,
	0, --stuff_tooltip,
	nil,
	nil)
	


