local _, ns = ...

------------------------------------------------------------
-- Minimap Button
------------------------------------------------------------

local RADIUS = 104

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

function ns:InitMinimapButton()
    local button = CreateFrame("Button", "HousingHeraldMinimapButton", Minimap)
    button:SetSize(33, 33)
    button:SetFrameLevel(8)
    button:RegisterForDrag("LeftButton")
    button:RegisterForClicks("LeftButtonUp", "RightButtonUp")

    -- Icon (addon logo)
    local icon = button:CreateTexture(nil, "BACKGROUND")
    icon:SetSize(24, 24)
    icon:SetPoint("TOPLEFT", button, "TOPLEFT", 6, -5)
    icon:SetTexture("Interface\\AddOns\\HousingHerald\\media\\logo.tga")
    icon:SetMask("Interface\\CharacterFrame\\TempPortraitAlphaMask")

    -- Border (standard minimap button chrome)
    local border = button:CreateTexture(nil, "OVERLAY")
    border:SetSize(54, 54)
    border:SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0)
    border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")

    -- Highlight
    local highlight = button:CreateTexture(nil, "HIGHLIGHT")
    highlight:SetSize(24, 24)
    highlight:SetPoint("TOPLEFT", button, "TOPLEFT", 6, -5)
    highlight:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    highlight:SetBlendMode("ADD")

    -- Position from saved angle
    local angle = ns.db.minimapButtonAngle or 225
    UpdateButtonPosition(button, angle)

    -- Left-click toggles the window; right-click opens the addon options.
    button:SetScript("OnClick", function(_, clicked)
        if clicked == "RightButton" then
            if ns.OpenSettings then ns:OpenSettings() end
        elseif ns.ToggleMainFrame then
            ns:ToggleMainFrame()
        end
    end)

    -- Tooltip
    button:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:ClearLines()
        GameTooltip:AddLine("Housing Herald")
        GameTooltip:AddLine("Click to toggle", 1, 1, 1)
        GameTooltip:AddLine("Drag to move", 1, 1, 1)
        GameTooltip:Show()
    end)

    button:SetScript("OnLeave", function()
        GameTooltip:Hide()
    end)

    -- Drag handlers
    button:SetScript("OnDragStart", function(self)
        self:SetScript("OnUpdate", function(self)
            local cx, cy = GetCursorPosition()
            local scale = Minimap:GetEffectiveScale()
            local mx, my = Minimap:GetCenter()
            local dx = cx / scale - mx
            local dy = cy / scale - my
            local angle = math.deg(math.atan2(dy, dx))
            ns.db.minimapButtonAngle = angle
            UpdateButtonPosition(self, angle)
        end)
    end)

    button:SetScript("OnDragStop", function(self)
        self:SetScript("OnUpdate", nil)
    end)

    ns.minimapButton = button
end
