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

public class PlayerManager : MonoBehaviour 
{
	public GameObject [] playerHUDStates;
	public Transform [] playerSpawns;
	public Transform [] playerDead;
	public tk2dUIProgressBar [] playerEnduranceProgress;
	public tk2dUIProgressBar [] playerHealthProgress;

	public GameObject [] fistHUD; 
	public GameObject [] footHUD;
	public GameObject [] specialHUD;

	//Input
	private GamePadState state;
	private GamePadState prevState;

	private int currentPlayers = 0;
	private int [] currentControlers = { -1, -1, -1, -1, -1 };

	private double lastStart;

	void Start () 
	{
		lastStart = Time.time;
	}

	void PlayJoin()
	{
		if (currentPlayers == 0)
			AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.PlayerOneJoin);
		else if (currentPlayers == 1)
			AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.PlayerTwoJoin);
		else if (currentPlayers == 2)
			AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.PlayerThreeJoin);
		else if (currentPlayers == 3)
			AudioManager.audioManager.PlayAnnoucements(AudioManager.Announcements.PlayerFourJoin);
	}

	public void Reset()
	{
		lastStart = Time.time;
		currentPlayers = 0;
		GameObject [] allPlayers = GameObject.FindGameObjectsWithTag("Player");

		for (int i = 0; i < allPlayers.Length; i++)
		{
			Destroy(allPlayers[i]);
		}

		for (int i = 0; i < currentControlers.Length; i++)
		{
			currentControlers[i] = -1;
		}

		for (int i = 0; i < playerDead.Length; i++)
		{
			playerDead[i].gameObject.SetActive(false);
		}

		for (int i = 0; i < playerHUDStates.Length; i++)
		{
			playerHUDStates[i].gameObject.SetActive(true);
		}

		for (int i = 0; i < fistHUD.Length; i++)
		{
			fistHUD[i].SetActive(false);
		}

		for (int i = 0; i < footHUD.Length; i++)
		{
			footHUD[i].SetActive(false);
		}

		for (int i = 0; i < specialHUD.Length; i++)
		{
			specialHUD[i].SetActive(false);
		}
	}

	void Update () 
	{
		if (GameManager.currentState == GameManager.GameStates.PlayerSelect)
		{
			if (Input.GetKeyDown(KeyCode.Escape))
			{
				Application.Quit();
			}

			if (currentPlayers < 4)
			{
				prevState = state;
				state = GamePad.GetState((PlayerIndex)0);
				
				if (prevState.Buttons.Y == ButtonState.Released && 
				    state.Buttons.Y == ButtonState.Pressed)
				{
						GameObject player = (GameObject)Instantiate(Resources.Load("Player"));
						PlayerAttack playerObject = player.GetComponent<PlayerAttack>();
						player.GetComponent<PlayerMovement>().playerIndex = 5;
						player.name = "Player " + (currentPlayers+1);

						playerObject.playerIndex = 5;
						playerObject.enduranceBar = playerEnduranceProgress[currentPlayers];
						playerObject.healthBar = playerHealthProgress[currentPlayers];
						playerObject.kickObject = fistHUD[currentPlayers];
						playerObject.fistObject = footHUD[currentPlayers];
						playerObject.specialObject = specialHUD[currentPlayers];
						playerObject.deathObject = playerDead[currentPlayers].gameObject;
						playerObject.playerGameID = currentPlayers;

						GameManager.playersAmount++;
							
						player.transform.position = playerSpawns[currentPlayers].position;

						PlayJoin();

						playerHUDStates[currentPlayers].SetActive(false);
						currentControlers[4] = 5;
							
						currentPlayers++;
				}

				for (int i = 0; i < 4; ++i)
				{
					PlayerIndex testPlayerIndex = (PlayerIndex)i;
					GamePadState testState = GamePad.GetState(testPlayerIndex);

					if (testState.IsConnected)
					{
						if (testState.Buttons.A == ButtonState.Pressed)
						{
							if (currentControlers[i] == -1)
							{
								GameObject player = (GameObject)Instantiate(Resources.Load("Player"));
								PlayerAttack playerObject = player.GetComponent<PlayerAttack>();
								player.GetComponent<PlayerMovement>().playerIndex = i;
								player.name = "Player " + (currentPlayers+1);

								playerObject.playerIndex = i;
								playerObject.enduranceBar = playerEnduranceProgress[currentPlayers];
								playerObject.healthBar = playerHealthProgress[currentPlayers];
								playerObject.kickObject = fistHUD[currentPlayers];
								playerObject.fistObject = footHUD[currentPlayers];
								playerObject.specialObject = specialHUD[currentPlayers];
								playerObject.deathObject = playerDead[currentPlayers].gameObject;
								playerObject.playerGameID = currentPlayers;
								GameManager.playersAmount++;

								PlayJoin();

								player.transform.position = playerSpawns[currentPlayers].position;

								playerHUDStates[currentPlayers].SetActive(false);
								currentControlers[i] = i;

								currentPlayers++;
							}
						}
					}
				}
			}
		}
	}
}
