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

public class CharacterSelectionController : MonoBehaviour
{

	bool choising;
	int currentPlayer;
	public StandaloneInputModule inputModule;
	public EventSystem eventSystem;

	private List<int> assignedControllers = new List<int> ();
	private List<int> byPass = new List<int> ();

	private int redPlayers;
	private int bluePlayers;

	[Header("Alert Panel")]
	public Text alertMessage;
	public GameObject alertPanel;
	private bool showingAlert;

	[Header("Others")]
	public Button[] buttons;
	public Animator[] playersAnimator;

	private bool bypassUpdate;
	
	// Update is called once per frame
	void Update ()
	{
			if (showingAlert) {
				for (int i = 0; i < 4; i++) {

					//mexeu bugou, perigo fatal
					string inputName = "Jump" + i;
					if (i == 0)
						inputName = "Confirm";

					if (Input.GetButtonDown (inputName)) {
						HiddenAlert ();
						return; //quebra o ciclo do update
					}
				}
			}

			if (!choising && !showingAlert) {
				for (int i = 0; i < 4; i++) {
					
					//mexeu bugou, perigo fatal
					string inputName = "Jump" + i;
					if (i == 0)
						inputName = "Confirm";
					//fim da zona de risco

					if (Input.GetButtonDown (inputName)) {
						if (!assignedControllers.Contains (i)) {
							currentPlayer = i;
							choising = true;
							ActivePlayer (inputName);
							break;
						} else {
							if (!byPass.Contains (i)) {
								StartGame ();
							} else {
								byPass.Remove (i);
							}
						}
					}
				}
			}
	}

	void StartGame(){
		if (redPlayers == 0 || bluePlayers == 0) {
			assignedControllers.Clear ();
			Game.ResetRoundPlayerSettings ();
			foreach (Animator a in playersAnimator) {
				a.SetTrigger ("Idle");
			}
			ShowAlert ("One of the teams is empty, choose again");
		} else {
			Game.OpenScene ("GamePlay");
		}
	}

	void ShowAlert(string text){
		showingAlert = true;
		alertMessage.text = text;
		alertPanel.SetActive (true);

		foreach (Button b in buttons) {
			b.interactable = true;
		}

		eventSystem.enabled = true;
	}

	public void HiddenAlert(){
		showingAlert = false;
		alertPanel.SetActive (false);
	}

	//inputName é gambiarra
	void ActivePlayer (string inputName)
	{
		GameObject availableChampion=null;

		foreach (Button b in buttons) {
			if (b.interactable)
				availableChampion = b.gameObject;
		}

		eventSystem.enabled = true;
		eventSystem.SetSelectedGameObject (availableChampion);
		inputModule.horizontalAxis = "Horizontal" + currentPlayer;
		inputModule.verticalAxis = "Vertical" + currentPlayer;
		inputModule.submitButton = inputName;
	}

	public void SelectBlueChampion (int playerID)
	{
		AudioController.PlayHudSound(HudSoundEffects.SELECT);
		bluePlayers++;
		assignedControllers.Add (currentPlayer);
		byPass.Add (currentPlayer);

		if (playerID == 0) {
			playersAnimator [0].SetBool ("selected", false);
			playersAnimator [0].SetTrigger ("confirmed");
		} else {
			playersAnimator [1].SetBool ("selected", false);
			playersAnimator [1].SetTrigger ("confirmed");
		}

		GameObject gm=eventSystem.currentSelectedGameObject;
		gm.GetComponent<Animator> ().SetTrigger ("confirmed");

		Game.AddPlayer ((PlayerNames)playerID, Teams.BLUE, (Joysticks)currentPlayer);
		choising = false;
		eventSystem.enabled = false;
	}

	public void SelectRedChampion (int playerID)
	{
		AudioController.PlayHudSound(HudSoundEffects.SELECT);
		redPlayers++;
		assignedControllers.Add (currentPlayer);

		if (playerID == 0) {
			playersAnimator [2].SetBool ("selected", false);
			playersAnimator [2].SetTrigger ("confirmed");
		} else {
			playersAnimator [3].SetBool ("selected", false);
			playersAnimator [3].SetTrigger ("confirmed");
		}

		byPass.Add (currentPlayer);
		Game.AddPlayer ((PlayerNames)playerID, Teams.RED, (Joysticks)currentPlayer);
		choising = false;
		eventSystem.enabled = false;
	}
}
