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

public class LevelController : MonoBehaviour
{
    public TextMeshProUGUI numRootText;
    public int maxRoot = 10;
    public int numRoot = 0;
    public string nextScene;
    public float StartDelay;
    public float WinDelay;
    public float LoseDelay;

    public Animator cameraAnimator;

    [Header("HUDElements")]
    public GameObject[] hudElements;

    [Header("WinElements")]
    public GameObject winPanel;

    [Header("LoseElements")]
    public GameObject losePanel;
    public GameObject deathCamera;

    private void Start()
    {
        StartCoroutine(GameReady());
    }

    private void Update()
    {
        if (Manager.Instance.win || Manager.Instance.lost || !Manager.Instance.readyToPlay)
        {
            if (hudElements[0].activeSelf) for (int j = 0; j < hudElements.Length; j++) hudElements[j].SetActive(false);
        }
        else
        {
            if (!hudElements[0].activeSelf) for (int j = 0; j < hudElements.Length; j++) hudElements[j].SetActive(true);
        }
        

        if (numRoot == maxRoot)
        {
            Manager.Instance.DestroyTileBlink();
        }
        int i = maxRoot - numRoot;
        numRootText.text = /*"Roots: " +*/ i.ToString();
    }

    public void EndGame(bool win)
    {
        StartCoroutine(win?WinCoroutine():LoseCoroutine());
    }

    IEnumerator GameReady()
    {
        cameraAnimator.Play("InLevelCam");
        yield return new WaitForEndOfFrame();
        if (cameraAnimator != null) cameraAnimator.Play("BackCam");
        yield return new WaitForSeconds(2);
        if (cameraAnimator != null) cameraAnimator.Play("LevelCam");
        yield return new WaitForSeconds(2);
        Manager.Instance.StartPlaying();
    }

    public void p_Win()
    {
        TransitionManager.instance.ChangeScene(nextScene);
    }

    public void p_NextLevel(string SceneName)
    {
        cameraAnimator.Play("OutLevelCam");
        TransitionManager.instance.ChangeScene(SceneName);
    }

    public void p_Retry()
    {
        TransitionManager.instance.ResetScene();
    }

    public void p_ToMenu()
    {
        TransitionManager.instance.ChangeScene("MainMenu");
    }

    IEnumerator WinCoroutine()
    {
        DisableHUDElements();

        if (winPanel != null) winPanel.SetActive(true);

        if (cameraAnimator != null) cameraAnimator.Play("BackCam");

        yield return new WaitForSecondsRealtime(4);

        if (cameraAnimator != null) cameraAnimator.Play("WinLoseCam");

        yield return new WaitForSecondsRealtime(2);

    }

    IEnumerator LoseCoroutine()
    {
        DisableHUDElements();

        Vector3 _newDeathCamPos = deathCamera.transform.position;

        GameObject[] _plants = Manager.Instance.m_plants;

        for (int i = 0; i < _plants.Length; i++)
        {
            if (_plants[i].GetComponent<PlantsController>().ActualNeeds.alimentHave <= 0)
            {
                _newDeathCamPos = new Vector3(_plants[i].transform.position.x, deathCamera.transform.position.y, deathCamera.transform.position.z);
                break;
            }
        }

        deathCamera.transform.position = _newDeathCamPos;

        if (losePanel != null) losePanel.SetActive(true);

        if (cameraAnimator != null) cameraAnimator.Play("DeathCam"); 

        yield return new WaitForSecondsRealtime(4);

        if (cameraAnimator != null) cameraAnimator.Play("BackCam");

        yield return new WaitForSecondsRealtime(2);

        if (cameraAnimator != null) cameraAnimator.Play("WinLoseCam");

        yield return new WaitForSecondsRealtime(2);

    }

    void DisableHUDElements()
    {
        if (hudElements.Length != 0)
        {
            for (int i = 0; i < hudElements.Length; i++)
            {
                hudElements[i].SetActive(false);
            }
        }
    }
}
