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

namespace EventsAppQuest.Presentation
{
    /// <summary>
    /// Exibe o estado de cena atual: nome, descrição e mood visual.
    /// </summary>
    public class QuestSceneStateView : MonoBehaviour
    {
        private Text _nameText;
        private Text _descriptionText;
        private Text _moodText;

        public void Build(Transform parent, Font font)
        {
            var go = new GameObject("QuestSceneStateView");
            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;

            _nameText = AddLabel(go.transform, "Estado da cena", 16, font, true);
            _descriptionText = AddLabel(go.transform, "", 14, font, false);
            _moodText = AddLabel(go.transform, "", 14, font, false);
        }

        public void Refresh(QuestPresentationSession session)
        {
            var state = session?.CurrentSceneState;
            if (state != null)
            {
                _nameText.text = string.IsNullOrEmpty(state.Name) ? "Estado " + (session.CurrentSceneStateIndex + 1) : state.Name;
                _descriptionText.text = state.Description ?? "";
                _descriptionText.gameObject.SetActive(!string.IsNullOrEmpty(state.Description));
                _moodText.text = string.IsNullOrEmpty(state.VisualMood) ? "" : "Mood: " + state.VisualMood;
                _moodText.gameObject.SetActive(!string.IsNullOrEmpty(state.VisualMood));
            }
            else
            {
                _nameText.text = "Nenhum estado de cena";
                _descriptionText.text = "";
                _descriptionText.gameObject.SetActive(false);
                _moodText.text = "";
                _moodText.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;
        }
    }
}
