using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

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

    [Header("Boost Effect")]
    [SerializeField] ParticleSystem m_boostParticles;

    private void OnEnable()
    {
        if (!m_localPlayer)
            return;

        m_localPlayer.Controller.OnBoostStart += StartBoostVisual;
        m_localPlayer.Controller.OnBoostEnd += StopBoostVisual;
    }

    private void OnDisable()
    {
        if (!m_localPlayer)
            return;

        m_localPlayer.Controller.OnBoostStart -= StartBoostVisual;
        m_localPlayer.Controller.OnBoostEnd -= StopBoostVisual;
    }

    private void Start()
    {
        m_boostParticles = GetComponent<ParticleSystem>();

        if (m_localPlayer)
            return;

        StartCoroutine(TryGetPlayer());
    }

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

            yield return null;
        }
        
        m_localPlayer.Controller.OnBoostStart += StartBoostVisual;
        m_localPlayer.Controller.OnBoostEnd += StopBoostVisual;
    }

    void StartBoostVisual()
    {
        if (m_boostParticles)
            m_boostParticles.Play();
    }

    void StopBoostVisual()
    {
        if (m_boostParticles)
            m_boostParticles.Stop();
    }
}
