using UnityEngine;
using UnityEngine.UI;
using EventsAppQuest.Web.Producer.Application;

namespace EventsAppQuest.Web.Producer.Presentation
{
    /// <summary>
    /// Lista de observações/notas de execução.
    /// </summary>
    public class ProducerExecutionNotesView : MonoBehaviour, IProducerContentRoot
    {
        private Transform _content;

        public void SetContentRoot(Transform contentRoot) => _content = contentRoot;

        private void Start() => Refresh();

        public void Refresh()
        {
            if (_content == null) return;
            ClearContent(_content);
            var service = ProducerRuntimeContext.DataService;
            if (service == null) return;
            var font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
            foreach (var note in service.GetExecutionNotes())
            {
                var go = new GameObject("NoteRow");
                go.transform.SetParent(_content, false);
                var rt = go.AddComponent<RectTransform>();
                rt.sizeDelta = new Vector2(0, 36);
                var text = go.AddComponent<Text>();
                text.text = $"[{note.OrderId}] {note.AuthorLabel}: {note.Content}\n  {note.CreatedAt:g}";
                text.font = font;
                text.fontSize = 10;
            }
        }

        private static void ClearContent(Transform content)
        {
            for (var i = content.childCount - 1; i >= 0; i--)
                Destroy(content.GetChild(i).gameObject);
        }
    }
}
