using System.Collections.Generic;
using AsteroidGame.Entities;
using UnityEngine;
using UnityEngine.UIElements;
using AsteroidGame.Handlers;
using AsteroidGame.ScriptableObjects;
using UnityEngine.Serialization;

namespace AsteroidGame.UI
{
    public class BuildMenuUiController : MonoBehaviour
    {
        [FormerlySerializedAs("_availableSStructuresObject")]
        [FormerlySerializedAs("_availableStructuresObject")]
        [Header("Structures")]
        [SerializeField] private SoStructureBaseList _availableSoStructuresObject;

        private VisualElement _mRoot;
        private VisualElement _mSlotContainer;
        private List<StructureBase> _buildings = new();
        private BuildingHandler _buildingHandler;

        private void OnEnable()
        {
            _buildings = _availableSoStructuresObject._structureList;
            _buildingHandler = FindObjectOfType<BuildingHandler>();

            //Store the root from the  UI Document component
            _mRoot = GetComponent<UIDocument>().rootVisualElement;
            _mSlotContainer = _mRoot.Q<VisualElement>("Menu");

            for (int i = 0; i < _buildings.Count; i++)
            {
                StructureBase building = _buildings[i];
                BuildingButton button = new()
                {
                    name = building.name,
                    text = building.UiFriendlyName
                };
                button.RegisterCallback<ClickEvent, int>((evt, index) => { _buildingHandler.EnterBuildMode(index); },
                    i);
                _mSlotContainer.Add(button);
            }
        }
    }
}