namespace Marro.PacManUdon
{
    using UdonSharp;
    using UnityEngine;
    using VRC.SDK3.Components;
    using VRC.SDKBase;
    using VRC.Udon;

    public class PelletManager : UdonSharpBehaviour
    {
        [SerializeField] Pellet[] pellets;
        VRCObjectPool pelletPool;
        Renderer[] pelletRenderers;
        Animator[] powerPellets;


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

        public void Initialize(VRCObjectPool pelletPool)
        {
            gameObject.SetActive(true);
            this.pelletPool = pelletPool;
            pelletRenderers = new Renderer[pelletPool.Pool.Length];
            for (int i = 0; i < pelletRenderers.Length; i++)
            {
                pelletRenderers[i] = pelletPool.Pool[i].GetComponent<Renderer>();
            }

            powerPellets = GetComponentsInChildren<Animator>(true);
            Debug.Log($"{gameObject} Initialized, powerPellets: {powerPellets}");
            powerPelletBlinkToggleInterval = PacManConstants.GetPowerPelletBlinkToggleInterval();
            SetPowerPelletsBlink(false);
        }

        public void Initialize()
        {
            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);
        }

        void Update()
        {
            if (!powerPelletBlinkEnabled)
            {
                return;
            }

            powerPelletBlinkProgress += Time.deltaTime;
            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 (GameObject pellet in pelletPool.Pool)
            {
                if (pelletPool.TryToSpawn() == false)
                {
                    break;
                }
            }

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