using System;
using System.Collections.Generic;
using UnityEngine;

namespace AlicizaX.UI
{
    [DisallowMultipleComponent]
    [AddComponentMenu("Controller/控制器状态")]
    public class UXControllerStateRecorder : MonoBehaviour
    {
        [SerializeField] private int _id;
        [SerializeField] private UXController _controller;

        internal int ID => _id;
        internal UXController Controller => _controller;

        [Serializable]
        public class StateEntry
        {
            [SerializeReference] public ControllerStateBase State = null;
            public string ControllerName = string.Empty;
            public int ControllerIndex = 0;
        }

        [SerializeField] private List<StateEntry> _stateEntries = new List<StateEntry>();

        // private void Awake()
        // {
        //     foreach (var entry in _stateEntries)
        //     {
        //         entry.State.Init(this);
        //     }
        // }

        internal void OnControllerIndexChanged(string controllerName, int selectedIndex)
        {
            var entrys = _stateEntries.FindAll(t => t.ControllerName.Equals(controllerName));
            if (entrys != null)
            {
                foreach (var entry in entrys)
                {
                    entry.State.Execute(this, entry.ControllerIndex, selectedIndex);
                }
            }
        }
    }
}
