local _, ns = ...
local MythicTools = ns.MythicTools

local WHITE_TEXTURE = "Interface\\Buttons\\WHITE8x8"
local ADDON_ICON = "Interface\\AddOns\\SkyMythicHistory\\Media\\smh.tga"
local MINIMAP_BUTTON_SIZE = 32
local MINIMAP_BUTTON_ICON_INSET = 5
local MINIMAP_BUTTON_EDGE_OVERLAP = 8

local cos = math.cos
local sin = math.sin
local rad = math.rad
local deg = math.deg
local atan2 = math.atan2 or math.atan
local min = math.min

local function GetMinimapButtonRadius()
    local width = Minimap and Minimap:GetWidth() or 140
    local height = Minimap and Minimap:GetHeight() or width
    return (min(width or 140, height or 140) / 2) + (MINIMAP_BUTTON_SIZE / 2) - MINIMAP_BUTTON_EDGE_OVERLAP
end

function MythicTools:UpdateMinimapButtonPosition()
    if not (self.minimapButton and Minimap and self.db and self.db.settings) then
        return
    end

    local angle = tonumber(self.db.settings.minimapAngle) or -35
    local radius = GetMinimapButtonRadius()
    self.minimapButton:ClearAllPoints()
    self.minimapButton:SetPoint("CENTER", Minimap, "CENTER", cos(rad(angle)) * radius, sin(rad(angle)) * radius)
end

function MythicTools:CreateMinimapButton()
    if self.minimapButton or not Minimap then
        return self.minimapButton
    end

    local button = CreateFrame("Button", "MythicToolsMinimapButton", Minimap)
    button:SetSize(MINIMAP_BUTTON_SIZE, MINIMAP_BUTTON_SIZE)
    button:SetFrameStrata("MEDIUM")
    button:SetFrameLevel((Minimap:GetFrameLevel() or 0) + 8)
    button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    button:RegisterForDrag("LeftButton")

    button.Icon = button:CreateTexture(nil, "ARTWORK")
    button.Icon:SetPoint("TOPLEFT", MINIMAP_BUTTON_ICON_INSET, -MINIMAP_BUTTON_ICON_INSET)
    button.Icon:SetPoint("BOTTOMRIGHT", -MINIMAP_BUTTON_ICON_INSET, MINIMAP_BUTTON_ICON_INSET)
    button.Icon:SetTexture(ADDON_ICON)
    button.Icon:SetBlendMode("BLEND")
    button.Icon:SetTexCoord(0, 1, 0, 1)
    button.Icon:SetVertexColor(1, 1, 1, 1)

    button:SetHighlightTexture(WHITE_TEXTURE)
    local highlight = button:GetHighlightTexture()
    highlight:SetPoint("TOPLEFT", MINIMAP_BUTTON_ICON_INSET, -MINIMAP_BUTTON_ICON_INSET)
    highlight:SetPoint("BOTTOMRIGHT", -MINIMAP_BUTTON_ICON_INSET, MINIMAP_BUTTON_ICON_INSET)
    highlight:SetVertexColor(1, 1, 1, 0.10)

    button:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:AddLine(MythicTools:L("ADDON_TITLE"), 1, 0.82, 0)
        GameTooltip:AddLine(MythicTools:L("MINIMAP_LEFT_CLICK"), 1, 1, 1)
        GameTooltip:AddLine(MythicTools:L("MINIMAP_RIGHT_CLICK"), 1, 1, 1)
        GameTooltip:Show()
    end)

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

    button:SetScript("OnClick", function(_, mouseButton)
        if type(MythicTools.ToggleMainFrame) ~= "function" then
            return
        end

        if mouseButton == "RightButton" then
            MythicTools:ToggleMainFrame(true)
            if type(MythicTools.ShowTab) == "function" then
                MythicTools:ShowTab("Settings")
            end
        else
            MythicTools:ToggleMainFrame()
        end
    end)

    button:SetScript("OnDragStart", function(self)
        GameTooltip:Hide()
        self:SetScript("OnUpdate", function()
            local centerX, centerY = Minimap:GetCenter()
            local scale = Minimap:GetEffectiveScale() or 1
            local cursorX, cursorY = GetCursorPosition()
            cursorX = cursorX / scale
            cursorY = cursorY / scale
            MythicTools.db.settings.minimapAngle = deg(atan2(cursorY - centerY, cursorX - centerX))
            MythicTools:UpdateMinimapButtonPosition()
        end)
    end)

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

    self.minimapButton = button
    return button
end

function MythicTools:UpdateMinimapButton()
    if not (self.db and self.db.settings) then
        return
    end

    local button = self:CreateMinimapButton()
    if not button then
        return
    end

    if self.db.settings.showMinimapButton then
        self:UpdateMinimapButtonPosition()
        button:Show()
    else
        button:Hide()
    end
end
