using System;
using UnityEngine;
using UnityEngine.UIElements;
using VOID.Generic.SaveLoad;

namespace VOID.UserInterface.MainMenu
{
    public class ProfileUI : MonoBehaviour
    {
        public static ProfileUI InitNew(
            GameObject gameObject,
            VisualElement parentWrapperElement,
            PlayerProfile profile,
            Action<PlayerProfile> onSelect,
            Action<PlayerProfile> onDelete
        )
        {
            // New instance to manage this option
            var instance = gameObject.AddComponent<ProfileUI>();

            // Get all needed visual elements
            var templateElement = GameUI.current.profileTemplate.CloneTree().contentContainer;
            templateElement.Q<Button>("select-profile").text = profile.name;
            templateElement.Q<Button>("select-profile").clicked += () => onSelect.Invoke(profile);
            templateElement.Q<Button>("delete-profile").clicked += () =>
            {
                onDelete.Invoke(profile);
                parentWrapperElement.Remove(templateElement);
                Destroy(instance);
            };
            
            // 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;
        }
    }
}
