using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.SceneManagement;

public class RewindScript : MonoBehaviour
{

   public TextMeshProUGUI text;

   private bool showing;
   
   private DateTime hour;
   
   public PostProcessProfile profile;
   
   public Gradient colorGradient;

   public AudioClip rewindSound;
   
   private ColorGrading colorGradingLayer;
   
   private void Start()
   {
      
      
      InvokeRepeating("ChangeShowing",0, 0.5f);
      
      profile.TryGetSettings(out colorGradingLayer);
      hour = DateTime.Now.AddYears(5);
      
      colorGradingLayer.enabled.value = true;

      Color c = colorGradingLayer.colorFilter.value;
      DOTween.To(() => c, x => c = x, colorGradient.Evaluate(GetHour()), 2).OnUpdate(() => 
      { 
         colorGradingLayer.colorFilter.value = c;
      });
      
      Invoke("PlaySound",1.4f);
   }

   private void PlaySound()
   {
      AudioController.Instance.PlaySound(rewindSound);
   }
   
   private float GetHour()
   {
      return Mathf.Clamp(((float) hour.Hour + ((float)hour.Minute / 60)) / 23, 0, 1);
   }

   private void ChangeShowing()
   {
      if (showing)
         text.text = "";
      else
         text.text = "RELOADING LIFE";

      showing = !showing;
   }

   public void LoadGame()
   {
      SceneManager.LoadScene(0);
   }
}
