local addonName = ...
local LSM = LibStub("LibSharedMedia-3.0")

--------------------------------------------------
-- Sound registration
--------------------------------------------------

local basePath = "Interface\\AddOns\\" .. addonName .. "\\sounds\\"

local sounds = {
    "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s",
    "astral_harvest", "break", "crystalline_shockwave",
    "inverse_gravity", "out", "pools", "reverse_gravity",
    "soak", "soulfire_convergence", "soulfray_annihilation",
    "spread", "starkiller_swing", "targeted", "dot",
}

for _, sound in ipairs(sounds) do
    LSM:Register("sound", "CCS: " .. sound, basePath .. sound .. ".ogg")
end

--------------------------------------------------
-- Generic Info Frame
--------------------------------------------------

local InfoFrame
local activeFontStrings = {}

local function ClearFrame()
    for _, fs in ipairs(activeFontStrings) do
        fs:Hide()
    end
    wipe(activeFontStrings)
end

local function PopulateFrame(data)
    ClearFrame()

    local yOffset = -30

    for _, entry in ipairs(data) do
        local fs = InfoFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        fs:SetPoint("TOPLEFT", 20, yOffset)
        fs:SetWidth(InfoFrame:GetWidth() - 40)
        fs:SetJustifyH("LEFT")
        fs:SetJustifyV("TOP")
        fs:SetWordWrap(true)
        fs:SetText(entry.text)

        if entry.bold then
            fs:SetFontObject("GameFontHighlightLarge")
        end

        yOffset = yOffset - fs:GetStringHeight() - 8
        table.insert(activeFontStrings, fs)
    end
end

local function ShowInfo(data)
    if not InfoFrame then
        InfoFrame = CreateFrame("Frame", "CCS_InfoFrame", UIParent, "BackdropTemplate")
        InfoFrame:SetSize(420, 750)
        InfoFrame:SetPoint("CENTER")
        InfoFrame:SetMovable(true)
        InfoFrame:EnableMouse(true)
        InfoFrame:RegisterForDrag("LeftButton")
        InfoFrame:SetScript("OnDragStart", InfoFrame.StartMoving)
        InfoFrame:SetScript("OnDragStop", InfoFrame.StopMovingOrSizing)

        InfoFrame:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8x8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true,
            tileSize = 8,
            edgeSize = 12,
            insets = { left = 2, right = 2, top = 2, bottom = 2 }
        })
        InfoFrame:SetBackdropColor(0, 0, 0, 0.92)

        local close = CreateFrame("Button", nil, InfoFrame, "UIPanelCloseButton")
        close:SetPoint("TOPRIGHT", -4, -4)
    end

    PopulateFrame(data)
    InfoFrame:Show()
end

--------------------------------------------------
-- Slash command handler
--------------------------------------------------

SLASH_CCS1 = "/ccs"

SlashCmdList["CCS"] = function(msg)
    local key = msg:match("^%s*(.-)%s*$"):lower()

    if key == "" then
        print("|cffff5555CCS usage:|r /ccs <name>")
        return
    end

    -- Convert "manaforge" → "CCS_Manaforge"
    local tableName = "CCS_" .. key:gsub("^%l", string.upper)
    local data = _G[tableName]

    if not data then
        print("|cffff5555CCS error:|r No data file found for '" .. key .. "'")
        return
    end

    ShowInfo(data)
end
