local ZMusicManager = {}

local tClipList = {}

local INIT_NUM = 10

local function InitBGM()
    local audioBGM = CS.UnityEngine.GameObject("BGM")
    ZMusicManager.audioBGM = audioBGM

    CS.UnityEngine.Object.DontDestroyOnLoad(audioBGM)
    audioBGM:AddComponent(typeof(CS.UnityEngine.AudioSource))
end

local function InitSound()
    local audioSoud = CS.UnityEngine.GameObject("Sound")
    ZMusicManager.audioSoud = audioSoud
    for i = 1, INIT_NUM, 1 do
        local audioSource = audioSoud:AddComponent(typeof(CS.UnityEngine.AudioSource))
        table.insert(tClipList, audioSource)
    end

    CS.UnityEngine.Object.DontDestroyOnLoad(audioSoud)
end

-- Music

function ZMusicManager:PlayMusic(szBGMPath)
    local audioSourceBGM = ZMusicManager.audioBGM:GetComponent("AudioSource")
    local BGM            = LoadResource(szBGMPath)
    audioSourceBGM.clip  = BGM
    audioSourceBGM.loop  = true
    audioSourceBGM:Play()
end

function ZMusicManager:PauseMusic()
    local audioSourceBGM = ZMusicManager.audioBGM:GetComponent("AudioSource")
    audioSourceBGM:Pause()
end

function ZMusicManager.UnPauseMusic()
    local audioSourceBGM = ZMusicManager.audioBGM:GetComponent("AudioSource")
    audioSourceBGM:UnPause()
end

function ZMusicManager:StopMusic()
    local audioSourceBGM = ZMusicManager.audioBGM:GetComponent("AudioSource")
    audioSourceBGM:Stop()
end

function ZMusicManager:SetMusicVolume(nVolume)
    local audioSourceBGM  = ZMusicManager.audioBGM:GetComponent("AudioSource")
    audioSourceBGM.volume = nVolume
end

-- Sound

function ZMusicManager:PlaySound(szSoundPath, bLoop)
    local soundSource = LoadResource(szSoundPath)
    for nIndex, audioSource in pairs(tClipList) do
        if not audioSource.isPlaying then
            tClipList[nIndex].clip      = soundSource
            tClipList[nIndex].clip.name = szSoundPath
            tClipList[nIndex].loop      = bLoop
            audioSource:Play();
            return
        end
    end
end

function ZMusicManager:StopSound(szSoundPath)
    if not ZMusicManager.audioSoud then
        return
    end

    for _, audioSource in pairs(tClipList) do
        if audioSource.isPlaying and audioSource.clip and audioSource.clip.name == szSoundPath then
            audioSource:Stop()
        end
    end
end

function ZMusicManager:PauseSound(szSoundPath)
    if not ZMusicManager.audioSoud then
        return
    end

    for _, audioSource in pairs(tClipList) do
        if audioSource.clip and audioSource.clip.name == szSoundPath then
            audioSource:Pause()
        end
    end
end

function ZMusicManager:UnPauseSound(szSoundPath)
    if not ZMusicManager.audioSoud then
        return
    end

    for _, audioSource in pairs(tClipList) do
        if audioSource.clip and audioSource.clip.name == szSoundPath then
            audioSource:UnPause()
        end
    end
end

function ZMusicManager:SetSoundVolume(szSoundPath, nVolume)
    if not ZMusicManager.audioSoud then
        return
    end

    for _, audioSource in pairs(tClipList) do
        if audioSource.clip and audioSource.clip.name == szSoundPath then
            audioSource.volume = nVolume
        end
    end
end

function ZMusicManager:Init()
    InitBGM()
    InitSound()
end

function ZMusicManager:UnInit()
end

return ZMusicManager