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


public class StageManager : MonoBehaviour {

	public static StageManager SM;
	// Actors
	public GameObject stage;
	public Camera cam;
	private GS state;
	public bool starting = false;
	private bool interactable = true;

	// Stage Movement
	public Text readyText;
	private bool move;
	public float speed = 3f;


	// Stage Generation values
	private float rightBound;
	private int segment;

	// Players
	public GameObject p1;
	public GameObject p2;
	public GameObject p3;
	public GameObject p4;
	bool p1ready = false;
	bool p2ready = false;
	bool p3ready = false;
	bool p4ready = false;


	// Use this for initialization
	void Start () {
		SM = this;
		string[] joy = Input.GetJoystickNames ();
		Debug.Log (joy);
		for (int i = 0; i < joy.Length; i++) {
			Debug.Log (i + "\t" + joy[i]);
		}

		segment = 0;

		move = false; //TEMPORARY
		//GetReady ();
		readyText.enabled = false;
	}

	// Update is called once per frame
	void Update () {
		rightBound = cam.transform.position.x;

		if (GameManager.GM.GetState() == GS.Ready && interactable){
			Debug.Log ("ready?"); 
			if (Input.GetButtonDown ("P1 B") || Input.GetButtonDown ("P1 A")) {
				p1ready = true;
				Debug.Log ("P1 ready!");
			}
			if (Input.GetButtonDown ("P2 B") || Input.GetButtonDown ("P2 A")) {
				p2ready = true;
				Debug.Log ("P2 ready!");
			}
			if (Input.GetButtonDown ("P3 B") || Input.GetButtonDown ("P3 A") || GameManager.GM.numPlayers < 3) {
				p3ready = true;
				Debug.Log ("P3 ready!");
			}
			if (Input.GetButtonDown ("P4 B") || Input.GetButtonDown ("P4 A") || GameManager.GM.numPlayers < 4) {
				p4ready = true;
				Debug.Log ("P4 ready!");
			}
			if (p1ready && p2ready && p3ready && p4ready && !starting) {
				StartCoroutine (countdown ());
				starting = true;
			}
		}

		if (GameManager.GM.GetState() == GS.Playing) {
			if (cam.transform.position.x < 235f)
				cam.transform.Translate (Vector3.right * speed * Time.deltaTime);
			if (p1.activeSelf) {
				if (!checkBounds (p1)) {
					AudioController.AC.playDeath ();
					p1.SetActive (false);
				}
			}
			if (p2.activeSelf) {
				if (!checkBounds (p2)) {
					AudioController.AC.playDeath ();
					p2.SetActive (false);
				}
			}
			if (p3.activeSelf) {
				if (!checkBounds (p3)) {
					AudioController.AC.playDeath ();
					p3.SetActive (false);
				}
			}
			if (p4.activeSelf) {
				if (!checkBounds (p4)) {
					AudioController.AC.playDeath ();
					p4.SetActive (false);
				}
			}

			// Get Players
			GameObject[] players = GameObject.FindGameObjectsWithTag ("Player");
			int count = 0;
			if (p1.activeInHierarchy == false) {
				count++;
			}
			if (p2.activeInHierarchy == false) {
				count++;
			}
			if (p3.activeInHierarchy == false) {
				count++;
			}
			if (p4.activeInHierarchy == false) {
				count++;
			}
			if (count >= 3 ) { //if All but 1 characters ded
				for (int i = 0; i < players.Length; i++) {
					if (players[i].activeSelf) {
						GameManager.GM.Win (players[i]);
					}
				}
			}
		}
			
	}


	void GetReady() {
		state = GameManager.GM.GetState ();
		Debug.Log (GameManager.GM.GetNumPlayers ());
		p1ready = false;
		p2ready = false;
		p3ready = false;
		p4ready = false;
		p1.SetActive (true);
		p2.SetActive (true);
		p3.SetActive (GameManager.GM.GetNumPlayers () >= 3);
		p4.SetActive (GameManager.GM.GetNumPlayers () >= 4);
		readyText.text = "All players must jump or punch to continue!";
		readyText.enabled = true;
		starting = false;
	}

	IEnumerator countdown() {
		readyText.text = "3";
		yield return new WaitForSeconds (1f);
		readyText.text = "2";
		yield return new WaitForSeconds (1f);
		readyText.text = "1";
		yield return new WaitForSeconds (1f);
		readyText.text = "Go!";
		GameManager.GM.StartPlay ();
		yield return new WaitForSeconds (1f);
		readyText.enabled = false;
	}

	bool checkBounds(GameObject player) {
		return !(player.transform.position.x < (cam.transform.position.x - 10) || 
			player.transform.position.y < (cam.transform.position.y - 6));
	}

	void GenerateObstacles() {
		
	}

//	void OnGUI() {
//		Event e = Event.current;
//		if (e.isKey)
//			Debug.Log("Detected key code: " + e.keyCode);
//
//	}

	public IEnumerator SlideCamToStart () {
		GetReady ();
		float startime = Time.time;
		Vector3 pos = cam.transform.position;
		float velocity = 0;
		while ((cam.transform.position.x < 0) && ((Time.time - startime)) < 5f) {
			float move = Mathf.SmoothDamp (cam.transform.position.x, 0, ref velocity, 1f);
			cam.transform.position = new Vector3(move, cam.transform.position.y, cam.transform.position.z);
			yield return null;
		}

//		Vector3 pos = cam.transform.position;
//		float t = 0;
//		while ((cam.transform.position.x < 0) && ((Time.time - startime)) < 3f) {
//			cam.transform.position = new Vector3 (Mathf.Lerp (pos.x, 0, t), 1, 0);
//			t += 0.5f * Time.deltaTime;
//			yield return null;
//		}
		interactable = true;
		GetReady ();

	}


}
