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

public class Mg_Audio : Singleton<Mg_Audio> 
{
    struct Sound
    {
        public AudioSource src;
        public bool queueForRemove;

        public void SetVolume(float f)
        {
            src.volume = f;
        }

        public void RemoveSound()
        {
            queueForRemove = true;
        }
    }

    Dictionary<string, AudioSource> _sounds = new Dictionary<string, AudioSource>();
    Dictionary<int, Sound> _activeSounds;

    private int currentSound = 0;

    void Awake()
    {
        //DontDestroyOnLoad(gameObject);
        //_sounds = new Dictionary<string, AudioSource>();
        _activeSounds = new Dictionary<int, Sound>();
    }

    void Start()
    {
        AudioSource[] sfxSources = GetComponentsInChildren<AudioSource>();
        for (int i = 0; i < sfxSources.Length; i++)
        {
            Debug.Log("sound " + sfxSources[i].name);

            _sounds[sfxSources[i].name] = sfxSources[i];
        }

        Debug.Log("total " + _sounds.Count);
    }

    public int Play(string name, float pitch = 1f, float volume = 0, bool looping = false)
    {
        AudioSource[] sfxSources = GetComponentsInChildren<AudioSource>();
        for (int i = 0; i < sfxSources.Length; i++)
        {
            Debug.Log("sound " + sfxSources[i].name);

            _sounds[sfxSources[i].name] = sfxSources[i];
        }
        Debug.Log("total " + _sounds.Count);

        //AudioSource src = gameObject.AddComponent< AudioSource > ();
        Sound snd = new Sound();
        snd.src = _sounds[name];
        snd.src.volume = _sounds[name].volume;
        snd.src.clip = _sounds[name].clip;
        snd.src.pitch = pitch;
        snd.src.loop = looping;
        snd.src.Play();
        int index = currentSound;

        snd.src.volume = 10f;

        _activeSounds[currentSound] = (snd);
        currentSound++;

        return index;
    }

    public void PlaySfx(string name)
    {
        //AudioSource src = gameObject.AddComponent< AudioSource > ();
        Sound snd = new Sound();
        snd.src = _sounds[name];
        snd.src.volume = 1f;
        snd.src.clip = _sounds[name].clip;
        snd.src.pitch = 1f;
        snd.src.loop = false;
        snd.src.Play();
    }

    public void StopLoop(int id)
    {
        if (_activeSounds.ContainsKey(id))
        {
            //_activeSounds[id].volume.FadeTo(0f, 3f);
            _activeSounds[id].RemoveSound();
        }
    }

    void LateUpdate()
    {
        List<int> buffer = new List<int>(_activeSounds.Keys);

        for (int i = 0; i < buffer.Count; i++)
        {
            if (!_activeSounds[buffer[i]].src.isPlaying)
            {
                _activeSounds.Remove(buffer[i]);
            }
            else if (_activeSounds[buffer[i]].queueForRemove)
            {
                if (_activeSounds[buffer[i]].src.volume <= 0f)
                {
                    _activeSounds[buffer[i]].src.Stop();
                    _activeSounds.Remove(buffer[i]);
                }
            }
        }
    }
}
