--------------------------------------------------------------------------------
-- S-Frames: Minimap quick access button (MinimapButton.lua)
-- Left Click  : open /nui UI settings
-- Right Click : open /nui bag settings
-- Shift + Drag: move icon around minimap
--------------------------------------------------------------------------------

SFrames.MinimapButton = SFrames.MinimapButton or {}

local button = nil
local DEFAULT_ANGLE = 225

local function EnsureDB()
    if not SFramesDB then
        SFramesDB = {}
    end

    if type(SFramesDB.minimapButton) ~= "table" then
        SFramesDB.minimapButton = {}
    end

    local db = SFramesDB.minimapButton
    if type(db.angle) ~= "number" then
        db.angle = DEFAULT_ANGLE
    end
    if db.hide == nil then
        db.hide = false
    end

    return db
end

local function SafeAtan2(y, x)
    if math.atan2 then
        return math.atan2(y, x)
    end

    if x > 0 then
        return math.atan(y / x)
    elseif x < 0 and y >= 0 then
        return math.atan(y / x) + math.pi
    elseif x < 0 and y < 0 then
        return math.atan(y / x) - math.pi
    elseif x == 0 and y > 0 then
        return math.pi / 2
    elseif x == 0 and y < 0 then
        return -math.pi / 2
    end

    return 0
end

local function GetOrbitRadius()
    local w = Minimap and Minimap:GetWidth() or 140
    return w / 2 + 6
end

local function UpdatePosition()
    if not button or not Minimap then
        return
    end

    local db = EnsureDB()
    local radius = GetOrbitRadius()
    local angleRad = math.rad(db.angle or DEFAULT_ANGLE)
    local x = math.cos(angleRad) * radius
    local y = math.sin(angleRad) * radius

    button:ClearAllPoints()
    button:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

local function StartDrag()
    if not button or not Minimap then
        return
    end

    button:SetScript("OnUpdate", function()
        local mx, my = GetCursorPosition()
        local scale = Minimap:GetEffectiveScale() or 1
        if scale == 0 then scale = 1 end

        mx = mx / scale
        my = my / scale

        local cx, cy = Minimap:GetCenter()
        if not cx or not cy then
            return
        end

        local angle = math.deg(SafeAtan2(my - cy, mx - cx))
        if angle < 0 then
            angle = angle + 360
        end

        EnsureDB().angle = angle
        UpdatePosition()
    end)
end

local function StopDrag()
    if button then
        button:SetScript("OnUpdate", nil)
    end
end

function SFrames.MinimapButton:Refresh()
    local db = EnsureDB()

    if not button then
        return
    end

    if db.hide then
        button:Hide()
    else
        button:Show()
    end

    if button.icon and SFrames.SetIcon then
        SFrames:SetIcon(button.icon, "logo")
        local A = SFrames.ActiveTheme
        if A and A.accentLight then
            button.icon:SetVertexColor(A.accentLight[1], A.accentLight[2], A.accentLight[3], 1)
        end
    end

    UpdatePosition()
end

function SFrames.MinimapButton:Initialize()
    if button then
        self:Refresh()
        return
    end

    if not Minimap then
        return
    end

    EnsureDB()

    button = CreateFrame("Button", "SFramesMinimapButton", Minimap)
    button:SetWidth(32)
    button:SetHeight(32)
    button:SetFrameStrata("MEDIUM")
    button:SetMovable(false)
    button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    button:RegisterForDrag("LeftButton")

    local border = button:CreateTexture(nil, "OVERLAY")
    border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
    border:SetWidth(56)
    border:SetHeight(56)
    border:SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0)

    local bg = button:CreateTexture(nil, "BACKGROUND")
    bg:SetTexture("Interface\\Minimap\\UI-Minimap-Background")
    bg:SetWidth(20)
    bg:SetHeight(20)
    bg:SetPoint("CENTER", button, "CENTER", 0, 0)

    local icon = button:CreateTexture(nil, "ARTWORK")
    icon:SetWidth(20)
    icon:SetHeight(20)
    icon:SetPoint("CENTER", button, "CENTER", 0, 0)

    local hl = button:CreateTexture(nil, "HIGHLIGHT")
    hl:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    hl:SetBlendMode("ADD")
    hl:SetWidth(31)
    hl:SetHeight(31)
    hl:SetPoint("CENTER", button, "CENTER", 0, 0)

    button.icon = icon

    button:SetScript("OnClick", function()
        if arg1 == "RightButton" then
            if SFrames.ConfigUI and SFrames.ConfigUI.Build then
                SFrames.ConfigUI:Build("bags")
            end
        else
            if SFrames.ConfigUI and SFrames.ConfigUI.Build then
                SFrames.ConfigUI:Build("ui")
            end
        end
    end)

    button:SetScript("OnEnter", function()
        GameTooltip:SetOwner(this, "ANCHOR_LEFT")
        GameTooltip:ClearLines()
        local A = SFrames.ActiveTheme
        local tr, tg, tb = 1, 0.82, 0.94
        if A and A.accentLight then
            tr, tg, tb = A.accentLight[1], A.accentLight[2], A.accentLight[3]
        end
        GameTooltip:AddLine("Nanami-UI", tr, tg, tb)
        GameTooltip:AddLine("左键: 打开 UI 设置", 0.85, 0.85, 0.85)
        GameTooltip:AddLine("右键: 打开背包设置", 0.85, 0.85, 0.85)
        GameTooltip:AddLine("Shift+拖动: 移动图标", 0.6, 0.9, 0.6)
        GameTooltip:Show()
    end)

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

    button:SetScript("OnDragStart", function()
        if not IsShiftKeyDown() then
            return
        end
        StartDrag()
    end)

    button:SetScript("OnDragStop", function()
        StopDrag()
    end)

    self:Refresh()
end
