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

public class GameManager : MonoBehaviour {

	public Transform wavePrefab;
	public List<Transform> powerupPrefabs;
	public Text timeText;
	float time;	
	public static Wave lastWave;

	public AudioClip s;
	public AudioClip b;
	public AudioClip p;
	public AudioClip hurt;

	public static GameManager instance;

	void Start() {
		instance = this;
		GetComponent<AudioSource> ().PlayOneShot (b);
		lastWave = Instantiate (wavePrefab, Vector3.zero + Vector3.up, Quaternion.identity).GetComponent<Wave>();
		StartCoroutine (Loop (1));
		StartCoroutine (PowerupLoop (5));
	}
	void Update() {
		if(GameObject.Find("Player1").GetComponent<Shooting>().lastLaser != null) {
			if(GameObject.Find("Player1").GetComponent<Collider>().bounds.Intersects(GameObject.Find("Player1").GetComponent<Shooting>().lastLaser.GetComponent<Collider>().bounds)) {
				Application.LoadLevel ("Player2Wins");
			}
			if(GameObject.Find("Player2").GetComponent<Collider>().bounds.Intersects(GameObject.Find("Player1").GetComponent<Shooting>().lastLaser.GetComponent<Collider>().bounds)) {
				Application.LoadLevel ("Player1Wins");
			}
		}
		if(GameObject.Find("Player2").GetComponent<Shooting>().lastLaser != null) {
			if(GameObject.Find("Player1").GetComponent<Collider>().bounds.Intersects(GameObject.Find("Player2").GetComponent<Shooting>().lastLaser.GetComponent<Collider>().bounds)) {
				Application.LoadLevel ("Player2Wins");
			}
			if(GameObject.Find("Player2").GetComponent<Collider>().bounds.Intersects(GameObject.Find("Player2").GetComponent<Shooting>().lastLaser.GetComponent<Collider>().bounds)) {
				Application.LoadLevel ("Player1Wins");
			}
		}
		timeText.text = time.ToString ();
	}
	IEnumerator Loop (float initTime) {
		time = initTime;
		while(time > 0) {
			yield return new WaitForSeconds (1f);
			GetComponent<AudioSource> ().PlayOneShot (s);
			time--;
		}
		GetComponent<AudioSource> ().PlayOneShot (b);
		lastWave = Instantiate (wavePrefab, Vector3.zero + Vector3.up, Quaternion.identity).GetComponent<Wave>();
		StartCoroutine (Loop (initTime));
	}
	IEnumerator PowerupLoop (float initTime) {
		yield return new WaitForSeconds (initTime);
		GetComponent<AudioSource> ().PlayOneShot (p);
		int n = Random.Range (0, 100);
		int toSpawn = 0;
		if (n < 10) {
			toSpawn = 1;
		} else if (n < 30) {
			toSpawn = 3;
		} else if (n < 60) {
			toSpawn = 2;
		} else if (n < 80) {
			toSpawn = 0;
		} else if (n < 100) {
			toSpawn = 4;
		} 
		toSpawn = Random.Range (0, 7);
		float x = Random.Range (-20, 20);
		float z = Random.Range (-20, 20);
		if (x % 2 == 0) {
			x--;
		}
		if (z % 2 == 0) {
			z--;
		}
		Instantiate (powerupPrefabs[toSpawn], new Vector3(x, 1, z), Quaternion.identity);
		StartCoroutine (PowerupLoop (initTime));
	}
	public static void WinP1(string weapon) {
		GameManager.instance.StartCoroutine(GameManager.instance.P1(weapon));
	}
	public IEnumerator P1(string weapon) {
		GetComponent<AudioSource> ().PlayOneShot (hurt);
		yield return new WaitForSeconds (1);
		PlayerPrefs.SetString ("weapon", weapon);
		Application.LoadLevel ("Player1Wins");
	}
	public static void WinP2(string weapon) {
		GameManager.instance.StartCoroutine(GameManager.instance.P2(weapon));
	}
	public IEnumerator P2(string weapon) {
		GetComponent<AudioSource> ().PlayOneShot (hurt);
		yield return new WaitForSeconds (1);
		PlayerPrefs.SetString ("weapon", weapon);
		Application.LoadLevel ("Player2Wins");
	}
}
