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

public class PlayerDeathVFX : MonoBehaviour
{
    PostProcessVolume postProcess;

    public float blendDuration = 0.5f;

    private Coroutine blendCoroutine;

    private void Awake()
    {
        postProcess = GetComponent<PostProcessVolume>();
    }

    public void BlendIn()
    {
        StopBlendCoroutine();
        blendCoroutine = StartCoroutine(BlendWeight(0f, 1f, blendDuration));
    }

    public void BlendOut()
    {
        StopBlendCoroutine();
        blendCoroutine = StartCoroutine(BlendWeight(1f, 0f, blendDuration));
    }

    public void ResetWeight()
    {
        postProcess.weight = 0;
    }

    public void BlendTo(float targetWeight)
    {
        StopBlendCoroutine();
        blendCoroutine = StartCoroutine(BlendWeight(postProcess.weight, targetWeight, blendDuration));
    }

    private IEnumerator BlendWeight(float startWeight, float endWeight, float duration)
    {
        float elapsedTime = 0f;

        while (elapsedTime < duration)
        {
            elapsedTime += Time.deltaTime;
            float t = Mathf.Clamp01(elapsedTime / duration);
            postProcess.weight = Mathf.Lerp(startWeight, endWeight, t);
            yield return null;
        }

        postProcess.weight = endWeight;
    }

    private void StopBlendCoroutine()
    {
        if (blendCoroutine != null)
        {
            StopCoroutine(blendCoroutine);
        }
    }
}
