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

public sealed class LocalSpaceList : MonoBehaviour
{
    [SerializeField]
    private Button[] buttons = null;
    [SerializeField]
    private Text label = null;

    public event Action<int> OnSelectObject = null;

    public void SetButtons(List<string> items = null) {
        label.gameObject.SetActive(items != null && items.Count > 0);
        for (int i = 0; i < buttons.Length; i++) {
            if (i < (items?.Count ?? 0))
            {
                buttons[i].gameObject.SetActive(true);
                buttons[i].GetComponentInChildren<Text>().text = items[i];
            }
            else
            {
                buttons[i].gameObject.SetActive(false);
            }
        }
    }

    private void Start()
    {
        for (int i = 0; i < buttons.Length; i++)
        {
            var id = i;
            buttons[i].onClick.AddListener(() => {
                OnButtonClick(id);
            });
        }
    }

    private void OnButtonClick(int id)
    {
        OnSelectObject?.Invoke(id);
    }
}
