using System;
using Managers;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace UI_and_Audio
{
    public class InputBufferOnSceneLoad : MonoBehaviour
    {
        [SerializeField] private float delayInSeconds = 2f;
        [SerializeField] Button[] levelButtons;

        private Button[] allButtons;

        void Start()
        {
            // Find all buttons in the scene
            allButtons = FindObjectsOfType<Button>();
        
            // Disable all buttons immediately
            foreach (var button in allButtons)
            {
                button.interactable = false;
            }
        
            // Use UniRx Timer to wait for specified seconds
            Observable.Timer(TimeSpan.FromSeconds(delayInSeconds))
                .Subscribe(_ => EnableButtons())
                .AddTo(this);
        }

        private void EnableButtons()
        {
            foreach (var button in allButtons)
            {
                button.interactable = true;
            }

            for (int i = GlobalData.lastCompletedLevel + 1; i < levelButtons.Length; i++)
            {
                levelButtons[i].interactable = false;
            }
        }
    }
}
