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

public enum GameStates{
	STARTING,
	GAME_PLAY,
	GAME_FINISH,
}

public class GameController : MonoBehaviour {

	[HideInInspector]
	public GameStates currentGameStates;

	[Header("Spawn Settings")]
	public GameObject[] playersPrefab;
	public Transform[] redSpawnPoints;
	public Transform[] blueSpawnPoints;

	public List<PlayerController> players=new List<PlayerController>();

	[Header("Timer Settings")]
	public float roundDuration=60;
	public float startDuration=5;

	[Header("Feedbacks")]
	public Text centerText;
	public Text upperText;

	[Header("Players HUD")]
	public Sprite[] charIconSprites;

	public GameObject mainPlayerPainel;
	public Text[] playersScoreText;
	public Image[] playersLifeImage;
	public Image[] playerPicture;
	public Image[] playerMainPanel;

	public Color redTeamColor;
	public Color blueTeamColor;

	private float startTimer;
	private float roundTimer;

	[Header("Others")]
	public PoolManager soundWavePool;
	public GameObject panelResults;
	public Text textResults;

	public static GameController instance;

	public static GameStates GetCurrentStates(){
		return instance.currentGameStates;
	}
	//Use this for initialization
	void Start () {
		ToggleInterface (false);
		SpawnPlayers ();
	}

	void Awake(){
		instance=this;
	}

	void SpawnPlayers(){
		int redSpawnIndex= 0;
		int blueSpawnIndex = 0;
		int playerID = 0;
		foreach (PlayerRoundSettings playerSettings in Game.PlayersSettings) {

			if (playerSettings.playerTeam == Teams.BLUE) {
				GameObject gm = Instantiate (playersPrefab [(int)playerSettings.playerName], blueSpawnPoints [blueSpawnIndex].position, blueSpawnPoints [blueSpawnIndex].rotation) as GameObject;
				PlayerController p = gm.GetComponent<PlayerController> ();
				p.SetJoystick(playerSettings.joystick);
				p.team = Teams.BLUE;
				p.playerID=playerID;
				players.Add(p);
				playerMainPanel[playerID].color=blueTeamColor;
				playerMainPanel [playerID].gameObject.SetActive (true);
				playerPicture [playerID].sprite = charIconSprites [(int)playerSettings.playerName];
				blueSpawnIndex++;
			}else if(playerSettings.playerTeam == Teams.RED){
				GameObject gm = Instantiate (playersPrefab [(int)playerSettings.playerName], redSpawnPoints [redSpawnIndex].position, redSpawnPoints [redSpawnIndex].rotation);
				PlayerController p = gm.GetComponent<PlayerController> ();
				p.SetJoystick(playerSettings.joystick);
				p.team = Teams.RED;
				p.playerID=playerID;
				p.isFacingRight = true;
				players.Add(p);
				playerMainPanel[playerID].color=redTeamColor;
				playerPicture [playerID].sprite = charIconSprites [(int)playerSettings.playerName];
				playerMainPanel [playerID].gameObject.SetActive (true);
				redSpawnIndex++;
			}
			playerID++;
		}

		startTimer = startDuration;
		ChangeStat (GameStates.STARTING);
	}

	void ToggleInterface(bool value){
		mainPlayerPainel.SetActive(value);
	}

	public static void UpdatePlayersPoints(int playerID, int score){
		instance.playersScoreText [playerID].text = score.ToString();
	}

	public static void UpdatePlayerLife(int playerID, int life){
		instance.playersLifeImage [playerID].fillAmount = life * 0.01f;
	}
	
	// Update is called once per frame
	void Update () {

		switch (currentGameStates) {

		case GameStates.STARTING:
				{
					startTimer -= Time.deltaTime;

					if (startTimer < 1) 
						centerText.text = startTimer.ToString ("Go!!");
					else
						centerText.text = startTimer.ToString ("00");
				
					if (startTimer <= 0) {
						centerText.gameObject.SetActive (false);
						ChangeStat (GameStates.GAME_PLAY);
					}
				}
				break;
		
		case GameStates.GAME_PLAY:
			{
				roundTimer += Time.deltaTime;

				upperText.text = roundTimer.ToString ("00");
				
				if (roundTimer >= roundDuration) {
					
					roundTimer = 0;
					upperText.gameObject.SetActive (false);
					centerText.gameObject.SetActive (true);
					centerText.text = startTimer.ToString ("Game Finish!!");

					ChangeStat (GameStates.GAME_FINISH);
				}
			}
			break;
		}
	}

	//por enquanto isso é uma merda que não serve pra nada
	public void ShowMessage(string message){
		//mostrar a mensagem aqui
	}

	public void ChangeStat(GameStates newStat){
		currentGameStates = newStat;

		if (currentGameStates == GameStates.GAME_PLAY) {
			ToggleInterface (true);
		}
		if (currentGameStates == GameStates.GAME_FINISH) {
			ToggleInterface (false);

			int redPoints = 0;
			int bluePoints = 0;

			foreach (PlayerController p in players) {
				if (p.team == Teams.BLUE)
					bluePoints += p.points;
				else
					redPoints += p.points;
			}

			if (redPoints == bluePoints) {
				textResults.text="Draw game!";
			}else if (redPoints > bluePoints) {
				textResults.text="<color=red>Red team wins!</color>";
			}else if (redPoints < bluePoints) {
				textResults.text="<color=blue>Blue team wins!</color>";
			}

			panelResults.SetActive (true);
		}
	}

	public void GoMenu(){
		Game.OpenScene ("MainMenu");
	}

	public void Rematch(){
		Game.OpenScene ("GamePlay");
	}


}
