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

namespace KitsuneCafe.UI
{
    public readonly struct HeadlessModalElementInstance : IUiElement
    {
        public readonly string Content;

        private readonly ModalElement so;
        public readonly VisualTreeAsset VisualTreeAsset => so.VisualTreeAsset;

        public HeadlessModalElementInstance(string content, ModalElement so)
        {
            Content = content;
            this.so = so;
        }

        public VisualElement Instantiate()
        {
            var instance = VisualTreeAsset.CloneTree();
            Configure(instance);
            return instance;
        }

        public void Configure(VisualElement ve)
        {
            ve.Q(so.HeaderId).style.display = DisplayStyle.None;
            ve.Q<Label>(so.ContentId).text = Content;
        }

        public IEnumerable<BaseUiEffect> GetEffectsFor(UiEvent timing)
        {
            return so.GetEffectsFor(timing);
        }
    }
    public readonly struct ModalElementInstance : IUiElement
    {
        public readonly string Title;
        public readonly string Content;

        private readonly ModalElement so;
        public readonly VisualTreeAsset VisualTreeAsset => so.VisualTreeAsset;

        public ModalElementInstance(string title, string content, ModalElement so)
        {
            Title = title;
            Content = content;
            this.so = so;
        }

        public VisualElement Instantiate()
        {
            var instance = VisualTreeAsset.CloneTree();
            Configure(instance);
            return instance;
        }

        public void Configure(VisualElement ve)
        {
            ve.Q<Label>(so.TitleId).text = Title;
            ve.Q<Label>(so.ContentId).text = Content;
        }

        public IEnumerable<BaseUiEffect> GetEffectsFor(UiEvent timing)
        {
            return so.GetEffectsFor(timing);
        }
    }

    [CreateAssetMenu(menuName = KitsuneCafeMenu.UiElement + "Modal")]
    public class ModalElement : UiElementSo
    {
        [SerializeField]
        private string headerId = "header";
        public string HeaderId => headerId;

        [SerializeField]
        private string titleId = "title";
        public string TitleId => titleId;

        [SerializeField]
        private string bodyId = "body";
        public string BodyId => bodyId;

        [SerializeField]
        private string contentId = "content";
        public string ContentId => contentId;


        public HeadlessModalElementInstance CreateHeadless(string content)
        {
            return new HeadlessModalElementInstance(content, this);
        }

        public ModalElementInstance Create(string title, string content)
        {
            return new ModalElementInstance(title, content, this);
        }
    }
}
