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

namespace EventsAppQuest.Web.Producer.Presentation
{
    /// <summary>
    /// Painel para aceitar, rejeitar ou marcar pedido como precisa esclarecimento (mock).
    /// </summary>
    public class ProducerAcceptancePanel : 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");

            var title = new GameObject("Title");
            title.transform.SetParent(_content, false);
            var titleRt = title.AddComponent<RectTransform>();
            titleRt.sizeDelta = new Vector2(0, 22);
            var titleText = title.AddComponent<Text>();
            titleText.text = "Ações de aceite / revisão";
            titleText.font = font;
            titleText.fontSize = 12;
            titleText.fontStyle = FontStyle.Bold;

            foreach (var order in service.GetOrders())
            {
                var orderId = order.OrderId;
                var row = new GameObject("OrderActions_" + orderId);
                row.transform.SetParent(_content, false);
                var rowRt = row.AddComponent<RectTransform>();
                rowRt.sizeDelta = new Vector2(0, 28);

                var label = new GameObject("Label");
                label.transform.SetParent(row.transform, false);
                var labelRt = label.AddComponent<RectTransform>();
                labelRt.anchorMin = new Vector2(0, 0);
                labelRt.anchorMax = new Vector2(1, 1);
                labelRt.offsetMin = new Vector2(0, 0);
                labelRt.offsetMax = new Vector2(-320, 0);
                var labelText = label.AddComponent<Text>();
                labelText.text = $"{order.OrderId}: {order.AcceptanceStatus}";
                labelText.font = font;
                labelText.fontSize = 11;

                AddButton(row.transform, "Aceitar", orderId, ProducerAcceptanceStatus.Accepted, font, new Color(0.3f, 0.7f, 0.4f), service, false, 0);
                AddButton(row.transform, "Rejeitar", orderId, ProducerAcceptanceStatus.Rejected, font, new Color(0.8f, 0.35f, 0.3f), service, false, 76);
                AddButton(row.transform, "Esclarecer", orderId, ProducerAcceptanceStatus.NeedsClarification, font, new Color(0.9f, 0.75f, 0.2f), service, true, 152);
            }
        }

        private void AddButton(Transform parent, string label, string orderId, ProducerAcceptanceStatus status, Font font, Color color, IMockProducerDataService service, bool needsClarification, float xOffsetFromRight)
        {
            var btn = new GameObject("Btn_" + label + "_" + orderId);
            btn.transform.SetParent(parent, false);
            var rt = btn.AddComponent<RectTransform>();
            rt.anchorMin = new Vector2(1, 0.5f);
            rt.anchorMax = new Vector2(1, 0.5f);
            rt.pivot = new Vector2(1, 0.5f);
            rt.sizeDelta = new Vector2(72, 22);
            rt.anchoredPosition = new Vector2(-xOffsetFromRight, 0);
            var img = btn.AddComponent<Image>();
            img.color = color;
            var b = btn.AddComponent<Button>();
            var tGo = new GameObject("Text");
            tGo.transform.SetParent(btn.transform, false);
            var t = tGo.AddComponent<Text>();
            t.text = label;
            t.font = font;
            t.fontSize = 10;
            t.alignment = TextAnchor.MiddleCenter;
            var tRt = tGo.GetComponent<RectTransform>();
            tRt.anchorMin = Vector2.zero;
            tRt.anchorMax = Vector2.one;
            tRt.offsetMin = Vector2.zero;
            tRt.offsetMax = Vector2.zero;
            b.targetGraphic = img;
            b.onClick.AddListener(() =>
            {
                service.UpdateOrderAcceptance(orderId, status, null, needsClarification, needsClarification ? "Pedido de esclarecimento pelo produtor" : null);
                Refresh();
            });
        }

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