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

public class PostProcessHandler : MonoBehaviour
{
    [Header("Player")]
    [SerializeField] RacerCore m_localPlayer = null;

    [Header("Post-Processing Components")]
    [SerializeField] PostProcessVolume m_postVolume;
    [SerializeField] PostProcessProfile m_postProfile;

    [Header("Post-Processing Settings")]
    [SerializeField] [Range(-50, 50)] float m_defaultValue = 0;
    [SerializeField] [Range(-50, 50)] float m_boostValue = -45f;
    [SerializeField] [Range(0.1f, 10)] float m_rateOfChange = 5f;

    IEnumerator co_startBlur, co_endBlur;

    // DEFAULT METHODS
    #region
    void OnValidate()
    {
        if (!m_postVolume)
            m_postVolume = GetComponent<PostProcessVolume>();

        if (m_postVolume && !m_postProfile)
            m_postProfile = m_postVolume.profile;
    }
    void Start()
    {
        if (!m_postVolume)
            m_postVolume = GetComponent<PostProcessVolume>();

        if (m_postVolume && !m_postProfile)
            m_postProfile = m_postVolume.profile;

        if (m_localPlayer)
            return;

        StartCoroutine(TryGetPlayer());
    }
    void OnEnable()
    {
        if (!m_localPlayer)
            return;

        m_localPlayer.Controller.OnBoostStart += StartBoostBlur;
        m_localPlayer.Controller.OnBoostEnd += EndBoostBlur;
    }
    void OnDisable()
    {
        if (!m_localPlayer)
            return;

        m_localPlayer.Controller.OnBoostStart -= StartBoostBlur;
        m_localPlayer.Controller.OnBoostEnd -= EndBoostBlur;
    }
    #endregion


    IEnumerator TryGetPlayer()
    {
        while (!m_localPlayer)
        {
            m_localPlayer = RacerCore.LocalRacer;

            yield return null;
        }

        m_localPlayer.Controller.OnBoostStart += StartBoostBlur;
        m_localPlayer.Controller.OnBoostEnd += EndBoostBlur;
    }
    
    void StartBoostBlur()
    {
        if (!m_postProfile)
            return;

        if (co_startBlur != null)
            return;


    }
    void EndBoostBlur()
    {

    }
    IEnumerator ShiftBlurToBoost()
    {
        float _currentDistortionValue = m_postProfile.GetSetting<LensDistortion>().intensity.value;

        while (m_postProfile.GetSetting<LensDistortion>().intensity.value != m_boostValue)
        {
            _currentDistortionValue = m_postProfile.GetSetting<LensDistortion>().intensity.value = Mathf.MoveTowards(_currentDistortionValue, m_boostValue, m_rateOfChange * Time.deltaTime);

            if (_currentDistortionValue <= -20 && !m_postProfile.GetSetting<MotionBlur>().enabled.value)
                m_postProfile.GetSetting<MotionBlur>().enabled.value = true;

            yield return null;
        }
    }
    IEnumerator ShiftBlurToDefault()
    {
        while (m_postProfile.GetSetting<LensDistortion>().intensity.value != m_defaultValue)
        {
            m_postProfile.GetSetting<LensDistortion>().intensity.value = m_rateOfChange * Time.deltaTime;

            if (m_postProfile.GetSetting<LensDistortion>().intensity.value <= -20 && !m_postProfile.GetSetting<MotionBlur>().enabled.value)
                m_postProfile.GetSetting<MotionBlur>().enabled.value = true;

            yield return null;
        }

        yield break;
    }
}
