using UnityEngine;
using UnityEngine.UI;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class PlaySound : MonoBehaviour {
	public StepSequencer2 stepSequencer2;
	public int step;
	public bool playing = false;
	public AudioClip[] clips = new AudioClip[17];
	private AudioSource[] audioSources = new AudioSource[17];
	//private bool toggleMode1 = true;
	public UnityEngine.UI.Toggle toggleMode1;
	//public UnityEngine.UI.Toggle toggleMode2;
	//private myColor1 = new Color32(255, 0, 110, 255)

	// Use this for initialization
	void Start () {
		renderer.material.color = Color.white;
		int i = 0;
		while (i < 17) {
			GameObject child = new GameObject("Player");
			child.transform.parent = gameObject.transform;
			audioSources[i] = child.AddComponent("AudioSource") as AudioSource;
			i++;
			toggleMode1 = GameObject.Find ("ToggleMode1").GetComponent<UnityEngine.UI.Toggle>();
		}
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		if (stepSequencer2.flip != step && playing == true) {
			playing = false;
		}
		if (toggleMode1.isOn == true) { 
			if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 0, 110, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (192, 0, 130, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (128, 0, 170, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (64, 0, 210, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 38, 255, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 51, 204, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 102, 153, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 153, 102, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 204, 51, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 255, 33, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (64, 255, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (128, 255, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (192, 255, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 216, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 144, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 72, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 0, 0, 255) && playing == false) {
				audio.Play ();
				playing = true;
			}
		}
		if (toggleMode1.isOn == false) {
			if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 0, 110, 255) && playing == false) {
				audioSources [0].clip = clips [0];
				audioSources [0].volume = 1.0F;
				audioSources [0].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (192, 0, 130, 255) && playing == false) {
				audioSources [1].clip = clips [1];
				audioSources [1].volume = 0.3F;
				audioSources [1].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (128, 0, 170, 255) && playing == false) {
				audioSources [2].clip = clips [2];
				audioSources [2].volume = 1.0F;
				audioSources [2].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (64, 0, 210, 255) && playing == false) {
				audioSources [3].clip = clips [3];
				audioSources [3].volume = 1.0F;
				audioSources [3].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 38, 255, 255) && playing == false) {
				audioSources [4].clip = clips [4];
				audioSources [4].volume = 1.0F;
				audioSources [4].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 51, 204, 255) && playing == false) {
				audioSources [5].clip = clips [5];
				audioSources [5].volume = 1.0F;
				audioSources [5].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 102, 153, 255) && playing == false) {
				audioSources [6].clip = clips [6];
				audioSources [6].volume = 1.0F;
				audioSources [6].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 153, 102, 255) && playing == false) {
				audioSources [7].clip = clips [7];
				audioSources [7].volume = 1.0F;
				audioSources [7].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 204, 51, 255) && playing == false) {
				audioSources [8].clip = clips [8];
				audioSources [8].volume = 1.0F;
				audioSources [8].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (0, 255, 33, 255) && playing == false) {
				audioSources [9].clip = clips [9];
				audioSources [9].volume = 0.8F;
				audioSources [9].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (64, 255, 0, 255) && playing == false) {
				audioSources [10].clip = clips [10];
				audioSources [10].volume = 1.0F;
				audioSources [10].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (128, 255, 0, 255) && playing == false) {
				audioSources [11].clip = clips [11];
				audioSources [11].volume = 1.0F;
				audioSources [11].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (192, 255, 0, 255) && playing == false) {
				audioSources [12].clip = clips [12];
				audioSources [12].volume = 1.0F;
				audioSources [12].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 216, 0, 255) && playing == false) {
				audioSources [13].clip = clips [13];
				audioSources [13].volume = 1.0F;
				audioSources [13].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 144, 0, 255) && playing == false) {
				audioSources [14].clip = clips [14];
				audioSources [14].volume = 1.0F;
				audioSources [14].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 72, 0, 255) && playing == false) {
				audioSources [15].clip = clips [15];
				audioSources [15].volume = 0.2F;
				audioSources [15].Play ();
				playing = true;
			} else if (stepSequencer2.flip == step && renderer.material.color == new Color32 (255, 0, 0, 255) && playing == false) {
				audioSources [16].clip = clips [16];
				audioSources [16].volume = 0.2F;
				audioSources [16].Play ();
				playing = true;
			}
		}
	}
}