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

public class MainMenu : MonoBehaviour 
{
	float timer = 5f;
	int readyPlayersCount;
	int minimumPlayers = 1; // TODO: Change to 2 later
	bool isPlayerOneReady;
	bool isPlayerTwoReady;
	bool isPlayerThreeReady;
	bool isPlayerFourReady;
	bool ready;

	public string SceneAfterReady = "City1";

	public bool IsPlayerOneReady 
	{ 
		get
		{
			return isPlayerOneReady;
		}

		private set
		{
			isPlayerOneReady = value;
			UpdateReadyPlayersCount();
			UpdatePlayerReadyGraphics();
		}
	}

	public bool IsPlayerTwoReady
	{
		get
		{
			return isPlayerTwoReady;
		}

		private set
		{
			isPlayerTwoReady = value;
			UpdateReadyPlayersCount();
			UpdatePlayerReadyGraphics();
		}
	}

	public bool IsPlayerThreeReady
	{
		get
		{
			return isPlayerThreeReady;
		}

		private set
		{
			isPlayerThreeReady = value;
			UpdateReadyPlayersCount();
			UpdatePlayerReadyGraphics();
		}
	}

	public bool IsPlayerFourReady 
	{ 
		get
		{
			return isPlayerFourReady;
		}

		private set
		{
			isPlayerFourReady = value;
			UpdateReadyPlayersCount();
			UpdatePlayerReadyGraphics();
		}
	}

	void Awake() 
	{
		Reset();
		
		GameState.Instance.Reset(); 
	}

	void Update()
	{
		var player1 = ReInput.players.GetPlayer(0);
		var player2 = ReInput.players.GetPlayer(1);
		var player3 = ReInput.players.GetPlayer(2);
		var player4 = ReInput.players.GetPlayer(3);

		// Exit application if ESC is pressed
		if (player1.GetButtonDown("Menu")) 
		{
			Application.Quit();
		}
		
		// Set player ready if player presses Fire button
		if (!IsPlayerOneReady && player1.GetButtonDown("X")) 
		{
			IsPlayerOneReady = true;
		}
		else if (IsPlayerOneReady && player1.GetButtonDown("X"))
		{
			IsPlayerOneReady = false;
		}

		if (!IsPlayerTwoReady && player2.GetButtonDown("X")) 
		{
			IsPlayerTwoReady = true;
		}
		else if (IsPlayerTwoReady && player2.GetButtonDown("X"))
		{
			IsPlayerTwoReady = false;
		}

		if (!IsPlayerThreeReady && player3.GetButtonDown("X")) 
		{
			IsPlayerThreeReady = true;
		}
		else if (IsPlayerThreeReady && player3.GetButtonDown("X"))
		{
			IsPlayerThreeReady = false;
		}

		if (!IsPlayerFourReady && player4.GetButtonDown("X")) 
		{
			IsPlayerFourReady = true;
		}
		else if (IsPlayerFourReady && player4.GetButtonDown("X"))
		{
			IsPlayerFourReady = false;
		}

		Ready();
	}

	void FixedUpdate() 
	{

	}

	void Ready()
	{
		if (ready || readyPlayersCount < minimumPlayers)
		{
			return;
		}

		timer -= Time.deltaTime;
		UpdateTimerText();

		if (timer <= 0)
		{
			ready = true;

			// Set initial game state
			SetGameState();

			FindObjectOfType<SceneController>().FadeAndLoadScene(SceneAfterReady);
		}
	}

	void UpdatePlayerReadyGraphics()
	{
		var readyPlayerOne = GameObject.Find("ReadyPlayerOne").GetComponentsInChildren<Image>();
		var readyPlayerTwo = GameObject.Find("ReadyPlayerTwo").GetComponentsInChildren<Image>();
		var readyPlayerThree = GameObject.Find("ReadyPlayerThree").GetComponentsInChildren<Image>();
		var readyPlayerFour = GameObject.Find("ReadyPlayerFour").GetComponentsInChildren<Image>();

		var c1 = readyPlayerOne.First(x => x.name == "On").color;
		var c2 = readyPlayerTwo.First(x => x.name == "On").color;
		var c3 = readyPlayerThree.First(x => x.name == "On").color;
		var c4 = readyPlayerFour.First(x => x.name == "On").color;

		if (isPlayerOneReady) 
		{
			c1.a = 1f;
		}
		else 
		{
			c1.a = 0.7f;
		}

		if (isPlayerTwoReady)
		{
			c2.a = 1f;
		}
		else 
		{
			c2.a = 0.7f;
		}

		if (isPlayerThreeReady)
		{
			c3.a = 1f;
		}
		else 
		{
			c3.a = 0.7f;
		}

		if (isPlayerFourReady)
		{
			c4.a = 1f;
		}
		else 
		{
			c4.a = 0.7f;
		}

		readyPlayerOne.First(x => x.name == "On").color = c1;
		readyPlayerTwo.First(x => x.name == "On").color = c2;
		readyPlayerThree.First(x => x.name == "On").color = c3;
		readyPlayerFour.First(x => x.name == "On").color = c4;

		// Init player	
		GameState.Instance.InitPlayer(0);
		GameState.Instance.InitPlayer(1);
		GameState.Instance.InitPlayer(2);
		GameState.Instance.InitPlayer(3);
	}

	void UpdateTimerText() 
	{
		var label = GameObject.Find("ReadyCountdown").GetComponentInChildren<Text>();

		if (readyPlayersCount < minimumPlayers)
		{
			label.text = "Press X to join";
		}
		else 
		{ 
			var time = Mathf.RoundToInt(timer);

			if (time <= 0)
		    {
				label.text = $"Go go go!";
			}
			else 
			{
				label.text = time.ToString();
			}
		}
	}

	void UpdateReadyPlayersCount() 
	{
		readyPlayersCount = 0;

		if (isPlayerOneReady)
			readyPlayersCount++;
		if (isPlayerTwoReady)
			readyPlayersCount++;
		if (isPlayerThreeReady)
			readyPlayersCount++;
		if (isPlayerFourReady)
			readyPlayersCount++;

		// Reset timer every time someone changes their ready state
		timer = 5f;
		UpdateTimerText();
	}

	void Reset()
	{
		timer = 5f;
		readyPlayersCount = 0;
		isPlayerOneReady = false;
		isPlayerTwoReady = false;
		isPlayerThreeReady = false;
		isPlayerFourReady = false;

		UpdateTimerText();
		UpdatePlayerReadyGraphics();
	}

	void SetGameState() 
	{
		if (isPlayerOneReady) 
		{
			GameState.Instance.SetPlayerState(0, true);
		}

		if (isPlayerTwoReady) 
		{
			GameState.Instance.SetPlayerState(1, true);
		}

		if (isPlayerThreeReady) 
		{
			GameState.Instance.SetPlayerState(2, true);
		}

		if (isPlayerFourReady) 
		{
			GameState.Instance.SetPlayerState(3, true);
		}
	}
}
