-- ============================================================
-- Komo Hold'Em — Minimap button
-- Round button with the small K logo; click opens the launcher,
-- drag to reposition around the minimap. /kh minimap toggles it.
-- ============================================================
local _, KH = ...
KH.Minimap = {}
local MM = KH.Minimap

local MEDIA = "Interface\\AddOns\\KomoHoldEm\\Media\\"

function MM:Create()
    if self.button then return self.button end

    local b = CreateFrame("Button", "KomoHoldEmMinimapButton", Minimap)
    b:SetSize(32, 32)
    b:SetFrameStrata("MEDIUM")
    b:SetFrameLevel(8)
    b:RegisterForClicks("LeftButtonUp")
    b:RegisterForDrag("LeftButton")
    b:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")

    local icon = b:CreateTexture(nil, "BACKGROUND")
    icon:SetSize(20, 20)
    icon:SetPoint("CENTER", 0, 1)
    icon:SetTexture(MEDIA .. "LogoSmall")
    icon:SetTexCoord(0, 0.638671875, 0, 0.728515625)
    icon:AddMaskTexture(b:CreateMaskTexture())
    local mask = b:CreateMaskTexture()
    mask:SetTexture("Interface\\CharacterFrame\\TempPortraitAlphaMask",
        "CLAMPTOBLACKADDITIVE", "CLAMPTOBLACKADDITIVE")
    mask:SetAllPoints(icon)
    icon:AddMaskTexture(mask)

    local border = b:CreateTexture(nil, "OVERLAY")
    border:SetSize(54, 54)
    border:SetPoint("TOPLEFT")
    border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")

    b:SetScript("OnClick", function()
        KH.Launcher:Toggle()
    end)
    b:SetScript("OnDragStart", function(self)
        self.dragging = true
        self:SetScript("OnUpdate", function(self)
            local mx, my = Minimap:GetCenter()
            local cx, cy = GetCursorPosition()
            local scale = Minimap:GetEffectiveScale()
            cx, cy = cx / scale, cy / scale
            KH.db.window.minimapAngle = math.deg(math.atan2(cy - my, cx - mx))
            MM:UpdatePosition()
        end)
    end)
    b:SetScript("OnDragStop", function(self)
        self.dragging = false
        self:SetScript("OnUpdate", nil)
    end)
    b:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:AddLine("|cfffad990Komo Hold'Em|r")
        GameTooltip:AddLine("Click to open. Drag to move.", 1, 1, 1)
        GameTooltip:Show()
    end)
    b:SetScript("OnLeave", function() GameTooltip:Hide() end)

    self.button = b
    self:UpdatePosition()
    return b
end

function MM:UpdatePosition()
    if not self.button then return end
    local angle = math.rad(KH.db.window.minimapAngle or 200)
    local radius = (Minimap:GetWidth() / 2) + 6
    self.button:ClearAllPoints()
    self.button:SetPoint("CENTER", Minimap, "CENTER",
        math.cos(angle) * radius, math.sin(angle) * radius)
end

function MM:Refresh()
    if KH.db.settings.minimap then
        self:Create():Show()
    elseif self.button then
        self.button:Hide()
    end
end

function MM:Toggle()
    KH.db.settings.minimap = not KH.db.settings.minimap
    self:Refresh()
    KH:Print("Minimap button " .. (KH.db.settings.minimap and "shown" or "hidden") ..
        ". |cfffad990/kh minimap|r to toggle.")
end
