using System;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UIElements;
using VOID.ScriptableObjects.Player;

namespace VOID.UserInterface.Game.LevelUp
{
    public class LevelUpOptionUI : MonoBehaviour
    {
        public event Action OnSelect;
        
        public static LevelUpOptionUI InitNew(GameObject gameObject, VisualElement parentWrapperElement, LevelUpOption option)
        {
            // New instance to manage this option
            var instance = gameObject.AddComponent<LevelUpOptionUI>();
            
            // Get all needed visual elements
            var templateElement = GameUI.current.levelUpOptionTemplate.CloneTree().contentContainer;
            templateElement.Q<Button>("root-level-up-option").clicked += () => instance.OnSelect?.Invoke();
            templateElement.Q<Label>("title").text = option.displayName;
            templateElement.Q("image").style.backgroundImage = option.displayIcon;
            templateElement.Q<Label>("description").text = new StringBuilder()
                .AppendJoin("\n", option.effects.Select(effect => effect.GetSimpleDescription()))
                .AppendLine(option.displayDescription)
                .ToString();
            
            // Put this option into LevelUpUI
            parentWrapperElement.Add(templateElement);

            // Bind UI to this script + make it pickable
            templateElement.dataSource = instance;
            templateElement.pickingMode = PickingMode.Position;
            
            return instance;
        }
    }
}
