using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour 
{
	//music loops
	public AudioSource chord1;
	public AudioSource chord2;
	public AudioSource drum1;
	public AudioSource drum2;
	public AudioSource drum3;

	//sfx
	public AudioSource newrule;
	public AudioSource breakSteam;
	public AudioSource mechMove;
	public AudioSource soundSpark;

	//input sfx
	public AudioSource button1;
	public AudioSource button2;
	public AudioSource button3;
	public AudioSource button4;


	//private
	private int currentEscalation = 0;

	//escalation reference
	//0 chord1
	//1 chord1 + drum1
	//2 chord1 + drum2
	//3 chord2 + drum2
	//4 chord2 + drum3

	void Start()
	{
		setCurrentEscalation (0);
	}

	public void setCurrentEscalation(int newEscalation)
	{
		currentEscalation = newEscalation;
		switch (currentEscalation)
		{
		case 0: //0 chord1
			drum1.loop = false;
			drum2.loop = false;
			drum3.loop = false;

			if (chord2.isPlaying)
			{
				chord2.loop = false;
				chord1.PlayDelayed(chord2.clip.length - chord2.time);
			}
			else if(!chord1.isPlaying)
			{
				chord1.loop = true;
				chord1.Play ();
			}
			break;

		case 1: //1 chord1 + drum1
			drum1.loop = true;
			drum2.loop = false;
			drum3.loop = false;

			if (chord2.isPlaying)
			{
				chord2.loop = false;
				chord1.PlayDelayed(chord2.clip.length - chord2.time);
				if(!drum1.isPlaying)
				{
					drum1.PlayDelayed(chord2.clip.length - chord2.time);
				}
			}
			else if(!chord1.isPlaying)
			{
				chord1.loop = true;
				chord1.Play ();
			}
			else
			{
				if(!drum1.isPlaying)
				{
					drum1.PlayDelayed(chord1.clip.length - chord1.time);
				}
			}
			break;

		case 2: //2 chord1 + drum2
			drum1.loop = false;
			drum2.loop = true;
			drum3.loop = false;
			
			if (chord2.isPlaying)
			{
				chord2.loop = false;
				chord1.PlayDelayed(chord2.clip.length - chord2.time);
				if(!drum2.isPlaying)
				{
					drum2.PlayDelayed(chord2.clip.length - chord2.time);
				}
			}
			else if(!chord1.isPlaying)
			{
				chord1.loop = true;
				chord1.Play ();
			}
			else
			{
				if(!drum2.isPlaying)
				{
					drum2.PlayDelayed(chord1.clip.length - chord1.time);
				}
			}
			break;

		case 3: //3 chord2 + drum2
			drum1.loop = false;
			drum2.loop = true;
			drum3.loop = false;
			chord2.loop = true;

			if (chord1.isPlaying)
			{
				chord1.loop = false;
				chord2.PlayDelayed(chord1.clip.length - chord1.time);
				if(!drum2.isPlaying)
				{
					drum2.PlayDelayed(chord1.clip.length - chord1.time);
				}
			}
			else if(!chord2.isPlaying)
			{
				chord2.Play ();
			}
			else
			{
				if(!drum2.isPlaying)
				{
					drum2.PlayDelayed(chord2.clip.length - chord2.time);
				}
			}
			break;

		case 4: //4 chord2 + drum3
			drum1.loop = false;
			drum2.loop = false;
			drum3.loop = true;
			chord2.loop = true;
			
			if (chord1.isPlaying)
			{
				chord1.loop = false;
				chord2.PlayDelayed(chord1.clip.length - chord1.time);
				if(!drum3.isPlaying)
				{
					drum3.PlayDelayed(chord1.clip.length - chord1.time);
				}
			}
			else if(!chord2.isPlaying)
			{
				chord2.Play ();
			}
			else
			{
				if(!drum3.isPlaying)
				{
					drum3.PlayDelayed(chord1.clip.length - chord1.time);
				}
			}
			break;

		default:
			break;
		}
	}

}
