using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[CreateAssetMenu(fileName = "New Menu", menuName = "Own UI/New Menu", order = 1)]
public class UIMenuItem : ScriptableObject {

    public string MenuText;
    public ScrollView MenuItemsList;
    public Button MenuName;
    public bool menuOpen = false;
    public PlacedObjectTypeSO.ObjectCategory category;

    public VisualTreeAsset buildableItemTemplate;

    public List<PlacedObjectTypeSO> BuildableItems { get; set; }

    private void OnEnable() {
        buildableItemTemplate = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Scripts/UI/ItemDatabase/BuildableObjectTemplate.uxml");

        MenuItemsList = new ScrollView();
        MenuItemsList.style.visibility = Visibility.Hidden;
    }

    public void SetBuildableItems(List<PlacedObjectTypeSO> items) {
        BuildableItems = items;
    }

    internal List<TemplateContainer> GetBuildableItemsList() {
        List<TemplateContainer> result = new List<TemplateContainer>();
        foreach (PlacedObjectTypeSO item in BuildableItems) {
            TemplateContainer buildableItem = buildableItemTemplate.Instantiate();
            Button button = buildableItem.Q<Button>("BuildButton");
            button.text = item.ButtonText;

            button.RegisterCallback<ClickEvent>(ev => SetClickedItem(item));
            result.Add(buildableItem);
        }

        return result;
    }

    private void SetClickedItem(PlacedObjectTypeSO item) {
        GroundGridControllerScript.Instance.SetActiveObejctToPlace(item);
    }
}