using System.Collections;
using UnityEngine;

public class GrindParticles : MonoBehaviour
{
    [Header("Active Status")]
    [SerializeField] [ReadOnly] bool m_active;

    [Header("Audio Sources")]
    [SerializeField] AudioSource m_driftStartAudio;
    [SerializeField] AudioSource m_driftAudio;

    [Header("Color Gradient")]
    [SerializeField] Gradient m_sparksGradient;

    int numTouching;
    float[] emissionRates = new float[] { 10, 10, 50, 3, 30, 32.5f, 10};
    Vector3 m_lastPos;
    Vector3[] m_defaultScales;
    float m_speed;

    IEnumerator co_updateColors;

    private void Start()
    {
        m_lastPos = transform.position;
        m_defaultScales = new Vector3[2];
        
        m_defaultScales[0] = transform.GetChild(3).localScale;
        m_defaultScales[1] = transform.GetChild(4).localScale;

        numTouching = 0;

        ParticleSystem[] systems = transform.GetComponentsInChildren<ParticleSystem>();

            for (int i = 0; i < systems.Length; i++)
            {
                var emission = systems[i].emission;

                emission.rateOverTime = 0;
            }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "TrackPiece")
        {
            numTouching++;

            if (numTouching > 0 && !m_active)
                StartGrindFX();
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "TrackPiece")
        {
            numTouching--;

            if (numTouching == 0 && m_active)
                StopGrindFX();
        }
    }

    void StartGrindFX()
    {
        m_driftStartAudio.Play();
        m_driftAudio.Play();

        ParticleSystem[] systems = transform.GetComponentsInChildren<ParticleSystem>();

        for (int i = 0; i < systems.Length - 1; i++)
        {
            var emission = systems[i].emission;

            emission.rateOverTime = emissionRates[i] * (1 + m_speed / 100);
        }

        m_active = true;

        if (co_updateColors != null)
            StopCoroutine(co_updateColors);

        co_updateColors = UpdateSparkColors();

        StartCoroutine(co_updateColors);
    }

    IEnumerator UpdateSparkColors()
    {
        m_lastPos = transform.position;

        while (m_active)
        {
            m_speed = Vector3.Distance(m_lastPos, transform.position) / Time.fixedDeltaTime;

            transform.GetChild(3).localScale = m_defaultScales[0] * m_speed / 20;
            transform.GetChild(4).localScale = m_defaultScales[1] * m_speed / 20;

            ParticleSystem[] systems = transform.GetComponentsInChildren<ParticleSystem>();

            for (int i = 0; i < systems.Length - 1; i++)
            {
                var main = systems[i].main;

                main.startColor = m_sparksGradient.Evaluate(m_speed / 100);
            }

            m_lastPos = transform.position;

            yield return null;
        }
    }

    void StopGrindFX()
    {
        m_driftAudio.Stop();

        ParticleSystem[] systems = transform.GetComponentsInChildren<ParticleSystem>();

        for (int i = 0; i < systems.Length - 1; i++)
        {
            var emission = systems[i].emission;

            emission.rateOverTime = 0;
        }

        m_active = false;
    }
}
