local addonName, SinsToolkit = ...

local BRAND_LOGO = "Interface\\AddOns\\SinsToolkit\\Media\\Logo.tga"

local function GradientText(text)
    local startColor = { 126, 79, 168 }
    local endColor = { 246, 239, 255 }
    local out, visibleIndex, visibleCount = {}, 0, 0

    for i = 1, #text do
        if text:sub(i, i) ~= " " then
            visibleCount = visibleCount + 1
        end
    end

    for i = 1, #text do
        local char = text:sub(i, i)
        if char == " " then
            out[#out + 1] = char
        else
            visibleIndex = visibleIndex + 1
            local t = visibleCount > 1 and ((visibleIndex - 1) / (visibleCount - 1)) or 1
            local r = math.floor(startColor[1] + ((endColor[1] - startColor[1]) * t) + 0.5)
            local g = math.floor(startColor[2] + ((endColor[2] - startColor[2]) * t) + 0.5)
            local b = math.floor(startColor[3] + ((endColor[3] - startColor[3]) * t) + 0.5)
            out[#out + 1] = ("|cff%02x%02x%02x%s|r"):format(r, g, b, char)
        end
    end

    return table.concat(out)
end

function SinsToolkit:GetOptionsTable()
    local getMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata
    local version = (getMetadata and getMetadata(addonName, "Version")) or "Unknown"
    local provider = (SinsToolkit.GetProviderDisplayName and SinsToolkit.GetProviderDisplayName()) or "Standalone"

    return {
        type = "group",
        name = "|T" .. BRAND_LOGO .. ":16:16|t " .. GradientText("Sin's Toolkit: QOL"),
        args = {
            desc = {
                type = "description",
                name = "Quality-of-life enhancements for World of Warcraft.\n\nVersion: " .. version .. "\nMode: " .. provider .. "\nStatus: Ready\n",
                fontSize = "medium",
                order = 1,
            },
            openApp = {
                type = "execute",
                name = "Open Sin's Toolkit",
                desc = "Launch the Sin's Toolkit standalone application.",
                order = 2,
                func = function()
                    if SettingsPanel and SettingsPanel:IsShown() then SettingsPanel:Hide() end
                    if InterfaceOptionsFrame and InterfaceOptionsFrame:IsShown() then InterfaceOptionsFrame:Hide() end
                    if SinsToolkit.Window then SinsToolkit.Window:Show() end
                end,
            },
            resetMinimap = {
                type = "execute",
                name = "Reset Minimap Position",
                desc = "Reset the main minimap button to its default position.",
                order = 3,
                func = function()
                    if not (SinsToolkitDB and SinsToolkitDB.minimap) then return end

                    SinsToolkitDB.minimap.minimapPos = 225
                    local DBIcon = LibStub and LibStub("LibDBIcon-1.0", true)
                    if DBIcon then
                        DBIcon:Refresh(addonName, SinsToolkitDB.minimap)
                    end
                    print("|cff9b59b6[Sin's Toolkit: QOL]|r Minimap button position reset.")
                end,
            },
        },
    }
end
