using System;
using System.Collections;
using UnityEngine;

public class TimeManager : MonoBehaviour
{
    public float gameSpeed = 1f;
    public Texture2D skyboxNight;
    public Texture2D skyboxSunrise;
    public Texture2D skyboxDay;
    public Texture2D skyboxSunset;

    public Gradient gradientNightToSunrise;
    public Gradient gradientSunriseToDay;
    public Gradient gradientDayToSunset;
    public Gradient gradientSunsetToNight;
    public Light globalLight;

    public int minutes;
    public int Minutes {  get { return minutes; } set { minutes = value; OnMinutesChanged(value); } }
    public int hours;
    public int Hours { get { return hours; } set { hours = value; OnHoursChanged(value); } }
    public int days;
    public int Days { get { return days; } set { days = value; } }

    public float tempSecond;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        tempSecond += Time.deltaTime * gameSpeed;
        if(tempSecond >= 1)
        {
            Minutes++;
            tempSecond--;
        }
    }

    private void OnMinutesChanged(float value)
    {
        globalLight.transform.Rotate(Vector3.up, (1f / 1440f) * 360f, Space.World);
        if(value >= 60)
        {
            Hours++;
            Minutes -= 60;
        }
        if (Hours >= 24)
        {
            Hours -= 24;
            Days++;
        }
    }

    private void OnHoursChanged(float value)
    {
        switch (value)
        {
            case 6:
                StartCoroutine(LerpSkybox(skyboxNight, skyboxSunrise, 10f));
                StartCoroutine(LerpLight(gradientNightToSunrise, 10f));
                break;

            case 10:
                StartCoroutine(LerpSkybox(skyboxSunrise, skyboxDay, 10f));
                StartCoroutine(LerpLight(gradientSunriseToDay, 10f));
                break;

            case 18:
                StartCoroutine(LerpSkybox(skyboxDay, skyboxSunset, 10f));
                StartCoroutine(LerpLight(gradientDayToSunset, 10f));
                break;

            case 22:
                StartCoroutine(LerpSkybox(skyboxSunset, skyboxNight, 10f));
                StartCoroutine(LerpLight(gradientSunsetToNight, 10f));
                break;
        }
    }

    private IEnumerator LerpSkybox(Texture2D a, Texture2D b, float time)
    {
        RenderSettings.skybox.SetTexture("_Texture1", a);
        RenderSettings.skybox.SetTexture("_Texture2", b);
        RenderSettings.skybox.SetFloat("_Blend", 0);

        for (float i=0; i < time; i += Time.deltaTime)
        {
            RenderSettings.skybox.SetFloat("_Blend", i / time);
            yield return null;
        }

        RenderSettings.skybox.SetTexture("_Texture1", b);
    }

    private IEnumerator LerpLight(Gradient lightGradient, float time)
    {
        for (float i = 0; i < time; i += Time.deltaTime)
        {
            globalLight.color = lightGradient.Evaluate(i / time);
            RenderSettings.fogColor = globalLight.color;
            yield return null;
        }
    }
}
