local _, ns = ...
local L = ns.L or {}

local UI = ns.UI or {}
ns.UI = UI

local MINIMAP_LOGO = "Interface\\Icons\\Inv_10_gearupgrade_drakesshadowflameenhancedcrest"

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

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

    return 0
end

local function createMinimapButton()
    local button = CreateFrame("Button", "VaultloomMinimapButton", Minimap)
    button:SetSize(31, 31)
    button:SetFrameStrata("HIGH")
    button:SetFrameLevel((Minimap:GetFrameLevel() or 0) + 8)
    button:SetMovable(true)
    button:EnableMouse(true)
    button:RegisterForClicks("LeftButtonUp")
    button:RegisterForDrag("RightButton")

    button.icon = button:CreateTexture(nil, "BACKGROUND")
    button.icon:SetTexture(MINIMAP_LOGO)
    button.icon:SetPoint("TOPLEFT", 4, -4)
    button.icon:SetPoint("BOTTOMRIGHT", -3, 3)
    button.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)

    if button.icon.AddMaskTexture then
        button.mask = button:CreateMaskTexture()
        button.mask:SetTexture("Interface\\CHARACTERFRAME\\TempPortraitAlphaMask", "CLAMPTOBLACKADDITIVE", "CLAMPTOBLACKADDITIVE")
        button.mask:SetAllPoints(button.icon)
        button.icon:AddMaskTexture(button.mask)
    end

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

    button.background = button:CreateTexture(nil, "BORDER")
    button.background:SetTexture("Interface\\Minimap\\MiniMap-TrackingBackground")
    button.background:SetSize(20, 20)
    button.background:SetPoint("TOPLEFT", 7, -5)
    button.background:SetVertexColor(0.10, 0.16, 0.26, 0.00)

    button.highlight = button:CreateTexture(nil, "HIGHLIGHT")
    button.highlight:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    button.highlight:SetBlendMode("ADD")
    button.highlight:SetSize(46, 46)
    button.highlight:SetPoint("CENTER", 0, 1)

    button.glow = button:CreateTexture(nil, "OVERLAY", nil, 1)
    button.glow:SetSize(18, 18)
    button.glow:SetPoint("CENTER", 0, -1)
    button.glow:SetTexture("Interface\\Buttons\\UI-Quickslot2")
    button.glow:SetBlendMode("ADD")
    button.glow:SetAlpha(0.16)

    return button
end

function UI:UpdateMinimapPosition()
    if ns.RefreshBrokerMinimapIcon and ns:RefreshBrokerMinimapIcon() then
        if self.minimapButton then
            self.minimapButton:Hide()
        end
        return
    end

    if not self.minimapButton or not Minimap then
        return
    end

    local angle = tonumber(ns:GetMinimapAngle()) or 225
    local radius = math.floor((math.max(Minimap:GetWidth() or 140, Minimap:GetHeight() or 140) * 0.5) + 12)
    local radians = math.rad(angle)
    local x = math.cos(radians) * radius
    local y = math.sin(radians) * radius

    self.minimapButton:ClearAllPoints()
    self.minimapButton:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

function UI:RefreshMinimapButton()
    if ns.EnsureBrokerMinimapIcon and ns:EnsureBrokerMinimapIcon() then
        if self.minimapButton then
            self.minimapButton:Hide()
        end
        return
    end

    if not self.minimapButton then
        return
    end

    if ns:IsMinimapHidden() then
        self.minimapButton:Hide()
    else
        self.minimapButton:Show()
        self:UpdateMinimapPosition()
    end
end

function UI:EnsureMinimapButton()
    if ns.EnsureBrokerMinimapIcon and ns:EnsureBrokerMinimapIcon() then
        if self.minimapButton then
            self.minimapButton:Hide()
        end
        return
    end

    if self.minimapButton then
        return
    end

    local button = createMinimapButton()
    button:SetScript("OnClick", function()
        self:Toggle()
    end)
    button:SetScript("OnMouseDown", function(selfButton)
        selfButton.icon:SetPoint("CENTER", 1, -2)
        if selfButton.glow then
            selfButton.glow:SetPoint("CENTER", 1, -2)
        end
    end)
    button:SetScript("OnMouseUp", function(selfButton)
        selfButton.icon:SetPoint("CENTER", 0, -1)
        if selfButton.glow then
            selfButton.glow:SetPoint("CENTER", 0, -1)
        end
    end)
    button:SetScript("OnDragStart", function(selfButton)
        selfButton.dragging = true
    end)
    button:SetScript("OnDragStop", function(selfButton)
        selfButton.dragging = false
    end)
    button:SetScript("OnUpdate", function(selfButton)
        if not selfButton.dragging or not Minimap then
            return
        end

        local centerX, centerY = Minimap:GetCenter()
        local cursorX, cursorY = GetCursorPosition()
        local scale = UIParent:GetEffectiveScale()
        cursorX = cursorX / scale
        cursorY = cursorY / scale

        local angle = math.deg(atan2(cursorY - centerY, cursorX - centerX))
        ns:SetMinimapAngle(angle)
    end)
    button:SetScript("OnEnter", function(selfButton)
        if selfButton.glow then
            selfButton.glow:SetAlpha(0.28)
        end
        GameTooltip:SetOwner(selfButton, "ANCHOR_CURSOR_RIGHT")
        GameTooltip:AddLine(L.ADDON_TITLE or "Vaultloom", 0.39, 0.85, 1.00)
        GameTooltip:AddLine(L.MINIMAP_TOOLTIP_OPEN_SHORT or "Click: open", 0.90, 0.90, 0.90, true)
        GameTooltip:Show()
    end)
    button:SetScript("OnLeave", function(selfButton)
        if selfButton.glow then
            selfButton.glow:SetAlpha(0.16)
        end
        GameTooltip:Hide()
    end)

    self.minimapButton = button
    self:RefreshMinimapButton()
end
