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

public enum GS {None, Title, NumPlayer, CharSelect, Ready, Playing, Fail, Win};

public class GameManager : MonoBehaviour {

	public static GameManager GM;
	private GS state = GS.None;
	public Camera cam;

	private float UIOutOfWayTop = 2000;
	private float UIOutOfWayBot = -600;
	private float SlideSpeed = 0.5f;


	private bool interactable = true;
	private bool smolInteract = true;
	// Title Screen


	// Choose num players screen
	public Text numberPlayers;
	public RectTransform numPlayerSlide;

	// Enter Player text screen
	public RectTransform playerMsgSlide;
	public List<string> textForEnd;


	// Win "Text"
	public GameObject winr;
	public Text winrarMsg;

	public int numPlayers = 3;

	void Awake () {
		GM = this;
		state = GS.NumPlayer;
	}

	// Use this for initialization
	void Start () {
		winr.SetActive (false);
		AudioController.AC.playMenu ();
	}
	
	// Update is called once per frame
	void Update () {
		if (state == GS.None) {
			// Go to title
		} else if (state == GS.NumPlayer) {
			NumPlayer ();
		} else if (state == GS.CharSelect) {
			CharSelect ();
		}
	}

	public void StartPlay() {
		state = GS.Playing;
		AudioController.AC.playTheme ();
	}

	public GS GetState () {
		return state;
	}

	public int GetNumPlayers() {
		return numPlayers;
	}

	public void GetReady() {
		state = GS.Ready;
		StartCoroutine (SlideUIVertical (playerMsgSlide, UIOutOfWayTop));
		StartCoroutine (StageManager.SM.SlideCamToStart());
	}

	public void Fail() {
		state = GS.Fail;
	}

	public void Win(GameObject winrar, bool platform=false) {
		AudioController.AC.playVictor ();
		state = GS.Win;
		Debug.Log (winrar.name);
		StartCoroutine (SlideCamToEnd ());


		if (!platform) {
			winrar.transform.position = new Vector3 (237, 1, 0);
			winrar.SetActive (true);
		}
		if (winrar.name.Equals ("Mailboy")) {
			winrarMsg.text = textForEnd.ToArray() [0];
		} else if (winrar.name.Equals ("MessageBoi")) {
			winrarMsg.text = textForEnd.ToArray() [1];
		} else if (winrar.name.Equals ("CloudBoi")) {
			winrarMsg.text = textForEnd.ToArray() [2];
		} else {
			winrarMsg.text = textForEnd.ToArray() [3];
		}

	}
		


	void NumPlayer() {
		if (smolInteract && interactable) {
			if (Input.GetAxis ("P1 X-Axis") > 0.9) {
				numPlayers = Mathf.Min (numPlayers + 1, 4);
				numberPlayers.text = "" + numPlayers;
				smolInteract = false;
			} else if (Input.GetAxis ("P1 X-Axis") < -.9) {
				numPlayers = Mathf.Max (numPlayers - 1, 2);
				numberPlayers.text = "" + numPlayers;
				smolInteract = false;
			}
			if (Input.GetButtonDown("Submit")) {
				StartCoroutine (SlideUIVertical(numPlayerSlide, UIOutOfWayTop));
				StartCoroutine (SlideUIVertical (playerMsgSlide, 300));
				interactable = false;
				state = GS.CharSelect;
			}
		}
		if (Mathf.Abs (Input.GetAxis("P1 X-Axis")) < 0.1f) {
			smolInteract = true;
		}

	}

	void CharSelect() {
		
	}

	public void getDatafromList(List<string> msg) {
		textForEnd = msg;
	}
		

	public IEnumerator SlideUIVertical (RectTransform pannel, float Ydest) {
		float startime = Time.time;
		Vector3 pos = pannel.position;
		float velocity = 0;
		while ((pannel.position.y < Ydest) && ((Time.time - startime) * SlideSpeed) < 3f) {
			float move = Mathf.SmoothDamp (pannel.position.y, Ydest, ref velocity, .1f);
			pannel.position = new Vector3(pannel.position.x, move, pannel.position.z);

			yield return null;
		}
		interactable = true;
	}
		
	public IEnumerator SlideCamToEnd () {
		Debug.Log ("Win!");
		float startime = Time.time;
		Vector3 pos = cam.transform.position;
		float velocity = 0;
		while ((cam.transform.position.x < 235) && ((Time.time - startime)) < 10f) {
			float move = Mathf.SmoothDamp (cam.transform.position.x, 235, ref velocity, 2f);
			cam.transform.position = new Vector3(move, cam.transform.position.y, cam.transform.position.z);
			yield return null;
		}

		winr.SetActive (true);
		//		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;


	}

}
