using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[Prefab]
public class SoundController : Singleton<SoundController> {

    public AudioSource musicAudioSource, sfxAudioSource;
    public string currentMusic;
    public AudioAssets audioAssets;
    public Dictionary<string, AudioClip> sfxNames;

    void Awake() {
        sfxNames = new Dictionary<string, AudioClip>();
        foreach (AudioClip a in audioAssets.musics) sfxNames[a.name] = a;
        foreach (AudioClip a in audioAssets.soundEffects) sfxNames[a.name] = a;
        DontDestroyOnLoad(gameObject);
    }

    public void playSfx(string sfxName) {
        if (sfxNames.ContainsKey(sfxName)) {
            sfxAudioSource.PlayOneShot(sfxNames[sfxName]);
        }
    }

    public void playMusic(string bgmusic) {
        if (sfxNames.ContainsKey(bgmusic)) {
            musicAudioSource.clip = sfxNames[bgmusic];
            musicAudioSource.loop = true;
            musicAudioSource.Play();
        }
    }

    public void stopMusic() {
        musicAudioSource.Stop();
    }

    public void pauseMusic() {
        musicAudioSource.Pause();
    }

    public void unpauseMusic() {
        musicAudioSource.UnPause();
    }
}
