ItemRackFu = AceLibrary("AceAddon-2.0"):new("FuBarPlugin-2.0","AceDB-2.0")
local L = AceLibrary("AceLocale-2.2"):new("FuBar_ItemRackFu")
local dewdrop = AceLibrary("Dewdrop-2.0")
local IR_UCS, helm, cloak, minimap, events = ItemRack.UpdateCurrentSet, ShowingHelm(), ShowingCloak()

ItemRackFu:RegisterDB("ItemRackFuDB")
ItemRackFu.version = GetAddOnMetadata("Fubar_ItemRackFu", "Version")
ItemRackFu.hasIcon = true
ItemRackFu.hasNoColor = true
ItemRackFu.blizzardTooltip = true
ItemRackFu.cannotAttachToMinimap = true
ItemRackFu.cannotDetachTooltip = true

local function posthook(...)
	ItemRackFu:UpdateText()
	return ...
end

local function IRFU_UCS(...)
	return posthook(IR_UCS(...))
end

function ItemRackFu:OnEnable()
	if ItemRackSettings.ShowMinimap == "ON" then minimap = true end
	if ItemRackUser.EnableEvents == "ON" then events = true end
	ItemRack.UpdateCurrentSet = IRFU_UCS
	self:Update()
end

function ItemRackFu:OnMenuRequest()
	dewdrop:AddLine(
		"text", L["Show helm"],
		"func", function() helm = not helm ShowHelm(helm) end,
		"checked", helm
	)
	dewdrop:AddLine(
		"text", L["Show cloak"],
		"func", function() cloak = not cloak ShowCloak(cloak) end,
		"checked", cloak
	)
	dewdrop:AddLine()
	dewdrop:AddLine(
		"text", L["Show minimap button"],
		"func", "ToggleMinimap",
		"arg1", self,
		"checked", minimap
	)
	dewdrop:AddLine(
		"text", L["Enable events"],
		"func", function() events = not events if events then ItemRack.ToggleEvents() else ItemRackUser.EnableEvents = "OFF" ItemRack.RegisterEvents() end end,
		"checked", events
	)
	dewdrop:AddLine(
		"text", L["Edit sets"],
		"func", function() ItemRack.ToggleOptions(2) end
	)
	dewdrop:AddLine()
end

function ItemRackFu:ToggleMinimap()
	if ItemRackSettings.ShowMinimap == "ON" then
		minimap = not minimap
		ItemRackSettings.ShowMinimap = "OFF"
		ItemRackMinimapFrame:Hide()
	else
		minimap = not minimap
		ItemRackSettings.ShowMinimap = "ON"
		ItemRackMinimapFrame:Show()
	end
end

function ItemRackFu:OnTooltipUpdate()
	GameTooltip:AddLine("ItemRack")
	GameTooltip:AddLine(" ")
	GameTooltip:AddLine(L["|cffeda55fLeft-Click|r|cff33ff33: Select a set|r"])
	GameTooltip:AddLine(L["|cffeda55fAlt-Click|r|cff33ff33: Show hidden sets|r"])
	GameTooltip:AddLine(L["|cffeda55fShift-Click|r|cff33ff33: Unequip current set|r"])
	GameTooltip:AddLine(L["|cffeda55fRight-Click|r|cff33ff33: Show menu|r"])
end

function ItemRackFu:OnTextUpdate()
	local setname, texture = ItemRackUser.CurrentSet
	if setname then
		self:SetText(setname)
		texture = ItemRackUser.Sets[ItemRackUser.CurrentSet].icon
	else
		self:SetText("ItemRack")
	end
	if texture then
		self:SetIcon(texture)
	else
		self:SetIcon()
	end
end

function ItemRackFu:OnClick()
	if IsShiftKeyDown() then
		if ItemRackUser.CurrentSet and ItemRackUser.Sets[ItemRackUser.CurrentSet] then
			ItemRack.UnequipSet(ItemRackUser.CurrentSet)
			return
		end
	end
	if ItemRackMenuFrame:IsVisible() then
		ItemRackMenuFrame:Hide()
	else
		local xpos,ypos = GetCursorPosition()
		if ypos>400 then
			ItemRack.DockWindows("TOPRIGHT",this,"BOTTOMRIGHT","VERTICAL")
		else
			ItemRack.DockWindows("BOTTOMRIGHT",this,"TOPRIGHT","VERTICAL")
		end
		ItemRack.BuildMenu(20)
	end
end