using UnityEngine;
using UnityEngine.UI;

public class WinConditionChecker : MonoBehaviour
{
    public Population left;
    public Population right;

    public Text VictoryText;

    private bool gameStarted;

    private void OnEnable()
    {
        ControlsUI.StartGame += StartGame;
    }

    private void OnDisable()
    {
        ControlsUI.StartGame -= StartGame;
    }


    private void StartGame()
    {
        Invoke("GameStarted", 1); 
    }

    private void GameStarted()
    {
        gameStarted = true;
    }

    private void Update()
    {
        if (gameStarted)
        {
            if (left.noOfHealthy == 0)
            {
                Victory("Right player wins !");
            }
            else if (right.noOfHealthy == 0)
            {
                Victory("Left player wins !");
            }
            else if(right.noOfDead == 0 && right.noOfContaminated == 0 && left.noOfDead == 0 && left.noOfContaminated == 0)
            {
                if (right.noOfHealthy > left.noOfHealthy)
                {
                    Victory("Right player wins !");
                }
                if (right.noOfHealthy < left.noOfHealthy)
                {
                    Victory("Left player wins !");
                }
                else
                {
                    Victory("Draw !");
                }
            }
        }

    }

    private void Victory(string victoryText)
    {

        VictoryText.text = victoryText;
        GetComponent<Animator>().SetBool("Win",true);
    }
}

