//CD Changable Music Toggle
local cv_cdmusic = CV_RegisterVar({
    name = "cdmusic",
    defaultvalue = "us",
    flags = CV_SHOWMODIF,
    PossibleValue = {off = 0, us = 1, jp = 2, whispmlem = 3}
})

//CD Level Music Toggle
local cv_cdlevels = CV_RegisterVar({
    name = "cdlevels",
    defaultvalue = "0",
    flags = CV_SHOWMODIF,
    PossibleValue = CV_OnOff
})

//Custom CD Level Music
local replaceableMusic = {
    GFZ1 = "CDGFZ1",
	GFZ2 = "CDGFZ2",
	THZ1 = "CDTHZ1",
	THZ2 = "CDTHZ2",
	DSZ1 = "CDDSZ1",
	DSZ2 = "CDDSZ2",
	CEZ1 = "CDCEZ1",
	CEZ2 = "CDCEZ2",
	ACZ1 = "CDACZ1",
	ACZ2 = "CDACZ2",
	RVZ1 = "CDRVZ1",
	ERZ1 = "CDERZ1",
	ERZ2 = "CDERZ2",
	BCZ1 = "CDBCZ1",
    VSMETL = "CDMETL",
    VSBRAK = "CDBRAK"
}

addHook("MusicChange", function(oldname, newname, pos)
    -- Check if there's no active player or if the active player's skin isn't "cdsonic"
	for i, player in ipairs({consoleplayer, secondarydisplayplayer})
		if not consoleplayer or skins[consoleplayer.skin].name ~= "cdsonic" then
			continue -- If conditions aren't met, exit the function
		end

    -- Check the value of the ConVar "cv_cdlevels" and exit if it's not equal to 1
		if cv_cdlevels.value ~= 1 then
			continue
		end

    -- Retrieve replacement music for the given 'newname' from the 'replaceableMusic' table
		local replacemusic = replaceableMusic[newname]

    -- If replacement music exists for the 'newname'
		if replacemusic then
        -- Return the replacement music along with other parameters to control music playback
			return replacemusic, 0, loop, pos, 0, 0
		end
	end
end)

//Changable CD Music Table
local usMusic = {
	_super = "CDUSPR",
	_inv = "CDUINV",
	_shoes = "CDUSPD",
	_clear = "CDUCLR",
	VSBOSS = "CDUBSS",
	SPEC1 = "CDUSPCL",
	SPEC2 = "CDUSPCL",
	SPEC3 = "CDUSPCL",
	SPEC4 = "CDUSPCL",
	SPEC5 = "CDUSPCL",
	SPEC6 = "CDUSPCL",
	SPEC7 = "CDUSPCL",
	SPEC8 = "CDUSPCL"
}
local jpMusic = {
	_super = "CDJSPR",
	_inv = "CDJINV",
	_shoes = "CDJSPD",
	_clear = "CDJCLR",
	VSBOSS = "CDJBSS",
	SPEC1 = "CDJSPCL",
	SPEC2 = "CDJSPCL",
	SPEC3 = "CDJSPCL",
	SPEC4 = "CDJSPCL",
	SPEC5 = "CDJSPCL",
	SPEC6 = "CDJSPCL",
	SPEC7 = "CDJSPCL",
	SPEC8 = "CDJSPCL"
}
local mlemMusic = {
	_super = "CDUSPR",
	_inv = "CDUINV",
	_shoes = "CDUSPD",
	_clear = "CDJCLR",
	VSBOSS = "CDJBSS",
	SPEC1 = "CDJSPCL",
	SPEC2 = "CDJSPCL",
	SPEC3 = "CDJSPCL",
	SPEC4 = "CDJSPCL",
	SPEC5 = "CDJSPCL",
	SPEC6 = "CDJSPCL",
	SPEC7 = "CDJSPCL",
	SPEC8 = "CDJSPCL"
}

//Custom CD Changable Music
addHook("MusicChange", function(oldname, newname)
    -- Check if there's an active console player
    if consoleplayer then
        local character = skins[consoleplayer.skin].name
        
        -- Check if the character is "cdsonic" and if cv_cdmusic is defined
        if character == "cdsonic" and cv_cdmusic then
            local cdMusicType = cv_cdmusic.value
            -- Music lookup based on different cv_cdmusic values
			if cdMusicType == 0 then
				return
            elseif cdMusicType == 1 then
                -- Check if the music is different and exists in usMusic table
                if oldname ~= newname then
                    local cdus = usMusic[newname]
                    if cdus then
                        return S_MusicName() == cdus or cdus
                    end
                end
            elseif cdMusicType == 2 then
                -- Check if music exists in jpMusic table
                local cdjp = jpMusic[newname]
                if cdjp then
                    return S_MusicName() == cdjp or cdjp
                end
            elseif cdMusicType == 3 then
                -- Check if music exists in mlemMusic table
                local cdmlem = mlemMusic[newname]
                if cdmlem then
                    return S_MusicName() == cdmlem or cdmlem
                end
            end
        end
    end
end)