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

public class GameManager : MonoBehaviour {


    public GameObject gameOverPanel;
    public GameObject roundOverTextObj;


    public Text p1Score;
    public Text p2Score;
    public Text p3Score;
    public Text p4Score;

    string[] characterNames = new string[4] { "The Jock", "Mike Wave", "Mr Handyman", "Waif" };


    public int roundNumber = 0;
    public int playersAlive = 4;

    public int p1Wins;
    public int p2Wins;
    public int p3Wins;
    public int p4Wins;

    public bool showingEndRoundText;
    bool gate; 
    // Use this for initialization
    void Start () {
      
	}
	
	// Update is called once per frame
	void Update () {
		
        if(playersAlive == 1 && !showingEndRoundText)
        {
            GameObject[] players = GameObject.FindGameObjectsWithTag("Player");

            foreach(GameObject g in players)
            {
                if (!g.GetComponent<PlayerController>().dead && !gate)
                {
                    gate = true;
                    StartCoroutine(EndRoundTimed(g.GetComponent<PlayerController>().playerNumber));

                }
            }
        }

	}

    IEnumerator EndRoundTimed(int winningPlayer)
    {
        yield return new WaitForSeconds(2);
        EndRound(winningPlayer);
    }

    public void PlayerKilled()
    {
        playersAlive --;
    }

    void StartRound()
    {
        //
        playersAlive = 4;
        roundNumber++;
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        gate = false;

        foreach (GameObject g in players)
        {
           g.GetComponent<PlayerController>().dead = false;
           g.GetComponent<Animator>().SetBool("Dead", false);
           g.transform.position = g.GetComponent<PlayerController>().startPos;

        }

    }

    void EndRound(int playerNumber)
    {
        int winnerPlayer;

       if (playerNumber == 1) {
            p1Wins++;
            p1Score.text = ("Score: " + p1Wins.ToString());
            EnableEndRoundText(1);
        }else if (playerNumber == 2){
            p2Wins++;
            p2Score.text = ("Score: " + p2Wins.ToString());
            EnableEndRoundText(2);
        }else if (playerNumber == 3){
            p3Wins++;
            p3Score.text = ("Score: " + p3Wins.ToString());
            EnableEndRoundText(3);
        }
        else if (playerNumber == 4){
            p4Wins++;
            p4Score.text = ("Score: " + p4Wins.ToString());
            EnableEndRoundText(4);
        }
       

        if(CheckForWinner(out winnerPlayer))
        {
            EndGame(winnerPlayer);
        }
        else
        {
            StartRound();
        }

    }

    void EnableEndRoundText(int roundVictor)
    {
        roundOverTextObj.SetActive(true);
        string s = characterNames[roundVictor-1] + " Wins!";
       // Debug.Log(roundVictor.ToString());
        roundOverTextObj.GetComponent<Text>().text = s;
        Invoke("DisableRoundText", 3);
    }

    void DisableRoundText()
    {
        roundOverTextObj.SetActive(false);
    }

    bool CheckForWinner(out int winningPlayer)
    {

        int winner = 0;
        int mostWins = 0;

        if(p1Wins > mostWins)
        {
            mostWins = p1Wins;
            winner = 1;
        }
        if (p2Wins > mostWins)
        {
            mostWins = p2Wins;
            winner = 2;
        }
        if (p3Wins > mostWins)
        {
            mostWins = p3Wins;
            winner = 3;
        }
        if (p4Wins > mostWins)
        {
            mostWins = p4Wins;
            winner = 4;
        }

        if(mostWins == 3)
        {
            //Victory!
            winningPlayer = winner;
            return true;
        }else
        {
            winningPlayer = 0;
            return false;
        }


    }



    void EndGame(int winningPlayer)
    {
        List<int> players = new List<int>();

        players.Add(1);
        players.Add(2);
        players.Add(3);
        players.Add(4);

        players.Remove(winningPlayer);

        gameOverPanel.SetActive(true);

        Text[] textObjects = gameOverPanel.GetComponentsInChildren<Text>();
        foreach (Text t in textObjects)
        {
            if (t.text.Contains("[X]"))
            {
                t.text = t.text.Replace("[X]", characterNames[winningPlayer].ToString());
            }

        }

        Time.timeScale = 0;

    }

    public void RestartLevel()
    {
        Time.timeScale = 1;
        SceneManager.LoadScene(1);

    }

    public void ReturnToMainMenu()
    {
        SceneManager.LoadScene(0);

    }

}
