local addonName, addon = ...

addon.MinimapButton = addon.MinimapButton or {}
local M = addon.MinimapButton
local DEFAULT_ANGLE = 225

local function Settings()
    WhatsNextUserData = type(WhatsNextUserData) == "table" and WhatsNextUserData or {}
    WhatsNextUserData.settings = type(WhatsNextUserData.settings) == "table" and WhatsNextUserData.settings or {}
    WhatsNextUserData.settings.minimap = type(WhatsNextUserData.settings.minimap) == "table" and WhatsNextUserData.settings.minimap or {}
    local s = WhatsNextUserData.settings.minimap
    if s.shown == nil then s.shown = true end
    if type(s.angle) ~= "number" then s.angle = DEFAULT_ANGLE end
    return s
end

local function Position(button, angle)
    local radius = math.max(20, math.min(Minimap:GetWidth() or 140, Minimap:GetHeight() or 140) * .5 - 5)
    local r = math.rad(tonumber(angle) or DEFAULT_ANGLE)
    button:ClearAllPoints()
    button:SetPoint("CENTER", Minimap, "CENTER", math.cos(r)*radius, math.sin(r)*radius)
end

local function CursorAngle()
    local scale = Minimap:GetEffectiveScale()
    local mx, my = Minimap:GetCenter()
    local cx, cy = GetCursorPosition()
    return math.deg(math.atan2(cy/scale-my, cx/scale-mx))
end

function M:SetShown(value)
    local s = Settings()
    s.shown = value and true or false
    if self.Button then self.Button:SetShown(s.shown) end
    if addon.UI and addon.UI.MinimapButtonSettingCheck then
        addon.UI.MinimapButtonSettingCheck:SetChecked(s.shown)
    end
end

function M:Refresh()
    if not self.Button then return end
    local s = Settings()
    Position(self.Button, s.angle)
    self.Button:SetShown(s.shown ~= false)
    if addon.UI and addon.UI.MinimapButtonSettingCheck then
        addon.UI.MinimapButtonSettingCheck:SetChecked(s.shown ~= false)
    end
end

function M:Initialize()
    if self.Button then self:Refresh() return end

    local b = CreateFrame("Button", "WhatsNextMinimapButton", Minimap)
    self.Button = b
    b:SetSize(32,32)
    b:SetFrameStrata("MEDIUM")
    b:SetFrameLevel((Minimap:GetFrameLevel() or 1)+8)
    b:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    b:RegisterForDrag("LeftButton")

    local icon = b:CreateTexture(nil, "ARTWORK")
    icon:SetTexture("Interface\\AddOns\\WhatsNext\\Media\\WhatsNext_Minimap")
    icon:SetSize(22,22)
    icon:SetPoint("CENTER")
    icon:SetTexCoord(.06,.94,.06,.94)

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

    local highlight = b:CreateTexture(nil, "HIGHLIGHT")
    highlight:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    highlight:SetBlendMode("ADD")
    highlight:SetAllPoints()

    b:SetScript("OnClick", function(_, mouseButton)
        if mouseButton == "LeftButton" then
            if addon.UI and addon.UI.Toggle then addon.UI:Toggle() end
        elseif mouseButton == "RightButton" then
            if addon.UI and addon.UI.Show then addon.UI:Show() end
            if addon.UI and addon.UI.ShowSection then addon.UI:ShowSection("settings") end
        end
    end)

    b:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:SetText("What's Next?", 1,.82,0)
        if addon.UI and addon.UI.T then
            GameTooltip:AddLine(addon.UI:T("MINIMAP_LEFT_CLICK","Left-click: Open / close addon"),1,1,1)
            GameTooltip:AddLine(addon.UI:T("MINIMAP_RIGHT_CLICK","Right-click: Open Settings"),.8,.8,.8)
            GameTooltip:AddLine(addon.UI:T("MINIMAP_DRAG","Drag: Move around the minimap"),.8,.8,.8)
        end
        GameTooltip:Show()
    end)
    b:SetScript("OnLeave", function() GameTooltip:Hide() end)

    b:SetScript("OnDragStart", function(self)
        self:SetScript("OnUpdate", function(btn)
            local a = CursorAngle()
            Settings().angle = a
            Position(btn,a)
        end)
    end)
    b:SetScript("OnDragStop", function(self)
        self:SetScript("OnUpdate",nil)
        local a = CursorAngle()
        Settings().angle = a
        Position(self,a)
    end)

    self:Refresh()
end
