using UnityEngine;
using System.Collections;
using XInputDotNetPure; // Required in C#

public class GameManager : MonoBehaviour 
{
	public enum GameStates
	{
		PlayerSelect,
		IntroGame,
		Game,
		GameOver
	};

	public static GameStates currentState;

	public static int playersAmount;

	public FadeOut introFadeOut;
	public PlayerManager playerManager;

	public GameObject gameOverScreen;
	public tk2dTextMesh playerGameOver;

	public GameObject characterSelectTutorial;

	private GamePadState state;
	private GamePadState prevState;

	public tk2dTextMesh countdown;
	private double startTime;

	private bool hasPlayedFight = false;

	void Start () 
	{
		AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.TitleKickPunch);
	}

	IEnumerator PlayClap()
	{
		yield return new WaitForSeconds(2);
		AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.TheClap);
	}

	void Update () 
	{
		prevState = state;
		state = GamePad.GetState((PlayerIndex)0);

		if (currentState == GameStates.Game)
		{
			if (playersAmount <= 1)
			{
				currentState = GameStates.GameOver;
				string winningPlayer = GameObject.FindGameObjectWithTag("Player").name;

				gameOverScreen.SetActive(true);
				playerGameOver.text = winningPlayer + " Wins!";

				string [] winningStr = winningPlayer.Split(' ');
				int playerID = int.Parse(winningStr[1]);

				if (playerID == 1)
				{
					AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.Player1Win);
				}
				else if (playerID == 2)
				{
					AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.Player2Win);
				}
				else if (playerID == 3)
				{
					AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.Player3Win);
				}
				else if (playerID == 3)
				{
					AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.Player4Win);
				}

				StartCoroutine(PlayClap());
			}
		}

		if (currentState == GameStates.PlayerSelect)
		{
			if (prevState.Buttons.Start == ButtonState.Released && 
			    state.Buttons.Start == ButtonState.Pressed)
			{
				characterSelectTutorial.SetActive(false);
				currentState = GameStates.IntroGame;
				startTime = Time.time;
				countdown.gameObject.SetActive(true);

				AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.ThreeTwoOne);
			}
		}

		if (currentState == GameStates.GameOver)
		{
			if (prevState.Buttons.Start == ButtonState.Released && 
			    state.Buttons.Start == ButtonState.Pressed)
			{
				currentState = GameStates.PlayerSelect;
				characterSelectTutorial.SetActive(true);
				gameOverScreen.SetActive(false);
				startTime = Time.time;
				playersAmount = 0;
				hasPlayedFight = false;
				introFadeOut.Reset();
				playerManager.Reset();
			}
		}

		if (currentState == GameStates.IntroGame)
		{
			if (Time.time - startTime > 4)
			{
				countdown.gameObject.SetActive(false);
				currentState = GameStates.Game; 
			}
			else
			{
				int timeNow = ((int)(Time.time - startTime));

				if (timeNow >= 3)
				{
					if (!hasPlayedFight)
					{
						hasPlayedFight = true;
						AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.Fight);
					}

					countdown.text = "FIGHT!";
				}
				else
				{
					countdown.text = (3 - ((int)(Time.time - startTime))).ToString();
				}
			}
		}
	}
}
