using UdonSharp;
using UnityEngine;

namespace Marro.PacManUdon
{
    public class PelletManager : SyncedObject
    {
        Pellet[] pellets;
        Renderer[] pelletRenderers;
        Animator[] powerPellets;

        bool powerPelletBlinkEnabled;
        float powerPelletBlinkToggleInterval;
        float powerPelletBlinkProgress;
        bool powerPelletBlinkCurrentlyVisible;

        public void Initialize()
        {
            gameObject.SetActive(true);
            pellets = GetComponentsInChildren<Pellet>(includeInactive: true);

            pelletRenderers = new Renderer[pellets.Length];
            for (int i = 0; i < pelletRenderers.Length; i++)
            {
                pelletRenderers[i] = pellets[i].GetComponent<Renderer>();
            }

            powerPellets = GetComponentsInChildren<Animator>(true);
            powerPelletBlinkToggleInterval = PacManConstants.GetPowerPelletBlinkToggleInterval();
            SetPowerPelletsBlink(false);
        }

        public override void SyncedUpdate()
        {
            if (!powerPelletBlinkEnabled)
            {
                return;
            }

            powerPelletBlinkProgress += networkManager.SyncedDeltaTime;
            if (powerPelletBlinkProgress >= powerPelletBlinkToggleInterval)
            {
                // Debug.Log($"{gameObject} PowerPelletBlink toggle");
                powerPelletBlinkProgress -= powerPelletBlinkToggleInterval;
                powerPelletBlinkCurrentlyVisible = !powerPelletBlinkCurrentlyVisible;
                SetPowerPelletsVisible(powerPelletBlinkCurrentlyVisible);
            }
        }

        void SetPowerPelletsVisible(bool visible)
        {
            // Debug.Log($"{gameObject} SetPowerPelletVisible {visible}, powerPellets.Length: {powerPellets.Length}");
            foreach (Animator powerPellet in powerPellets)
            {
                powerPellet.SetBool("Visible", visible);
            }
        }

        public void SetPowerPelletsBlink(bool enabled)
        {
            // Debug.Log($"{gameObject} SetPowerPelletBlink {enabled}");
            powerPelletBlinkEnabled = enabled;

            powerPelletBlinkCurrentlyVisible = true;
            powerPelletBlinkProgress = 0;
            SetPowerPelletsVisible(true);
        }

        public void FreezePowerPelletsBlink(bool frozen)
        {
            powerPelletBlinkEnabled = !frozen;
        }

        public void RestoreAllPellets()
        {
            foreach (var pellet in pellets)
            {
                pellet.gameObject.SetActive(true);
            }

            foreach (Renderer pelletRenderer in pelletRenderers)
            {
                pelletRenderer.enabled = true;
            }
        }

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

        }

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

        public int PelletCount => pellets.Length;
    }
}