--[[ --------------------------------------------------------
		Equipment manager
------------------------------------------------------------]]

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

local tooltip = nil
local equipPending = nil


local equipmentEvents = {
	"UNIT_INVENTORY_CHANGED",
	"EQUIPMENT_SETS_CHANGED",
	"PLAYER_LOGIN",
	"PLAYER_REGEN_ENABLED",
	"UNIT_INVENTORY_CHANGED",
	"EQUIPMENT_SETS_CHANGED",
}

local updateLDB = function ()
	local dataobj = LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Equipment"])

	local numEquipSets = GetNumEquipmentSets()
	if numEquipSets >= 1 then 
		for i = 1, GetNumEquipmentSets() do 
			local name, icon, _, isEquipped, _, _, _, numMissing = GetEquipmentSetInfo(i)
			if isEquipped then 
				dataobj.icon = icon
				dataobj.text = name
				return
			else 
				dataobj.icon = "Interface\\Addons\\Broker_Everything\\media\\equip.tga"
				dataobj.text = L["Unknown Equipment Set"]
			end
		end
	end


end


local drawEquipmentTip = function()
	local line, column

	tooltip:AddHeader(C.dkyellow(L["Equipment"]))
	tooltip:AddSeparator()
	if not CanUseEquipmentSets() then
		tooltip:AddLine(L["Equipment Manager is not enabled."])
		tooltip:AddLine(L["Enable Equipment Manager from the Character pane."])
		return
	end

	local numEquipSets = GetNumEquipmentSets()
	if numEquipSets < 1 then 
		tooltip:AddLine(L["You do not have any Equipment Sets."])
		line, column = tooltip:AddLine(L["Click here to open Equipment Manager."])
		tooltip:SetLineScript(line, "OnMouseUp", function(self) ToggleCharacter("PaperDollFrame") 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 = nil, nil
		return
	end

	for i = 1, numEquipSets do 
		local name, icon, _, isEquipped, _, _, _, numMissing = GetEquipmentSetInfo(i)
		local formatName = ((numMissing > 0) and C.red(name)) or (isEquipped and C.ltyellow(name)) or name

		line, column = tooltip:AddLine()
		--		tooltip:SetCell(line, 1, C.ltyellow(L["Level"]), nil, nil, 1, LibQTip.LabelProvidor, 0, 10)
		tooltip:SetCell(line, 1, formatName, nil, nil, 1, LibQTip.LabelProvidor, 0, 5)
		tooltip:SetCell(line, 2, " |T"..icon..":16|t", nil, nil, 1)
		tooltip:SetLineScript(line, "OnMouseUp", function(self) 
			if IsShiftKeyDown() then 
				local dialog = StaticPopup_Show('CONFIRM_SAVE_EQUIPMENT_SET', name)
				dialog.data = name 
			elseif IsControlKeyDown() then
				local dialog = StaticPopup_Show('CONFIRM_DELETE_EQUIPMENT_SET', name)
				dialog.data = name				
			else
				if InCombatLockdown() then 
					equipPending = name
				else
					UseEquipmentSet(name)
				end
			end 
		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

	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Click to equip a set."]), nil, nil, 2)
	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Shift-Click to update or save a set."]), nil, nil, 2)
	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Cntrl-Click to delete a set"]), nil, nil, 2)

	line, column = nil, nil
end


local equipment_OnEvent = function (self, event, ...)
	if event == "PLAYER_REGEN_ENABLED" then
		if equipPending ~= nil then
			UseEquipmentSet(equipPending)
			equipPending = nil
		end
	end

	if event == "PLAYER_LOGIN" then
		self:UnregisterEvent("PLAYER_LOGIN")
	end

	if event == "UNIT_INVENTORY_CHANGED" then
		local unit = ...
		if unit ~= "player" then return end
	end

	updateLDB()
end


local equipment_init = function(obj)

	if Broker_EverythingDB[L["Equipment"]].enabled == nil then
		Broker_EverythingDB[L["Equipment"]] = {
			enabled = true
		}
	end
	if not obj then return end

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

	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("EquipmentTT", 2, "LEFT", "LEFT") 
		drawEquipmentTip()

		func.createTooltip(self,tooltip)

	end
end







Broker_Everything:Register(L["Equipment"],	-- name
	"Interface\\Addons\\Broker_Everything\\media\\equip",--[=[Interface\Icons\INV_Misc_QuestionMark]=] -- icon
	equipment_init,								-- initialisation
	equipment_OnEvent,				-- onevent
	equipmentEvents,				-- events
	0,					-- onclick
	0,					-- tooltip
	nil,					-- onupdate
	nil)			-- update interval