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

namespace EventsAppQuest.Web.Producer.Presentation
{
    /// <summary>
    /// Lista de pedidos recebidos pelo produtor.
    /// </summary>
    public class ProducerOrderListView : 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 order in service.GetOrders())
            {
                var go = new GameObject("OrderRow");
                go.transform.SetParent(_content, false);
                var rt = go.AddComponent<RectTransform>();
                rt.sizeDelta = new Vector2(0, 32);
                var text = go.AddComponent<Text>();
                text.text = $"[{order.OrderId}] {order.EventCategory ?? "—"} — {order.ThemeLabel ?? "—"} | {HandoffIndicatorHelper.GetIndicatorLabel(order)} | Prioridade: {order.FollowUpPriority}";
                text.font = font;
                text.fontSize = 12;
            }
        }

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