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

public class TM_MusicTrack
{
    public string strName;
    public AudioClip music;
    public bool bShouldLoop;
}

public class TM_AudioManager : MonoBehaviour
{
    public static List<TM_MusicTrack> musicTracks;

    private static float fSFXVolume = 0.5f;
    private static float fMusicVolume = 1.0f;

    public static AudioSource mainMusicSource;

    private static TM_AudioManager _instance;


    private static TM_AudioManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<TM_AudioManager>();

                if (_instance != null)
                {
                    return _instance;
                }

                GameObject go = new GameObject("TM_AudioManager");
                _instance = go.AddComponent<TM_AudioManager>();
            }

            return _instance;
        }
    }

	public static void Start ()
	{
	}
	
    public static void Update()
    {
	}

    public static void ChangeMusicVolume(float fNewVolume)
    {
        fMusicVolume = fNewVolume;
        mainMusicSource.volume = fNewVolume;
    }

    public static void ChangeSFXVolume(float fNewVolume)
    {
        fSFXVolume = fNewVolume;
    }

    public static float GetSFXVolume() { return fSFXVolume; }
    public static float GetMusicVolume() { return fMusicVolume; }

    public static void PlayClip(AudioClip clip, AudioSource source)
    {
        if (source == null || clip == null)
            return;

        source.volume = fSFXVolume;
        source.pitch = Random.Range(0.9f, 1.1f);
        source.PlayOneShot(clip);
    }

    public static void PlayClip(AudioClip clip)
    {
        if (clip == null)
            return;

        GameObject garbagecode = new GameObject();
        AudioSource source = garbagecode.AddComponent<AudioSource>();
        source.volume = fSFXVolume;
        source.pitch = Random.Range(0.9f, 1.1f);
        source.PlayOneShot(clip);

        Destroy(garbagecode,clip.length + 0.1f);
    }
    
    /*

    public void FadeAndSuspendStageMusic(float fFadeTime )
    {
        StartCoroutine(FadeAndSuspendStageMusicInternal(fFadeTime));
    }

    public void FadeOutMusic()
    {
        StartCoroutine(FadeOutMusicInternal());
    }

    private IEnumerator FadeOutMusicInternal()
    {
        float t = mainMusicSource.volume;
        while (t > 0.0f)
        {
            t -= Time.deltaTime;
            mainMusicSource.volume = t;
            yield return null;
        }

        mainMusicSource.Stop();
        playingMusicFlag = EMusicCurrentlyPlaying.none;
    }

    private IEnumerator FadeAndSuspendStageMusicInternal(float fFadeTime)
    {
        float f = 0f;
        while (f < fFadeTime)
        {
            f += Time.deltaTime;
            mainMusicSource.volume = Mathf.Lerp((float)(iMusicVolume / 100.0f), 0.0f, f / fFadeTime);
            yield return null;
        }

        mainMusicSource.Pause();
        mainMusicSource.transform.localScale = new Vector3(mainMusicSource.time,0,0);
        playingMusicFlag = EMusicCurrentlyPlaying.none;
    }

    public void FadeMusicStageMusicBack(float fFadeTime)
    {
        StartCoroutine(FadeMusicStageMusicBackInternal(fFadeTime));
    }

    private IEnumerator FadeMusicStageMusicBackInternal(float fFadeTime)
    {
        mainMusicSource.Stop();
        mainMusicSource.volume = 0f;
        mainMusicSource.clip = mainTheme;
        mainMusicSource.time = mainMusicSource.transform.localScale.x;
        mainMusicSource.Play();

        float f = 0.0f;
        while (f < fFadeTime)
        {
            f += Time.deltaTime;
            mainMusicSource.volume = Mathf.Lerp(0.0f, (float) (iMusicVolume/100.0f), f/fFadeTime);
            yield return null;
        }
    }

    private void PlayMusicInternal( AudioClip music, EMusicCurrentlyPlaying flag)
    {
        if (mainMusicSource.isPlaying)
            mainMusicSource.Stop();

        playingMusicFlag = flag;
        mainMusicSource.volume = iMusicVolume / 100.0f;
        mainMusicSource.clip = music;
        mainMusicSource.loop = true;
        mainMusicSource.time = 0f;
        mainMusicSource.Play();
    }
    */
}
