local _, ns = ...
local SlickUI = ns.SlickUI
local Theme = SlickUI.Theme

local MinimapButton = {}
SlickUI.MinimapButton = MinimapButton

local dataObject
local iconLibrary
local IconPath = "Interface\\AddOns\\SlickUI\\Media\\Icons\\SlickUI_Icon.png"

local function GetErrorCounts()
	if SlickUI.ErrorCatcher and SlickUI.ErrorCatcher.GetCounts then
		return SlickUI.ErrorCatcher:GetCounts()
	end
	return 0, 0
end

function MinimapButton:Create()
	if dataObject then
		return
	end

	if SlickUI.DB.minimap.angle ~= nil then
		SlickUI.DB.minimap.minimapPos = SlickUI.DB.minimap.angle % 360
		SlickUI.DB.minimap.angle = nil
	end

	local dataBroker = LibStub("LibDataBroker-1.1")
	iconLibrary = LibStub("LibDBIcon-1.0")
	dataObject = dataBroker:NewDataObject("SlickUI", {
		type = "launcher",
		text = "SlickUI",
		label = "SlickUI",
		icon = IconPath,
		OnClick = function(_, mouseButton)
			if mouseButton == "LeftButton" then
				local _, unread = GetErrorCounts()
				if unread > 0 then
					SlickUI.ErrorCatcher:Show()
				else
					SlickUI.Options:Open()
				end
			elseif mouseButton == "RightButton" and SlickUI:IsAvailableOutOfCombat("Unit-frame locking") then
				SlickUI.Movers:SetLocked(not SlickUI.DB.locked)
				SlickUI:Print(SlickUI.DB.locked and "Edit Mode closed; frames locked." or "Edit Mode opened; drag SlickUI frames into place.")
			end
		end,
		OnTooltipShow = function(tooltip)
			tooltip:AddLine("SlickUI |cffaaaaaav" .. SlickUI.Version .. "|r", unpack(Theme.Colors.goldText))
			tooltip:AddLine("Clean modern unit frames", 1, 1, 1)
			local total, unread = GetErrorCounts()
			if unread > 0 then
				tooltip:AddLine(" ")
				tooltip:AddLine(string.format("%d new Lua error%s captured", unread, unread == 1 and "" or "s"), 1, 0.18, 0.18)
				tooltip:AddLine(string.format("%d error%s stored in history", total, total == 1 and "" or "s"), 1, 0.72, 0.20)
				tooltip:AddLine("Left-click to review and copy", 0.4, 0.8, 1)
			elseif total > 0 then
				tooltip:AddLine(" ")
				tooltip:AddLine(string.format("%d reviewed Lua error%s stored", total, total == 1 and "" or "s"), 1, 0.72, 0.20)
				tooltip:AddLine("Type /slick errors to view the report", 0.7, 0.7, 0.7)
			end
			tooltip:AddLine(" ")
			tooltip:AddDoubleLine("Left-click", unread > 0 and "Review errors" or "Open options", 0.4, 0.8, 1, 1, 1, 1)
			tooltip:AddDoubleLine("Right-click", "Lock / unlock frames", 0.4, 0.8, 1, 1, 1, 1)
			tooltip:AddLine("Drag to move around the minimap", 0.7, 0.7, 0.7)
		end,
	})

	iconLibrary:Register("SlickUI", dataObject, SlickUI.DB.minimap)
	self.Button = iconLibrary:GetMinimapButton("SlickUI")
	self.AlertDriver = CreateFrame("Frame")
	self.AlertDriver:Hide()
	self.AlertDriver:SetScript("OnUpdate", function()
		if not MinimapButton.Button or not MinimapButton.Button.icon then return end
		local pulse = (math.sin(GetTime() * 5.5) + 1) / 2
		local greenBlue = 1 - 0.86 * pulse
		MinimapButton.Button.icon:SetVertexColor(1, greenBlue, greenBlue)
	end)
	self:RefreshErrorAlert()
end

function MinimapButton:RefreshErrorAlert()
	if not self.Button or not self.AlertDriver then
		return
	end
	local _, unread = GetErrorCounts()
	if unread > 0 then
		self.AlertDriver:Show()
	else
		self.AlertDriver:Hide()
		if self.Button.icon then
			self.Button.icon:SetVertexColor(1, 1, 1)
		end
	end
end

function MinimapButton:Refresh()
	if not iconLibrary then
		return
	end

	if SlickUI.DB.minimap.hide then
		iconLibrary:Hide("SlickUI")
	else
		iconLibrary:Show("SlickUI")
	end
	self:RefreshErrorAlert()
end

function MinimapButton:SetShown(shown)
	SlickUI.DB.minimap.hide = not shown
	self:Refresh()
end

function MinimapButton:Initialize()
	self:Create()
	self:Refresh()
end

SlickUI:RegisterModule("MinimapButton", MinimapButton)
