using UnityEngine;
using System.Collections;
public class MusicManager : MonoBehaviour	// 3D sound/music is fucked! (Turn left/right to check.)
{
	public AudioClip mainMenuMusic;
	public AudioClip lowLevelMusic;
	private AudioSource audioSource;
	private float volumeIncrement = 0.05f;//0.0125f;
	private float maxVolume = 1.0f;//0.25f;			// Non-3D music got way louder for some reason. I have no idea why.
	private float minVolume = 0.0f;
	private bool isFacingOutMusic = false;
	void Start ()
	{
		//DontDestroyOnLoad(this);	// Need a persistent AudioSource to do this properly!
		if (Application.loadedLevel == 0)
		{
			playMainMenuMusic();
		}
		else if (Application.loadedLevel == 1)
		{
			//if (GameObject.Find("_Player").GetComponent<PlayerControl>().level <= 4)
			//{
				//if (audioSource == null	||
				//	audioSource.clip != lowLevelMusic)
				//{
					playLowLevelMusic();
				//}
			/*}
			else
			{
				//if (audioSource == null	||
				//	audioSource.clip != highLevelMusic)
				//{
					playHighLevelMusic();
				//}
			}*/
		}
	}
	void Update ()
	{
		GameObject player = GameObject.Find("_Player");
		if (player != null)
		{
			PlayerControl pc = GameObject.Find("_Player").GetComponent<PlayerControl>();
			if (pc != null	&&
				pc.currentHealth <= 0	&&
				!isFacingOutMusic)
			{
				toggleFadingMusicStatus();
				fadeOutMusic();
			}
		}
	}
	public void increaseMusicVolume ()
	{
		if (audioSource.volume < maxVolume)
		{
			float volume = audioSource.volume + volumeIncrement;
			if (volume > maxVolume)
			{
				volume = maxVolume;
			}
			audioSource.volume = volume;
		}
	}
	public void decreaseMusicVolume ()
	{
		if (audioSource.volume > minVolume)
		{
			float volume = audioSource.volume - volumeIncrement;
			if (volume < minVolume)
			{
				volume = minVolume;
			}
			audioSource.volume = volume;
		}
	}
	public void fadeInMusic ()
	{
		audioSource.volume = 0.0f;
		audioSource.Play();
		for (int i = 1; i <= (int)(1.0f / volumeIncrement); i++)
		{
			Invoke("increaseMusicVolume", i * 0.1f);
		}
		Invoke("toggleFadingMusicStatus", 1.0f / volumeIncrement);
	}
	public void fadeOutMusic ()
	{
		for (int i = 1; i <= (int)(1.0f / volumeIncrement); i++)
		{
			Invoke("decreaseMusicVolume", i * 0.1f);
		}
	}
	public void toggleFadingMusicStatus ()
	{
		isFacingOutMusic = !isFacingOutMusic;
	}
	public void playMainMenuMusic ()
	{
		audioSource = gameObject.AddComponent<AudioSource>();
		audioSource.loop = true;
		audioSource.clip = mainMenuMusic;
		fadeInMusic();
	}
	public void playLowLevelMusic ()
	{
		audioSource = GameObject.Find("_Player").GetComponent<AudioSource>();
		audioSource.loop = true;
		audioSource.clip = lowLevelMusic;
		fadeInMusic();
	}
	/*public void playHighLevelMusic ()
	{
		audioSource = GameObject.Find("_Player").GetComponent<AudioSource>();
		audioSource.loop = true;
		audioSource.clip = highLevelMusic;
		fadeInMusic();
	}
	public void playBossLevelMusic ()
	{
		audioSource = GameObject.Find("_Player").GetComponent<AudioSource>();
		audioSource.loop = true;
		audioSource.clip = bossMusic;
		fadeInMusic();
	}*/
}