#include "SoundManager.h"
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace kiwano
{
    namespace audio
    {

        SoundManager::SoundManager()
            : volume_rate(1.f)
        {
        }

        SoundManager::~SoundManager()
        {
            ClearCache();
        }

        size_t SoundManager::GetId(const String& file_path) const
        {
            return std::hash<String>()(file_path);
        }

        size_t SoundManager::GetId(const Resource& res) const
        {
            return static_cast<size_t>(res.GetId());
        }

        size_t SoundManager::Load(const String& file_path)
        {
            size_t id = GetId(file_path);
            if (sound_cache_.end() != sound_cache_.find(id))
                return id;

            SoundPtr sound = MakePtr<Sound>(file_path);
            if (sound)
            {
                sound->SetVolume(volume_rate * 1.0);
                sound_cache_.insert(std::make_pair(id, sound));
                return id;
            }
            return 0;
        }

        size_t SoundManager::Save_Load(const String& file_path)
        {
            size_t id = GetId(file_path);
            if (sound_save_.end() != sound_save_.find(id))
                return id;

            SoundPtr sound = MakePtr<Sound>(file_path);
            if (sound)
            {
                sound->SetVolume(volume_rate * 1.0);
                sound_save_.insert(std::make_pair(id, sound));
                return id;
            }
            return 0;
        }

        size_t SoundManager::Load(const Resource& res)
        {
            size_t id = GetId(res);
            if (sound_cache_.end() != sound_cache_.find(id))
                return id;

            SoundPtr sound = MakePtr<Sound>(res);

            if (sound)
            {
                sound->SetVolume(volume_rate * 1.0);
                sound_cache_.insert(std::make_pair(id, sound));
                return id;
            }
            return 0;
        }

        void SoundManager::Play(size_t id, int loop_count)
        {
            if (auto sound = GetSound(id))
                sound->Play(loop_count);
        }

        void SoundManager::Pause(size_t id)
        {
            if (auto sound = GetSound(id))
                sound->Pause();
        }

        void SoundManager::Resume(size_t id)
        {
            if (auto sound = GetSound(id))
                sound->Resume();
        }

        void SoundManager::Stop(size_t id)
        {
            if (auto sound = GetSound(id))
                sound->Stop();
        }

        bool SoundManager::IsPlaying(size_t id)
        {
            if (auto sound = GetSound(id))
                return sound->IsPlaying();
            return false;
        }

        float SoundManager::GetVolumeRate() const
        {
            return volume_rate;
        }

        void SoundManager::SetVolumeRate(float volume)
        {
            volume_rate = std::min(std::max(volume, -224.f), 224.f);
            for (auto& pair : sound_cache_)
            {
                pair.second->SetVolume(volume_rate);
            }
        }

        SoundPtr SoundManager::GetSound(size_t id) const
        {
            auto iter = sound_cache_.find(id);
            if (iter != sound_cache_.end())
                return iter->second;
            return SoundPtr();
        }

        SoundPtr SoundManager::GetSaveSound(size_t id) const
        {
            auto iter = sound_save_.find(id);
            if (iter != sound_save_.end())
                return iter->second;
            return SoundPtr();
        }

        void SoundManager::PauseAll()
        {
            for (auto& pair : sound_cache_)
            {
                pair.second->Pause();
            }
        }

        void SoundManager::ResumeAll()
        {
            for (auto& pair : sound_cache_)
            {
                pair.second->Resume();
            }
        }

        void SoundManager::StopAll()
        {
            for (auto& pair : sound_cache_)
            {
                pair.second->Stop();
            }
        }

        void SoundManager::ClearCache()
        {
            sound_cache_.clear();
        }

        void SoundManager::ReleaseSoundByIndex(const size_t Id)
        {
            auto iter = sound_cache_.find(Id);
            if (iter != sound_cache_.end()) {
                iter->second->Close();
                sound_cache_.erase(Id);
            }
        }

        void SoundManager::ReleaseSaveSoundByIndex(const size_t Id)
        {
            auto iter = sound_save_.find(Id);
            if (iter != sound_save_.end()) {
                iter->second->Close();
                sound_save_.erase(Id);
            }
        }

        Map<size_t, SoundPtr> SoundManager::GetAllSound()
        {
            Map<size_t, SoundPtr> Buf;
            Buf.insert(sound_cache_.begin(), sound_cache_.end());
            return Buf;
        }
    }  // namespace audio
}  // namespace kiwano
