using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

public class UIManagerScript : MonoBehaviour {

	public Text waveType;
	public Button startButton;
	public Button replayButton;

	//battery fil
	public Image fillingBattery;

	//wave animation
	public Image monitorImage;
	public Sprite[] sineWaves;
	public Sprite[] squareWaves;
	public Sprite[] triangleWaves;
	private float switchTime = 0.5f;
	private float currentTimeBeforeSwitch = 0;
	private int currentWaveFrame = 0;

	// Use this for initialization
	void Start () {
		//startButton.gameObject.SetActive(true);
		replayButton.gameObject.SetActive(false);
		fillingBattery.type = Image.Type.Filled;
		fillingBattery.fillMethod = Image.FillMethod.Vertical;
		currentTimeBeforeSwitch = switchTime;

		//PauseGame ();
	}
	
	// Update is called once per frame
	void Update () 
	{
		waveType.text = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript> ().GetCurrentWaveType ().ToString ();
		float percentToFill = (float)GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript> ().BatteryCount () / (float)MaxBatteries();
		fillingBattery.fillAmount = percentToFill;

		if (GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript> ().isDead ())
		{
			PauseGame ();

			replayButton.gameObject.SetActive(true);
		}
	}

	void FixedUpdate()
	{
		AnimateWaveType (GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript> ().GetCurrentWaveType ());
	}

	public void PauseGame()
	{
		GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript> ().Pause ();

		GameObject[] enemies = GameObject.FindGameObjectsWithTag ("Enemy");
		for (int i = 0; i < enemies.Length; i++)
		{
			enemies [i].GetComponent<BasicEnemy> ().Pause ();
		}

		GameObject[] walls = GameObject.FindGameObjectsWithTag ("Wall");
		for (int i = 0; i < walls.Length; i++) 
		{
			walls [i].GetComponent<BasicWall> ().Pause ();
		}
	}

	public void StartGame()
	{
		GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript> ().UnPause ();

		GameObject[] enemies = GameObject.FindGameObjectsWithTag ("Enemy");
		for (int i = 0; i < enemies.Length; i++)
		{
			enemies [i].GetComponent<BasicEnemy> ().UnPause ();
		}

		GameObject[] walls = GameObject.FindGameObjectsWithTag ("Wall");
		for (int i = 0; i < walls.Length; i++)
		{ 
			walls [i].GetComponent<BasicWall> ().UnPause ();
		}

		startButton.gameObject.SetActive(false);
	}

	public void ReplayGame()
	{
		SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
	}

	public int MaxBatteries()
	{
		GameObject[] batteries = GameObject.FindGameObjectsWithTag ("Battery");
		return batteries.Length;
	}


	public void AnimateWaveType(GlobalEnums.WaveTypes wave)
	{
		currentTimeBeforeSwitch -= Time.fixedDeltaTime; 

		if (currentTimeBeforeSwitch <= 0)
		{
			currentWaveFrame++;
			if (wave == GlobalEnums.WaveTypes.Sin)
			{
				if (currentWaveFrame >= sineWaves.Length)
				{
					currentWaveFrame = 0;
				}
				monitorImage.sprite = sineWaves[currentWaveFrame];
			} 
			else if (wave == GlobalEnums.WaveTypes.Square)
			{
				if (currentWaveFrame >= squareWaves.Length) 
				{
					currentWaveFrame = 0;
				}
				monitorImage.sprite = squareWaves[currentWaveFrame];
			} 
			else if (wave == GlobalEnums.WaveTypes.Triangle) 
			{
				if (currentWaveFrame >= triangleWaves.Length) 
				{
					currentWaveFrame = 0;
				}
				monitorImage.sprite = triangleWaves[currentWaveFrame];
			}
			currentTimeBeforeSwitch = switchTime;
		}
	}
}
