using UnityEngine;
using UnityEngine.UI;
using EventsAppQuest.Core;

namespace EventsAppQuest.Presentation
{
    /// <summary>
    /// Exibe o hotspot atual: título e descrição do ponto de destaque.
    /// </summary>
    public class QuestHotspotView : MonoBehaviour
    {
        private Text _titleText;
        private Text _descriptionText;

        public void Build(Transform parent, Font font)
        {
            var go = new GameObject("QuestHotspotView");
            go.transform.SetParent(parent, false);
            go.AddComponent<LayoutElement>().preferredHeight = 100;
            var vlg = go.AddComponent<VerticalLayoutGroup>();
            vlg.spacing = 6;
            vlg.padding = new RectOffset(20, 12, 20, 12);
            vlg.childForceExpandHeight = false;

            _titleText = AddLabel(go.transform, "Hotspot", 16, font, true);
            _descriptionText = AddLabel(go.transform, "", 14, font, false);
        }

        public void Refresh(QuestPresentationSession session)
        {
            var hotspot = session?.CurrentHotspot;
            if (hotspot != null)
            {
                _titleText.text = string.IsNullOrEmpty(hotspot.Title) ? "Destaque" : hotspot.Title;
                _descriptionText.text = hotspot.Description ?? "";
                _descriptionText.gameObject.SetActive(!string.IsNullOrEmpty(hotspot.Description));
            }
            else
            {
                _titleText.text = "Nenhum hotspot em foco";
                _descriptionText.text = "";
                _descriptionText.gameObject.SetActive(false);
            }
        }

        private static Text AddLabel(Transform parent, string text, int fontSize, Font font, bool bold)
        {
            var go = new GameObject("Label");
            go.transform.SetParent(parent, false);
            go.AddComponent<RectTransform>().sizeDelta = new Vector2(0, fontSize + 6);
            var t = go.AddComponent<Text>();
            t.text = text;
            t.font = font;
            t.fontSize = fontSize;
            t.fontStyle = bold ? FontStyle.Bold : FontStyle.Normal;
            return t;
        }
    }
}
