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

public class IntroController : MonoBehaviour, IResettable
{

    [SerializeField]
    private TMP_Text m_levelText;

    [SerializeField]
    private TextMeshProUGUI m_detailsText;

    [SerializeField]
    private TextMeshProUGUI m_timerText;

    [SerializeField]
    private TextMeshProUGUI m_goText;

    [SerializeField]
    private Variables.Reference<bool> m_freezePlayer;

    [SerializeField]
    private LevelManager m_manager;

    [SerializeField]
    private Image m_backGround;

    [SerializeField, BoxGroup("Settings")]
    private float LevelFadeTime = 2;
    [SerializeField, BoxGroup("Settings")]
    private float Details1 = 1;
    [SerializeField, BoxGroup("Settings")]
    private float Details2 = 0.1f;
    [SerializeField, BoxGroup("Settings")]
    private float Details3 = 0.8f;
    [SerializeField, BoxGroup("Settings")]
    private float Fadetime = 1;
    [SerializeField, BoxGroup("Settings")]
    private float gotime = 0.5f;


    private IEnumerator Start()
    {
        m_backGround.gameObject.SetActive(true);
        m_timerText.gameObject.SetActive(false);
        m_levelText.text = m_manager.levelName;

        m_freezePlayer.Value = true;


        yield return StartCoroutine(LevelName(LevelFadeTime));
        yield return StartCoroutine(DetailText(Details1, Details2, Details3));
        yield return StartCoroutine(Fade(Fadetime));
        yield return StartCoroutine(Go(gotime));
    }



    private IEnumerator LevelName(float time)
    {
        m_levelText.gameObject.SetActive(true);

        float start = 0;
        float end = 1;

        float elaspedTime = 0;
        Color color = m_levelText.color;

        while (elaspedTime < time)
        {
            float alpha = Mathf.Sin(Mathf.PI * (elaspedTime / time));
            color.a = alpha;
            m_levelText.color = color;

            yield return new WaitForEndOfFrame();
            elaspedTime += Time.deltaTime;
        }

        color.a = 0;
        m_levelText.color = color;

        m_levelText.gameObject.SetActive(false);
    }

    private IEnumerator DetailText(float time,float time2, float time3)
    {
        m_timerText.gameObject.SetActive(true);
        yield return new WaitForSeconds(time);


        m_detailsText.gameObject.SetActive(true);
        m_detailsText.text = "\nSeconds\n\n";
        yield return new WaitForSeconds(time);

        foreach(char c in m_manager.levelPrompt)
        {
            m_detailsText.text += c;
            yield return new WaitForSeconds(time2);
        }

        yield return new WaitForSeconds(time3);
    }

    private IEnumerator Fade(float time)
    {

        float elaspedTime = 0;
        Color textColor = m_detailsText.color;
        Color backgroundColor = m_backGround.color;

        

        while (elaspedTime < time)
        {
            textColor.a = 1-(elaspedTime/time);
            backgroundColor.a = 1-(elaspedTime/time);

            m_backGround.color = backgroundColor;
            m_detailsText.color = textColor;

            yield return new WaitForEndOfFrame();
            elaspedTime+= Time.deltaTime;

        }

        textColor.a = 0;
        backgroundColor.a = 0;

        m_backGround.color = backgroundColor;
        m_detailsText.color = textColor;

        m_detailsText.gameObject.SetActive(false);
        m_backGround.gameObject.SetActive(false);


    }

    private IEnumerator Go(float time)
    {
        m_goText.gameObject.SetActive(true);
        yield return new WaitForSeconds(time);
        m_freezePlayer.Value = false;
        m_goText.gameObject.SetActive(false);
    }

    public void OnLevelLoad()
    {

    }

    public IEnumerator OnResetStart(float time)
    {
        yield break;
    }

    private IEnumerator ResetRoutine(float time)
    {
        m_goText.gameObject.SetActive(true);
        yield return new WaitForSeconds(time);
        m_goText.gameObject.SetActive(false);
    }

    public void OnResetEnd()
    {
        StartCoroutine(ResetRoutine(gotime));
    }
}
