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

public class ButtonPanelUIScript : MonoBehaviour {

    private Button buildButton;
    private IMGUIContainer buttonContainer;
    bool building = false;
    private VisualTreeAsset MenuItemTemplate;
    private static List<PlacedObjectTypeSO> buildableItems = new List<PlacedObjectTypeSO>();
    private static List<UIMenuItem> menuItems = new List<UIMenuItem>();

    private void OnEnable() {
        VisualElement rootVisualElement = GetComponent<UIDocument>().rootVisualElement;

        buildButton = rootVisualElement.Q<Button>("BuildButton");
        buildButton.RegisterCallback<ClickEvent>(ev => StartBuilding());

        MenuItemTemplate = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI Toolkit/MenuItemTemplate.uxml");
        buttonContainer = rootVisualElement.Q<IMGUIContainer>("BuildPerfabsPanel");

        CreateGui();
    }

    private void CreateGui() {
        LoadAllBuildableItems();

        GenerateMenuItems();
    }

    private void StartBuilding() {
        buttonContainer.style.display = building ? DisplayStyle.None : DisplayStyle.Flex;
        building = !building;
    }

    private void GenerateMenuItems() {
        foreach (UIMenuItem item in menuItems) {
            TemplateContainer templateContainer = MenuItemTemplate.Instantiate();
            ScrollView scrollView = templateContainer.Q<ScrollView>("BuildableItemsList");
            Button button = templateContainer.Q<Button>("Button");
            button.text = item.MenuText;

            button.clicked += () => {
                scrollView.style.display =
                    scrollView.style.display == DisplayStyle.None ? DisplayStyle.Flex : DisplayStyle.None;
            };

            foreach (TemplateContainer i in item.GetBuildableItemsList()) {
                templateContainer.Q<ScrollView>("BuildableItemsList").Add(i);
            }

            buttonContainer.Add(templateContainer);
        }
    }

    private void LoadAllBuildableItems() {
        buildableItems.Clear();

        string[] allMenuPaths = Directory.GetFiles("Assets/Data/UI/Menus", "*.asset", SearchOption.AllDirectories);
        string[] allBuildablePaths = Directory.GetFiles("Assets/Data/UI/Buildables", "*.asset", SearchOption.AllDirectories);

        foreach (string path in allBuildablePaths) {
            string cleanedPath = path.Replace("\\", "/");
            buildableItems.Add((PlacedObjectTypeSO)AssetDatabase.LoadAssetAtPath(cleanedPath, typeof(PlacedObjectTypeSO)));
        }

        foreach (string path in allMenuPaths) {
            string cleanedPath = path.Replace("\\", "/");
            UIMenuItem uIMenuItem = (UIMenuItem)AssetDatabase.LoadAssetAtPath(cleanedPath, typeof(UIMenuItem));
            uIMenuItem.SetBuildableItems(buildableItems.FindAll(i => i.category.Equals(uIMenuItem.category)));
            menuItems.Add(uIMenuItem);
        }
    }

}
