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

public enum AudioSound {
	Flap1,
	Flap2,
	Flap3,
	Flap4,
	Flap5,
	Flap6,
	Flap7,
	Flap8,
	Flap9,
	Flap10
}

public class AudioSystem : MonoBehaviour {
	public AudioClip[] audioResources = new AudioClip[25];
	public List<AudioSource> audioSources = new List<AudioSource>();

	public static AudioSystem instance;

	void Start () {
		instance = this;

		audioResources[(int)AudioSound.Flap1] = Resources.Load<AudioClip>("Audio/Flapping/flapping1");
		audioResources[(int)AudioSound.Flap2] = Resources.Load<AudioClip>("Audio/Flapping/flapping2");
		audioResources[(int)AudioSound.Flap3] = Resources.Load<AudioClip>("Audio/Flapping/flapping3");
		audioResources[(int)AudioSound.Flap4] = Resources.Load<AudioClip>("Audio/Flapping/flapping4");
		audioResources[(int)AudioSound.Flap5] = Resources.Load<AudioClip>("Audio/Flapping/flapping5");
		audioResources[(int)AudioSound.Flap6] = Resources.Load<AudioClip>("Audio/Flapping/flapping6");
		audioResources[(int)AudioSound.Flap7] = Resources.Load<AudioClip>("Audio/Flapping/flapping7");
		audioResources[(int)AudioSound.Flap8] = Resources.Load<AudioClip>("Audio/Flapping/flapping8");
		audioResources[(int)AudioSound.Flap9] = Resources.Load<AudioClip>("Audio/Flapping/flapping9");
		audioResources[(int)AudioSound.Flap10] = Resources.Load<AudioClip>("Audio/Flapping/flapping10");
	}
	
	void Update () {
		foreach(AudioSource source in new List<AudioSource>(audioSources)) {
			if (source.isPlaying == false) {
				Destroy(source.gameObject);
				audioSources.Remove(source);
			}
		}	
	}

	static public void Play(AudioSound sound) {
		if (instance == null) {
			return;
		}
		GameObject soundObject = new GameObject(sound.ToString());
		soundObject.transform.parent = instance.transform;

		AudioSource source = soundObject.AddComponent<AudioSource>();
		source.clip = instance.audioResources[(int)sound];
		source.volume = 0.5f;

		soundObject.SetActive(false);
		soundObject.SetActive(true);

		
		instance.audioSources.Add(source);
	}
}
