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

/// <summary>
/// Example: Setting up PopupSystem with auto-scanned triggers.
///
/// 1. Attach this to a GameObject in your scene.
/// 2. Assign the PopupSettings asset and PopupReference prefab.
/// 3. Set canvasRoot to the root Canvas that contains your UI and PopupTrigger components.
/// 4. Place PopupTrigger components on any UI elements that should show popups on hover.
/// 5. The system auto-scans triggers on creation. Use GetTriggerHandler() to set content.
///
/// Element prefabs are configured in PopupSettings via PopupElementType.
/// Add entries mapping types (header, text, label_value_text, separator, image) to prefabs.
/// </summary>
public class PopupSystemExample : MonoBehaviour {
    [SerializeField] PopupSettings popupSettings;
    [SerializeField] PopupReference popupReferencePrefab;
    [SerializeField] Transform canvasRoot;

    IPopupSystem popupSystem;

    void Start() {
        popupSystem = new PopupSystem(popupSettings, popupReferencePrefab, canvasRoot);

        popupSystem.RegisterCategory(PopupCategory.Character, priority: 10);
        popupSystem.RegisterCategory(PopupCategory.Item, priority: 5);
        popupSystem.RegisterCategory(PopupCategory.General, priority: 1);

        // Set content on an auto-scanned trigger by GameObject name.
        var characterHandler = popupSystem.GetTriggerHandler("CharacterPortrait");
        characterHandler?.SetContent(builder => {
            builder
                .AddText("Kael", PopupElementType.Header)
                .AddText("Human Warrior", "CCCCCC", PopupElementType.Text)
                .AddSeparator(PopupElementType.Separator)
                .AddNameValue("Health", 55, PopupElementType.LabelValueText)
                .AddNameValue("Mana", 42, PopupElementType.LabelValueText)
                .AddNameValue("Level", 1, PopupElementType.LabelValueText)
                .AddSeparator(PopupElementType.Separator)
                .AddNameValue("Might", 8, PopupElementType.LabelValueText)
                .AddNameValue("Reflex", 2, PopupElementType.LabelValueText)
                .AddNameValue("Knowledge", 5, PopupElementType.LabelValueText)
                .AddNameValue("Perception", 1, PopupElementType.LabelValueText);
        });

        // Set content on all triggers of a category.
        foreach(var handler in popupSystem.GetTriggerHandlers(PopupCategory.Item)) {
            handler.SetContent(builder => {
                builder
                    .AddText("Health Potion", PopupElementType.Header)
                    .AddSeparator(PopupElementType.Separator)
                    .AddText("Restores a moderate amount of health.", PopupElementType.Text)
                    .AddNameValue("Heal Amount", 50, PopupElementType.LabelValueText);
            });
        }
    }

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

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