local versao_trilha = "2.4"

local legacy_tabs = {
    ["demo1"] = { [1] = "GFZ2", [2] = "THZ1", [3] = "VSBOSS", [4] = "CCZ" },
    ["demo2"] = { [1] = "GFZ2", [2] = "THZ1", [3] = "VSBOSS", [4] = "CCZ", [5] = "FHZ", [6] = "VSBOSS", [7] = "ACZ1", [8] = "ACZ2" },
    ["demo2.1"] = { [1] = "GFZ2", [2] = "THZ1", [3] = "VSBOSS", [4] = "CCZ", [5] = "FHZ", [6] = "VSBOSS", [7] = "ACZ1", [8] = "ACZ2" },
    ["demo2.2"] = { [1] = "GFZ2", [2] = "THZ1", [3] = "VSBOSS", [4] = "CCZ", [5] = "FHZ", [6] = "VSBOSS", [7] = "ACZ1", [8] = "ACZ2" },
    ["demo2.3"] = { [1] = "SGZ123", [2] = "SGZ223", [3] = "VSEGGMAN23", [4] = "SCZ123", [5] = "SCZ223", [6] = "VSEGGMAN23", [7] = "DTZ123", [8] = "DTZ223" }
}

local function AplicarMusicaLegacy(p)
    if versao_trilha == "2.4" then return end

    if gamestate == GS_LEVEL then
        local num_fase = gamemap
        if legacy_tabs[versao_trilha] and legacy_tabs[versao_trilha][num_fase] then
            S_ChangeMusic(legacy_tabs[versao_trilha][num_fase], true, p)
        end
    end
end

addHook("MapLoad", function()
    for p in players.iterate do AplicarMusicaLegacy(p) end
end)

COM_AddCommand("stscamusic", function(p, arg)
    if not arg then
        CONS_Printf(p, "Use: stscamusic <demo1 | demo2 | demo2.1 | demo2.2 | demo2.3 | original>")
        return
    end
    local v = arg:lower()
    if v == "original" or v == "2.4" then
        versao_trilha = "2.4"
        S_ChangeMusic(mapmusname, true, p)
        CONS_Printf(p, "\x82".."Original Track Activated!")
    elseif legacy_tabs[v] or menu_tabs[v] then
        versao_trilha = v
        CONS_Printf(p, "\x82".."Track " .. v .. " Activated!")
        AplicarMusicaLegacy(p)
    else
        CONS_Printf(p, "Invalid Version.")
    end
end)