local battleGeneral = require("scripts/battleGeneral")

local onlinePlay = require("scripts/onlinePlay")

local maskCollection

local music = {}


local musicFolderPath = Misc.levelFolder().. "maskCollection/resources/music/"

music.paths = {
    waves = {
        {
            musicFolderPath.. "demol1sh - Splatoon 3 Side Order.ogg",
            musicFolderPath.. "rout1nes - Splatoon 3 Side Order.ogg",
        },
        {
            musicFolderPath.. "demoli2h - Splatoon 3 Side Order.ogg",
            musicFolderPath.. "routine2 - Splatoon 3 Side Order.ogg",
            musicFolderPath.. "2ouvenir - Splatoon 3 Side Order.ogg",
        },
        {
            musicFolderPath.. "d3molish - Splatoon 3 Side Order.ogg",
            musicFolderPath.. "routin3s - Splatoon 3 Side Order.ogg",
            musicFolderPath.. "souv3nir - Splatoon 3 Side Order.ogg",
            musicFolderPath.. "jitt3rs - Splatoon 3 Side Order.ogg",
        },
    },
    waitingRoom = musicFolderPath.. "Upward - Splatoon 3 Side Order (Excerpt).ogg",
    results = musicFolderPath.. "Result (Defeat) - Splatoon 3 Side Order.ogg",
}


music.playingPath = nil
music.volumeModifier = 1
music.pitchModifier = 1


local function updateMusicVolume()
    Audio.MusicVolume(math.floor(51*music.volumeModifier + 0.5))
end

local function updateMusicPitch()
    Audio.MusicSetSpeed(music.pitchModifier)
end

local function updatePlayingMusic()
    if music.playingPath ~= nil and not battleGeneral.musicMuted() then
        Audio.MusicOpen(Misc.episodePath().. music.playingPath)
        Audio.MusicPlay()
    else
        Audio.MusicStop()
    end
end


function music.change(newMusic)
    music.playingPath = newMusic
    music.pitchModifier = 1

    if maskCollection.activeOnClientside then
        updatePlayingMusic()
    end
end

function music.stop()
    music.playingPath = nil

    if maskCollection.activeOnClientside then
        updatePlayingMusic()
    end
end

function music.setVolume(volumeModifier)
    music.volumeModifier = volumeModifier or 1

    if maskCollection.activeOnClientside then
        updateMusicVolume()
    end
end

function music.setPitch(pitchModifier)
    music.pitchModifier = pitchModifier or 1

    if maskCollection.activeOnClientside then
        updateMusicPitch()
    end
end


function battleGeneral.onMusicMuteSet()
    if maskCollection.activeOnClientside then
        Audio.SeizeStream(-1)

        updateMusicVolume()
        updateMusicPitch()
        updatePlayingMusic()
    end
end


function music.onInitAPI()
    maskCollection = require("maskCollection/scripts/maskCollection")
    
    function maskCollection.onClientsideActivation()
        Audio.SeizeStream(-1)

        updatePlayingMusic()
        updateMusicPitch()
        updateMusicVolume()
    end

    function maskCollection.onClientsideDeactivation()
        Audio.MusicStop()
        Audio.MusicVolume(51)

        if not battleGeneral.musicMuted() then
            Audio.ReleaseStream(-1)
            playMusic(0)
        end
    end
end


return music