local addonName, ns = ...

local math_rad = math.rad
local math_cos = math.cos
local math_sin = math.sin
local math_deg = math.deg
local math_atan2 = math.atan2

-- Create the minimap button
local btn = CreateFrame("Button", "BrightQoL_MinimapButton", Minimap)
btn:SetSize(32, 32)
btn:SetFrameStrata("MEDIUM")
btn:SetFrameLevel(8)
btn:RegisterForDrag("LeftButton")
btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
btn:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")

-- Background (simple black circle)
local bg = btn:CreateTexture(nil, "BACKGROUND")
bg:SetSize(24, 24)
bg:SetPoint("CENTER", 0, 0)
bg:SetTexture("Interface\\Minimap\\UI-Minimap-Background")
bg:SetVertexColor(0, 0, 0, 1)

-- Icon texture (centered, slightly smaller than frame)
local icon = btn:CreateTexture(nil, "ARTWORK")
icon:SetSize(20, 20)
icon:SetPoint("CENTER", 0, 0)
icon:SetTexture("Interface\\AddOns\\BrightQoL\\Assets\\icon.tga")

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

-- Position button around minimap edge using angle
local function UpdatePosition(angle)
    local radius = Minimap:GetWidth() / 2
    local rad = math_rad(angle)
    local x = math_cos(rad) * radius
    local y = math_sin(rad) * radius
    btn:ClearAllPoints()
    btn:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

-- Drag handling
local isDragging = false

btn:SetScript("OnDragStart", function(self)
    isDragging = true
    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))
        if angle < 0 then angle = angle + 360 end
        UpdatePosition(angle)
        BrightQoL.config.minimapButtonPos = angle
    end)
end)

btn:SetScript("OnDragStop", function(self)
    isDragging = false
    self:SetScript("OnUpdate", nil)
end)

-- Click handler - open settings
btn:SetScript("OnClick", function(self, button)
    if button == "LeftButton" then
        if InCombatLockdown() then
            ns:Log("Cannot open settings during combat.", "ff4444")
            return
        end
        if ns.MainFrame then
            if ns.MainFrame:IsShown() then
                ns.MainFrame:Hide()
            else
                ns.MainFrame:Show()
                if ns.MainFrame.ResetContent then
                    ns.MainFrame:ResetContent()
                end
                local lastTab = BrightQoL and BrightQoL.config and BrightQoL.config.lastTab
                if lastTab and ns.OpenTab then
                    ns:OpenTab(lastTab)
                elseif ns.InitOptOptions then
                    ns:InitOptOptions()
                end
            end
        end
    end
end)

-- Tooltip
btn:SetScript("OnEnter", function(self)
    if isDragging then return end
    GameTooltip:SetOwner(self, "ANCHOR_LEFT")
    GameTooltip:AddLine("BrightQoL")
    GameTooltip:AddLine("Click to open settings", 0.8, 0.8, 0.8)
    GameTooltip:AddLine("Drag to reposition", 0.6, 0.6, 0.6)
    GameTooltip:Show()
end)

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

-- Initialize position after saved variables load
local loader = CreateFrame("Frame")
loader:RegisterEvent("ADDON_LOADED")
loader:SetScript("OnEvent", function(self, event, name)
    if name == addonName then
        -- Set default if not saved
        if not BrightQoL.config.minimapButtonPos then
            BrightQoL.config.minimapButtonPos = 225
        end
        UpdatePosition(BrightQoL.config.minimapButtonPos)
        self:UnregisterEvent("ADDON_LOADED")
    end
end)

-- After login: hide minimap button if BrightMinimap addon compartment is enabled
local acHideFrame = CreateFrame("Frame")
acHideFrame:RegisterEvent("PLAYER_LOGIN")
acHideFrame:SetScript("OnEvent", function(self)
    self:UnregisterEvent("PLAYER_LOGIN")
    local db = BrightQoL and BrightQoL.minimap
    if db and db.enabled and db.addonCompartment then
        btn:Hide()
    end
end)
