using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class GameManagerScript : MonoBehaviour {

	public int ShipDurability = 2000;
	public int KrakenHealth = 10000;
	//HP
	public int DamageTakenFromWaves = 500;
	public int DamageTakenFromEnemyRam = 100;
	public int DamageDealtByHarpoon = 150;
	public int DamageTakenFromBubble = 100;
	public int DamageTakenFromBashFish = 100;

	public int SwitchToPhase2AtThisHealth = 7000;
	public int SwitchToPhase3AtThisHealth = 3000;

	//	public bool ShouldSpawnWaves;

	public bool SpawnWaveLane1;
	public bool SpawnWaveLane2;
	public bool SpawnWaveLane3;


	public GameObject[] WavePrefabs;

	public GameObject SingleWavePrefab;

	public Transform WaveSpawnLocationObject;


	public Sprite[] ShipSprites;
	GameObject player;

	void Awake()
	{
		//ShipSprites = Resources.LoadAll<Sprite> ("ships");
	}

	// Use this for initialization
	void Start () {
		player = GameObject.Find ("Player");
		//ShouldSpawnWaves = false;
		SpawnWaveLane1 = false;
		SpawnWaveLane2 = false;
		SpawnWaveLane3 = false;
	}
	
	// Update is called once per frame
	void Update () {

		if (ShipDurability <= 0) 
		{
			SceneManager.LoadScene ("losescreen");
		}

		if (ShipDurability <= 1500 && ShipDurability > 1000) 
		{
			player.GetComponent<SpriteRenderer> ().sprite = ShipSprites [1];
		}
		else if (ShipDurability <= 1000 && ShipDurability > 500) 
		{
			player.GetComponent<SpriteRenderer> ().sprite = ShipSprites [2];
		}
		else if (ShipDurability <500) 
		{
			player.GetComponent<SpriteRenderer> ().sprite = ShipSprites [3];
		}
		//Debug.Log (KrakenCurrentHealth);
		CheckForPhaseChange ();
		/*if (ShouldSpawnWaves == true) 
		{
			StartWaveOfWaves ();
		}*/


		if (SpawnWaveLane1 == true) 
		{
			SpawnWaveInLane1 ();
		}
		if (SpawnWaveLane2 == true) 
		{
			SpawnWaveInLane2 ();
		}
		if (SpawnWaveLane3 == true) 
		{
			SpawnWaveInLane3 ();
		}
	}

	void CheckDeath()
	{
		if (ShipDurability <= 0) 
		{
			Debug.Log ("PlayerHasLost");
			//load loss screen and previous checkpoint
		}
	}

	public void TakeDamageFromWaves()
	{
		//Debug.Log (ShipDurability);
	//	Debug.Log ("Should Take Damage");
		ShipDurability = ShipDurability - DamageTakenFromWaves;
		//Debug.Log (ShipDurability);
		CheckDeath ();
	}

	/*public void StartWaveOfWaves()
	{
		GameObject WaveToSpawn = WavePrefabs [Random.Range (0, 3)];
		Debug.Log (WaveToSpawn);
		Instantiate (WaveToSpawn, WaveSpawnLocationObject.position, Quaternion.identity);

		ShouldSpawnWaves = false;
	}*/

	public void SpawnWaveInLane1()
	{
		
		Debug.Log (SingleWavePrefab);
		Instantiate (SingleWavePrefab, new Vector3(WaveSpawnLocationObject.position.x, WaveSpawnLocationObject.position.y, WaveSpawnLocationObject.position.z) , Quaternion.identity);
		SpawnWaveLane1 = false;
	}
	public void SpawnWaveInLane2()
	{

		//Debug.Log (SingleWavePrefab);
		Instantiate (SingleWavePrefab, new Vector3(WaveSpawnLocationObject.position.x + 3.6f, WaveSpawnLocationObject.position.y, WaveSpawnLocationObject.position.z), Quaternion.identity);
		SpawnWaveLane2 = false;
	}
	public void SpawnWaveInLane3()
	{

		Debug.Log (SingleWavePrefab);
		Instantiate (SingleWavePrefab, new Vector3(WaveSpawnLocationObject.position.x + 7.2f, WaveSpawnLocationObject.position.y, WaveSpawnLocationObject.position.z) , Quaternion.identity);
		SpawnWaveLane3 = false;
	}

	void CheckForPhaseChange()
	{
		if (KrakenHealth <= SwitchToPhase3AtThisHealth) 
		{
			Debug.Log("TO PHASE 3");
			//switchToPhase2
			//DESIGNERS... play retreat animation here and at the end add an animation event which will load GoToPhase3Scene(); at the end of animation. Make sure the function goes to the right scene
		}
		else if (KrakenHealth <= SwitchToPhase2AtThisHealth)
		{
			Debug.Log("TO PHASE 2");
			//switchToPhase2
			//DESIGNERS... play retreat animation here and at the end add an animation event which will load GoToPhase2Scene(); at the end of animation. Make sure the function goes to the right scene
		}
	}

	public void GoToPhase2Scene()
	{
		SceneManager.LoadScene ("Phase2");
	}
	public void GoToPhase3Scene()
	{
		SceneManager.LoadScene ("Phase3");
	}


	public void KrakenTakeDamageFromHarpoon()
	{
		KrakenHealth = KrakenHealth - 150;
		Debug.Log (KrakenHealth);
	}

	public void TakeDamageFromBubbles()
	{
		ShipDurability = ShipDurability - DamageTakenFromBubble;
		Debug.Log ("bubble hit" + ShipDurability);
	}

	public void TakeDamageFromBashFish()
	{
		ShipDurability = ShipDurability - DamageTakenFromBashFish;
	}
}
