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

public class TheAudioManager : MonoBehaviour {

	public static TheAudioManager instance;
	public float timeToChange;
	public AudioMixer mixer;
	public AudioMixerGroup musicGameGroup, musicMenuGroup, musicFinaleGroup, sfxGroup, sfxElectricityGroup;
	public AudioSource sfx, sfxElectricity, sfxLongElectricity;
	public AudioSource musicGame, musicMenu, musicFinale;
	public AudioClip[] soundList;
	public AudioClip[] electricitySounds;
	public AudioClip clipMusicInGame, clipMusicMenu, clipMusicFinal;
	public AudioMixerSnapshot snapshotMenu, snapshotinGame, snapshotFinale;
	public float stunnedEffect;
	public float varPitch;

	void Awake()
	{
		if (instance == null) {
			instance = this;
			DontDestroyOnLoad(gameObject);
		} else {
			Destroy (gameObject);
		}
	}

	void Start()
	{
//		StunnedAudio (false);
		sfx.outputAudioMixerGroup = sfxGroup;
		sfxElectricity.outputAudioMixerGroup = sfxElectricityGroup;
		musicGame.outputAudioMixerGroup = musicGameGroup;
		musicMenu.outputAudioMixerGroup = musicMenuGroup;
		musicFinale.outputAudioMixerGroup = musicFinaleGroup;
		sfxLongElectricity.outputAudioMixerGroup = sfxElectricityGroup;
		timeToChange = 1f;
		PlayMusicMenu();


	}

	// SFX
	public void PlayUIButton()
	{
		sfx.pitch = 1f;
		sfx.PlayOneShot(soundList[0]);
	}
	public void PlayElectricity()
	{
		int electricityRange = electricitySounds.Length;
		PitchRandomizer ();
		sfxElectricity.PlayOneShot(electricitySounds[Random.Range (0, electricityRange)]);
	}
	public void PlayLongElectricity()
	{
		PitchRandomizer ();
		sfxLongElectricity.Play ();
	}

	public void PlayCounterNumber()
	{
		sfx.PlayOneShot (soundList [2]);
	}

	public void PlayCounterGo()
	{
		sfx.PlayOneShot (soundList [3]);
	}

	// MUSIC
	public void PlayMusicInGame()
	{
		musicGame.clip = clipMusicInGame;
		musicGame.Play ();
		snapshotinGame.TransitionTo(timeToChange);
	}
	public void PlayMusicMenu()
	{
		musicMenu.clip = clipMusicMenu;
		musicMenu.Play ();
		snapshotMenu.TransitionTo (timeToChange);
	}
	public void PlayMusicFinal()
	{
		musicFinale.clip = clipMusicFinal;
		musicFinale.Play();
		snapshotFinale.TransitionTo (timeToChange);
	}

	// OTHER
//	public void StunnedAudio(bool state) {
//		if (state)
//			mixer.SetFloat ("FlangeMix", 0.75f);
//		if (!state)
//			mixer.SetFloat ("FlangeMix", 0f);
//	}
	public void PitchRandomizer() {
		float randomPitch;
		randomPitch = Random.Range (1 - varPitch, 1 + varPitch);
		sfx.pitch = randomPitch;
	}
}