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

namespace EventsAppQuest.Web.Producer.Presentation
{
    /// <summary>
    /// Lista de requisitos operacionais por pedido/tipo.
    /// </summary>
    public class ProducerRequirementListView : 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 req in service.GetRequirements())
            {
                var go = new GameObject("ReqRow");
                go.transform.SetParent(_content, false);
                var rt = go.AddComponent<RectTransform>();
                rt.sizeDelta = new Vector2(0, 24);
                var text = go.AddComponent<Text>();
                text.text = $"[{req.OrderId}] {req.Type}: {req.Title} — {(req.IsCompleted ? "Concluído" : "Pendente")}";
                text.font = font;
                text.fontSize = 11;
            }
        }

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