using Jovian.PopupSystem;
using Jovian.PopupSystem.UI;
using UnityEngine;

/// <summary>
/// Example: Using PopupSystem with dynamically instantiated UI elements.
///
/// When UI elements are created at runtime (e.g. inventory slots, party member portraits),
/// use InitializeTriggersInChildren to scan, bind, and configure triggers after instantiation.
/// The callback receives both the trigger (MonoBehaviour, for hierarchy queries) and the
/// view (behavior handler, for setting content).
/// </summary>
public class DynamicTriggersExample : MonoBehaviour {
    [SerializeField] PopupSettings popupSettings;
    [SerializeField] PopupReference popupReferencePrefab;
    [SerializeField] Transform canvasRoot;
    [SerializeField] Transform slotsContainer;
    [SerializeField] GameObject slotPrefab;

    IPopupSystem popupSystem;

    void Start() {
        popupSystem = new PopupSystem(popupSettings, popupReferencePrefab, canvasRoot);
        popupSystem.RegisterCategory(PopupCategory.Item, priority: 5);

        for(int i = 0; i < 5; i++) {
            Instantiate(slotPrefab, slotsContainer);
        }

        popupSystem.InitializeTriggersInChildren(slotsContainer, (trigger, view) => {
            var slotName = trigger.gameObject.name;
            view.SetContent(builder => {
                builder
                    .AddText(slotName, PopupElementType.Header)
                    .AddSeparator(PopupElementType.Separator)
                    .AddText("This is a dynamically created slot.", PopupElementType.Text);
            });
        });
    }

    void Update() {
        popupSystem?.Tick(Time.deltaTime);
    }

    void OnDestroy() {
        popupSystem?.Dispose();
    }
}
