-- Minimap_Settings.lua
local addonName, addonTable = ...

local minimapButton = nil

local minimapShapes = {
    ["ROUND"]                  = {true, true, true, true},
    ["SQUARE"]                 = {false, false, false, false},
    ["CORNER-TOPLEFT"]         = {true, false, false, false},
    ["CORNER-TOPRIGHT"]        = {false, false, true, false},
    ["CORNER-BOTTOMLEFT"]      = {false, true, false, false},
    ["CORNER-BOTTOMRIGHT"]     = {false, false, false, true},
    ["SIDE-LEFT"]              = {true, true, false, false},
    ["SIDE-RIGHT"]             = {false, false, true, true},
    ["SIDE-TOP"]               = {true, false, true, false},
    ["SIDE-BOTTOM"]            = {false, true, false, true},
    ["TRICORNER-TOPLEFT"]      = {true, true, true, false},
    ["TRICORNER-TOPRIGHT"]     = {true, false, true, true},
    ["TRICORNER-BOTTOMLEFT"]   = {true, true, false, true},
    ["TRICORNER-BOTTOMRIGHT"]  = {false, true, true, true},
}

local function UpdateMinimapPos(btn)
    if not RNToolsOverlaySave then RNToolsOverlaySave = {} end
    local angle = math.rad(RNToolsOverlaySave.minimapAngle or 225)
    local x, y, q = math.cos(angle), math.sin(angle), 1
    if x < 0 then q = q + 1 end
    if y > 0 then q = q + 2 end
    local shape = GetMinimapShape and GetMinimapShape() or "ROUND"
    local quad = minimapShapes[shape]
    if quad[q] then
        x, y = x * 80, y * 80
    else
        local r = 103.13708498985
        x = math.max(-80, math.min(x * r, 80))
        y = math.max(-80, math.min(y * r, 80))
    end
    btn:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

local function OnDragUpdate(self)
    local mx, my = Minimap:GetCenter()
    local px, py = GetCursorPosition()
    local scale  = Minimap:GetEffectiveScale()
    px, py = px / scale, py / scale
    if not RNToolsOverlaySave then RNToolsOverlaySave = {} end
    RNToolsOverlaySave.minimapAngle = math.deg(math.atan2(py - my, px - mx)) % 360
    UpdateMinimapPos(self)
end

local function OnDragStart(self)
    self:LockHighlight()
    self.icon:SetTexCoord(0, 1, 0, 1)
    self:SetScript("OnUpdate", OnDragUpdate)
    GameTooltip:Hide()
end

local function OnDragStop(self)
    self:SetScript("OnUpdate", nil)
    self.icon:SetTexCoord(0.05, 0.95, 0.05, 0.95)
    self:UnlockHighlight()
end

local function OnClick(self, button)
    if button == "LeftButton" then
        if ToggleMainUI then ToggleMainUI() end
    elseif button == "RightButton" then
        if ToggleStatusOverlayVisibility then ToggleStatusOverlayVisibility() end
    end
end

local function OnEnter(self)
    GameTooltip:SetOwner(self, "ANCHOR_LEFT")
    GameTooltip:AddLine("RealNibba's Raid Tools", 1, 0.82, 0)
    GameTooltip:AddLine("by RealNibba", 0.7, 0.7, 0.7)
    GameTooltip:AddLine(" ")
    GameTooltip:AddLine("Left Click: Open / Close", 0, 1, 0)
    GameTooltip:AddLine("Right Click: Toggle Status Bar", 0, 1, 0)
    GameTooltip:Show()
end

local function OnLeave() GameTooltip:Hide() end

function RNTools_ApplyMinimapSettings()
    local p = (RNToolsAddon and RNToolsAddon.db and RNToolsAddon.db.profile) or {}
    if not minimapButton then return end
    if p.minimap_show == false then
        minimapButton:Hide()
    else
        minimapButton:Show()
    end
end

function CreateMinimapButtonIfNeeded()
    if minimapButton then return end

    minimapButton = CreateFrame("Button", "RNToolsMinimapButton", Minimap)
    minimapButton:SetFrameStrata("MEDIUM")
    minimapButton:SetWidth(31); minimapButton:SetHeight(31)
    minimapButton:SetFrameLevel(8)
    minimapButton:RegisterForClicks("anyUp")
    minimapButton:RegisterForDrag("LeftButton")
    minimapButton:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")

    local overlay = minimapButton:CreateTexture(nil, "OVERLAY")
    overlay:SetWidth(53); overlay:SetHeight(53)
    overlay:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
    overlay:SetPoint("TOPLEFT")

    local icon = minimapButton:CreateTexture(nil, "BACKGROUND")
    icon:SetWidth(20); icon:SetHeight(20)
    icon:SetTexture("Interface\\Icons\\Achievement_Boss_Algalon_01")
    icon:SetTexCoord(0.05, 0.95, 0.05, 0.95)
    icon:SetPoint("TOPLEFT", 7, -5)
    minimapButton.icon = icon

    minimapButton:SetScript("OnEnter",    OnEnter)
    minimapButton:SetScript("OnLeave",    OnLeave)
    minimapButton:SetScript("OnClick",    OnClick)
    minimapButton:SetScript("OnDragStart", OnDragStart)
    minimapButton:SetScript("OnDragStop",  OnDragStop)

    UpdateMinimapPos(minimapButton)
end
