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

public class AudioMgr : MonoBehaviour {

	Dictionary<string, AudioSource> sources = new Dictionary<string, AudioSource>();

	string currTheme;

	[System.Serializable]
	public class Theme
	{
		public AudioClip croud;
		public AudioClip fisa;
		public AudioClip drum;
		public AudioClip bass;
		public string ID;
	}

	public Theme[] themes;
	public static AudioMgr instance;
	public AudioMixer masterMixer;
	public float loadThemeCoolDown;

	float currCoolDown;

	string[] themesAvailable;

	void Awake () {
		if(instance == null) {
			instance = this;
		}
		themesAvailable = new [] {"b01", "b02", "b03", "b04"} ;
		currCoolDown = 0;
		AudioSource[] _sources;
		_sources = GetComponentsInChildren<AudioSource>();
		for(int i=0; i<_sources.Length; i++) {
			sources.Add(_sources[i].gameObject.name, _sources[i]);
		}
		currTheme = "b01";

		//playTheme("b01");
	}

	void Start() {
		muteAllThemes();
		playTheme("b01");
	}

	public void playTheme(string _theme) {
		Debug.Log("Stopping: " + currTheme + " " + "Playing: " + _theme);
		masterMixer.SetFloat(currTheme + "_master", -80f);
		currTheme = _theme;
		masterMixer.SetFloat(currTheme + "_master", 0f);
	}

	public void initGame() {
		masterMixer.SetFloat(currTheme + "_master", 0f);
		setBassVolume(0f);
		setDrumVolume(0f);
		setFisaVolume(0f);
	}

	public void muteAllThemes() {
		muteTheme("b01");
		muteTheme("b02");
		muteTheme("b03");
		muteTheme("b04");
	}

	public void muteTheme(string id) {
		Debug.Log("Muting: " + id + "_master");
		masterMixer.SetFloat(id + "_master", -80f);
	}

	public void muteTrack(string bandComponentTAG) {
		Debug.Log("Muting " + bandComponentTAG);
		if(bandComponentTAG.Equals("Guitar")) {
			setBassVolume(-80f);
		}
		if(bandComponentTAG.Equals("Drum")) {
			setDrumVolume(-80f);
		}
		if(bandComponentTAG.Equals("Singer")) {
			setFisaVolume(-80f);
		}
	}

	public void onPause() {
		setLowPassFreq(600f);
	}

	public void onGame() {
		setLowPassFreq(22000f);
	}

	public void setCroudVolume(float vol) {
		for(int i=0; i<themesAvailable.Length; i++) {
			masterMixer.SetFloat(themesAvailable[i] + "_croudV", vol);
		}
	}

	public void setBassVolume(float vol) {
		for(int i=0; i<themesAvailable.Length; i++) {
			masterMixer.SetFloat(themesAvailable[i] + "_bassV", vol);
		}
	}

	public void setDrumVolume(float vol) {
		for(int i=0; i<themesAvailable.Length; i++) {
			masterMixer.SetFloat(themesAvailable[i] + "_drumV", vol);
		}
	}

	public void setFisaVolume(float vol) {
		for(int i=0; i<themesAvailable.Length; i++) {
			masterMixer.SetFloat(themesAvailable[i] + "_fisaV", vol);
		}
	}

	public void setLowPassFreq(float freq) {
		masterMixer.SetFloat("lowPassFreq", freq);
	}

	void Update () {
		currCoolDown += Time.deltaTime;
		if(currCoolDown >= loadThemeCoolDown) {
			currCoolDown = 0f;
			int random = Random.Range(0, themesAvailable.Length);
			if(!themesAvailable[random].Equals(currTheme)) {
				playTheme(themesAvailable[random]);
			}
		}
	}
}
