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

public class SceneFader : MonoBehaviour {

    public Image img;
    public AnimationCurve fadeCurve;
    public GameObject mainMenu;
    public GameObject levelSelectMenu;

    public Button[] levelButtons;
    

    private void Start()
    {
        int levelReached = PlayerPrefs.GetInt("levelReached", 1);

        for (int i = 0; i < levelButtons.Length; i++)
        {
            if(i + 1 > levelReached)
            {
                levelButtons[i].interactable = false;
            }
            
        }

        img.gameObject.SetActive(true);
        StartCoroutine(FadeInAnimation());
    }

    public void FadeToLevelSelect()
    {
        StartCoroutine(FadeToLevelSelectAnimation());
    }

    IEnumerator FadeInAnimation()
    {
        float t = 1f;

        while(t > 0f)
        {
            t -= Time.deltaTime;
            float a = fadeCurve.Evaluate(t);
            img.color = new Color(0f, 0f, 0f, a);
            yield return 0;
        }


    }
    IEnumerator FadeToLevelSelectAnimation()
    {
        float t = 0f;

        while (t < 1f)
        {
            t += Time.deltaTime;
            float a = fadeCurve.Evaluate(t);
            img.color = new Color(0f, 0f, 0f, a);
            yield return 0;
        }
        StartCoroutine(FadeInAnimation());
        levelSelectMenu.SetActive(true);
        mainMenu.SetActive(false);
        

    }
}
