using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour {
	
	public static SoundManager instance = null;

	public AudioClip[] audio;
	public AudioClip musicToChange;

	private AudioClip music;
	private AudioSource Track;
	private bool transition = false;
	private AudioSource player;
	private Animator anim;

	public void PlaySound(int i){
		player.PlayOneShot (audio [i]);
	}

	public void ChangeMusic(){
		if (transition)
			return;
		transition = true;
		StartCoroutine (MusicChange ());
	}

	IEnumerator MusicChange(){
		anim.SetTrigger ("Fade Out");
		yield return new WaitForSeconds (2f);
		Track.clip = musicToChange ;
		musicToChange = music;
		music = Track.clip;
		Track.Play ();
		anim.SetTrigger ("Fade In");
		yield return new WaitForSeconds (.5f);
		transition = false;
	}

	// Use this for initialization
	void Start () {
		DontDestroyOnLoad (gameObject);
		Track = GetComponent<AudioSource> ();
		music = Track.clip;
		anim = GetComponent<Animator> ();
		player = GetComponent<AudioSource> ();
		instance = this;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
