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

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

    [Header("Testing")]
    [SerializeField] [Range(0, 1)] float m_currentPercentage = 0;

    private void OnValidate()
    {
        UpdateDisplay(m_currentPercentage);
    }

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

        m_localPlayer.Items.OnItemChargeUpdate += UpdateDisplay;
    }

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

        m_localPlayer.Items.OnItemChargeUpdate -= UpdateDisplay;
    }

    private void Start()
    {
        if (m_localPlayer)
            return;

        StartCoroutine(TryGetPlayer());
    }

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

            yield return null;
        }

        m_localPlayer.Items.OnItemChargeUpdate += UpdateDisplay;
    }

    void UpdateDisplay(float _perc)
    {
        transform.localScale = new Vector3(_perc, transform.localScale.y, 1);
    }
}
