using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class SunDown : Singleton.Singleton<SunDown> {
    private Volume vol;
    private void Start() {
        vol = GetComponent<Volume>();
    }

    public void Execute() {
        StartCoroutine(Exec());
    }

    //bloom
    private float intensity = 0.5f;
    private Vector2 center = new Vector2(0.1f, 0.5f);

    //coloradj
    [ColorUsage(true, true)]
    public Color filter = new Color(135, 135, 135);

    //bloom
    private float curintensity = 0.293f;
    private Vector2 curcenter = new Vector2(0.5f, 0.5f);

    //coloradj
    [ColorUsage(true, true)]
    public Color curfilter = new Color(255, 255, 255);


    private void OnDestroy() {
        vol.profile.TryGet(typeof(Vignette), out Vignette vignette);
        vol.profile.TryGet(typeof(ColorAdjustments), out ColorAdjustments colorAdjustments);

        colorAdjustments.colorFilter = new ColorParameter(curfilter);
        vignette.center = new Vector2Parameter(curcenter);
        vignette.intensity = new ClampedFloatParameter(curintensity, 0, 1);
    }

    IEnumerator Exec() {print("exec");
        vol.profile.TryGet(typeof(Vignette), out Vignette vignette);
        vol.profile.TryGet(typeof(ColorAdjustments), out ColorAdjustments colorAdjustments);
        curfilter = colorAdjustments.colorFilter.value;


        float t = 0;
        float duration = 9f;

        while (true) {
            var x_value = Mathf.Lerp(curcenter.x, center.x, t);
            print(x_value);
            var y_value_int = Mathf.Lerp(curintensity, intensity, t);
            Color col = Color.Lerp(curfilter, filter, t);
            if (t <= 1) t += Time.deltaTime / duration;
            else break; // may krakk
            print("hi");
            
            vignette.intensity.Override(y_value_int);
            vignette.center.Override(new Vector2(x_value, vignette.center.value.y));
            colorAdjustments.colorFilter.hdr = false;
            colorAdjustments.colorFilter.Override(col);
            yield return new WaitForEndOfFrame();


        }
    }
}
