-- Portal Pro :: UI/MinimapButton.lua
-- Hand-rolled rather than LibDBIcon: it's ~80 lines, keeps the addon dependency-free,
-- and lets the button match the rest of the theme instead of Blizzard's bevelled ring.

local ADDON_NAME, PP = ...
local UI, T, U = PP.UI, PP.Theme, PP.U

local MB = PP:NewModule("MinimapButton")
PP.MinimapButton = MB

local RADIUS = 80
local button

local function positionAt(angle)
	local rad = math.rad(angle)
	button:ClearAllPoints()
	button:SetPoint("CENTER", Minimap, "CENTER",
		math.cos(rad) * RADIUS, math.sin(rad) * RADIUS)
end

local function build()
	if button then return button end

	button = CreateFrame("Button", "PortalProMinimapButton", Minimap)
	button:SetSize(26, 26)
	button:SetFrameStrata("MEDIUM")
	button:SetFrameLevel(8)

	local bg = button:CreateTexture(nil, "BACKGROUND")
	bg:SetAllPoints()
	bg:SetColorTexture(T:C("base"))

	UI:Border(button, "borderStrong")

	local icon = button:CreateTexture(nil, "ARTWORK")
	icon:SetPoint("TOPLEFT", 3, -3)
	icon:SetPoint("BOTTOMRIGHT", -3, 3)
	icon:SetTexture("Interface\\ICONS\\Spell_Arcane_PortalStormWind")
	icon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
	button.icon = icon

	-- Lead-count pip
	local pip = button:CreateTexture(nil, "OVERLAY")
	pip:SetSize(8, 8)
	pip:SetPoint("TOPRIGHT", 1, 1)
	pip:SetColorTexture(T:C("accent"))
	pip:Hide()
	button.pip = pip

	button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
	button:SetScript("OnClick", function(self, click)
		if click == "RightButton" then
			if PP.CompactBar:IsShown() then
				PP.profile.ui.compact = false
				PP.CompactBar:Hide()
			else
				PP.profile.ui.compact = true
				PP.UI:Hide()
				PP.CompactBar:Show()
			end
		else
			PP.UI:Toggle()
		end
	end)

	button:SetScript("OnEnter", function(self)
		self:SetBorderColor("accent")
		GameTooltip:SetOwner(self, "ANCHOR_LEFT")
		GameTooltip:AddLine("Portal Pro", 1, 1, 1)
		local totals = PP.Stats:Totals("session")
		GameTooltip:AddLine(("Session net: %s"):format(U.MoneyPlain(totals.net)), 0.61, 0.63, 0.67)
		GameTooltip:AddLine(("Leads waiting: %d"):format(PP.Leads:Count()), 0.61, 0.63, 0.67)
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Left-click: open", 0.42, 0.60, 1)
		GameTooltip:AddLine("Right-click: compact bar", 0.42, 0.60, 1)
		GameTooltip:AddLine("Drag: reposition", 0.42, 0.60, 1)
		GameTooltip:Show()
	end)
	button:SetScript("OnLeave", function(self)
		self:SetBorderColor("borderStrong")
		GameTooltip:Hide()
	end)

	-- Drag around the minimap edge.
	button:RegisterForDrag("LeftButton")
	button:SetScript("OnDragStart", function(self)
		self:SetScript("OnUpdate", function()
			local mx, my = Minimap:GetCenter()
			local cx, cy = GetCursorPosition()
			local scale = Minimap:GetEffectiveScale()
			cx, cy = cx / scale, cy / scale
			local angle = math.deg(math.atan2(cy - my, cx - mx))
			PP.profile.ui.minimap.angle = angle
			positionAt(angle)
		end)
	end)
	button:SetScript("OnDragStop", function(self)
		self:SetScript("OnUpdate", nil)
	end)

	positionAt(PP.profile.ui.minimap.angle or 200)
	return button
end

function MB:Update()
	build()
	button:SetShown(not PP.profile.ui.minimap.hide)
	local count = PP.Leads:Count()
	button.pip:SetShown(count > 0)
end

function MB:OnEnable()
	if not Minimap then return end
	self:Update()
	PP:On("LEADS_CHANGED", function() MB:Update() end)
end
