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

namespace KitsuneCafe.UI
{
    public enum UiEvent
    {
        OnCreate,
        OnDestroy
    }

    public interface IUiElement
    {
        VisualTreeAsset VisualTreeAsset { get; }

        void Configure(VisualElement element);

        IEnumerable<BaseUiEffect> GetEffectsFor(UiEvent timing);
    }

    [Serializable]
    public struct EventEffectPair
    {
        [SerializeField]
        private UiEvent uiEvent;
        public readonly UiEvent Event => uiEvent;

        [SerializeField]
        private BaseUiEffect effect;
        public readonly BaseUiEffect Effect => effect;
    }

    [CreateAssetMenu(menuName = KitsuneCafeMenu.UiElement + "Element")]
    public abstract class UiElementSo : ScriptableObject, IUiElement
    {
        [SerializeField]
        private VisualTreeAsset visualTreeAsset;
        public VisualTreeAsset VisualTreeAsset => visualTreeAsset;

        [SerializeField]
        private List<EventEffectPair> effects;
        private ILookup<UiEvent, BaseUiEffect> effectLookup;
        public ILookup<UiEvent, BaseUiEffect> Effects => effectLookup ??= effects.ToLookup(p => p.Event, p => p.Effect);

        public virtual void Configure(VisualElement ve) { }

        public IEnumerable<BaseUiEffect> GetEffectsFor(UiEvent timing)
        {
            return Effects[timing];
        }
    }
}
