using UnityEngine;

namespace Marro.PacManUdon
{
    [RequireComponent(typeof(Animator))]
    public class ScoreBonusDisplay : SyncedObject
    {
        private Animator animator;
        private float countdownSeconds;
        private bool countingDown;
        public void Initialize()
        {
            animator = GetComponent<Animator>();
            countdownSeconds = 0;
            countingDown = false;
            gameObject.SetActive(false);
        }

        public override void SyncedUpdate()
        {
            if (countingDown)
            {
                countdownSeconds -= networkManager.SyncedDeltaTime;
                if (countdownSeconds <= 0)
                {
                    Hide();
                }
            }
        }

        public void DisplayTemporarily(int score, int seconds)
        {
            Display(score);
            countdownSeconds = seconds;
            countingDown = true;
        }

        public void Display(int score)
        {
            gameObject.SetActive(true);
            animator.SetFloat("Score", score);
        }

        public void Hide()
        {
            countingDown = false;
            gameObject.SetActive(false);
        }

        public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
        {

        }

        public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
        {
            return true;
        }
    }
}
