local _, addon = ...
local HLY = addon.HLY

local cos = math.cos
local rad = math.rad
local sin = math.sin

local function GetMinimapRadius()
    local width = Minimap:GetWidth()
    return (width / 2) + 5
end

function HLY:UpdateMinimapButtonPosition()
    local button = self.minimapButton
    if not button then
        return
    end

    local angle = (self.db and self.db.minimap and self.db.minimap.angle) or 225
    local radius = GetMinimapRadius()
    button:ClearAllPoints()
    button:SetPoint("CENTER", Minimap, "CENTER", cos(rad(angle)) * radius, sin(rad(angle)) * radius)
end

function HLY:UpdateMinimapButton()
    local button = self.minimapButton
    if not button then
        return
    end

    if self.db and self.db.minimap and self.db.minimap.hide then
        button:Hide()
    else
        self:UpdateMinimapButtonPosition()
        button:Show()
    end
end

function HLY:CreateMinimapButton()
    if self.minimapButton then
        return self.minimapButton
    end

    local button = CreateFrame("Button", "HLYqolMinimapButton", Minimap)
    button:SetSize(24, 24)
    button:SetFrameStrata("MEDIUM")
    button:SetMovable(true)
    button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    button:RegisterForDrag("LeftButton")

    button.icon = button:CreateTexture(nil, "ARTWORK")
    button.icon:SetAllPoints(button)
    button.icon:SetPoint("CENTER", 0, 0)
    button.icon:SetTexture(addon.iconPath)
    button.icon:SetTexCoord(0, 1, 0, 1)

    button.highlight = button:CreateTexture(nil, "HIGHLIGHT")
    button.highlight:SetAllPoints(button)
    button.highlight:SetTexture(addon.iconPath)
    button.highlight:SetTexCoord(0, 1, 0, 1)
    button.highlight:SetVertexColor(1, 1, 1, 0.2)

    button:SetScript("OnClick", function(_, mouseButton)
        if mouseButton == "RightButton" then
            if self.db and self.db.minimap then
                self.db.minimap.hide = true
                self:UpdateMinimapButton()
            end
            return
        end

        self:OpenOptions()
    end)

    button:SetScript("OnEnter", function(selfButton)
        GameTooltip:SetOwner(selfButton, "ANCHOR_LEFT")
        GameTooltip:SetText(addon.displayName)
        GameTooltip:AddLine("Left Click: Open options", 1, 1, 1)
        GameTooltip:AddLine("Right Click: Hide button", 1, 1, 1)
        GameTooltip:Show()
    end)

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

    button:SetScript("OnDragStart", function()
        button.isDragging = true
    end)

    button:SetScript("OnDragStop", function()
        button.isDragging = false
    end)

    button:SetScript("OnUpdate", function()
        if not button.isDragging then
            return
        end

        local mx, my = Minimap:GetCenter()
        local px, py = GetCursorPosition()
        local scale = UIParent:GetEffectiveScale()
        px, py = px / scale, py / scale

        local angle = math.deg(math.atan2(py - my, px - mx))
        if self.db and self.db.minimap then
            self.db.minimap.angle = angle
            self:UpdateMinimapButtonPosition()
        end
    end)

    self.minimapButton = button
    return button
end

function HLY:SetupMinimapButton()
    if self.minimapButtonSetup then
        self:UpdateMinimapButton()
        return
    end

    self:CreateMinimapButton()
    self:UpdateMinimapButton()
    self.minimapButtonSetup = true
end
