using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using EventsAppQuest.Application.Services;
using EventsAppQuest.Core;
using EventsAppQuest.Domain.Models;

namespace EventsAppQuest.Presentation
{
    /// <summary>
    /// Exibe avaliação de cobertura operacional por fornecedores (score, lacunas, áreas cobertas, avisos e recomendações).
    /// </summary>
    public class SupplierCoverageView : MonoBehaviour
    {
        private const string Title = "Cobertura de Fornecedores / Lacunas Operacionais";
        private const string NenhumProjeto = "Nenhum projeto ou proposta ativa.";
        private const string ScoreCobertura = "Score de cobertura (0–100): ";
        private const string Lacunas = "Lacunas / capacidades ausentes";
        private const string AreasFracas = "Áreas com cobertura fraca";
        private const string ZonasCobertas = "Zonas/tipos cobertos";
        private const string Avisos = "Avisos de cobertura";
        private const string Recomendacoes = "Recomendações";

        private AppSession _session;
        private ISupplierCoverageEvaluationService _coverageService;
        private Transform _contentRoot;
        private Font _font;

        private void Start()
        {
            _session = ServiceRegistry.Instance.Get<AppSession>();
            _coverageService = ServiceRegistry.Instance.Get<ISupplierCoverageEvaluationService>();
            if (_session != null)
                _session.OnSessionChanged += UpdateContent;

            _font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
            BuildUI();
            UpdateContent();
        }

        private void OnDestroy()
        {
            if (_session != null)
                _session.OnSessionChanged -= UpdateContent;
        }

        private void BuildUI()
        {
            var layout = gameObject.GetComponent<VerticalLayoutGroup>();
            if (layout == null)
            {
                layout = gameObject.AddComponent<VerticalLayoutGroup>();
                layout.spacing = 6;
                layout.padding = new RectOffset(12, 12, 12, 12);
                layout.childForceExpandHeight = false;
                layout.childForceExpandWidth = true;
            }

            var titleGo = new GameObject("Title");
            titleGo.transform.SetParent(transform, false);
            titleGo.AddComponent<RectTransform>().sizeDelta = new Vector2(0, 22);
            var titleText = titleGo.AddComponent<Text>();
            titleText.text = Title;
            titleText.font = _font;
            titleText.fontSize = 16;
            titleText.fontStyle = FontStyle.Bold;

            _contentRoot = new GameObject("ContentRoot").transform;
            _contentRoot.SetParent(transform, false);
            _contentRoot.gameObject.AddComponent<LayoutElement>().preferredHeight = 200;
            var vlg = _contentRoot.gameObject.AddComponent<VerticalLayoutGroup>();
            vlg.spacing = 4;
            vlg.childForceExpandHeight = false;
            vlg.childForceExpandWidth = true;
        }

        private void UpdateContent()
        {
            ClearChildren(_contentRoot);

            var project = _session?.CurrentProject;
            var active = project?.GetActiveProposal();
            if (project == null || active == null)
            {
                AppendText(_contentRoot, NenhumProjeto);
                return;
            }

            var coverage = _coverageService?.Evaluate(project, active);
            if (coverage == null)
            {
                AppendText(_contentRoot, "Avaliação de cobertura não disponível.");
                return;
            }

            AppendText(_contentRoot, ScoreCobertura + coverage.CoverageScore);
            AppendList(_contentRoot, Lacunas, coverage.MissingSupplierCapabilities, new Color(0.55f, 0.2f, 0.2f, 1f));
            AppendList(_contentRoot, AreasFracas, coverage.WeakCoverageAreas, new Color(0.5f, 0.35f, 0.15f, 1f));
            AppendList(_contentRoot, ZonasCobertas, coverage.CoveredZones, new Color(0.2f, 0.45f, 0.3f, 1f));
            AppendList(_contentRoot, Avisos, coverage.CoverageWarnings, new Color(0.6f, 0.4f, 0.1f, 1f));
            AppendList(_contentRoot, Recomendacoes, coverage.Recommendations, new Color(0.2f, 0.4f, 0.5f, 1f));
        }

        private void AppendText(Transform parent, string text)
        {
            var go = new GameObject("TextLine");
            go.transform.SetParent(parent, false);
            go.AddComponent<RectTransform>().sizeDelta = new Vector2(0, 16);
            var t = go.AddComponent<Text>();
            t.text = text;
            t.font = _font;
            t.fontSize = 12;
        }

        private void AppendList(Transform parent, string title, List<string> items, Color color)
        {
            if (items == null || items.Count == 0) return;
            var labelGo = new GameObject("Label");
            labelGo.transform.SetParent(parent, false);
            labelGo.AddComponent<RectTransform>().sizeDelta = new Vector2(0, 14);
            var label = labelGo.AddComponent<Text>();
            label.text = title + ":";
            label.font = _font;
            label.fontSize = 11;
            label.fontStyle = FontStyle.Bold;
            foreach (var item in items)
            {
                var line = new GameObject("Item");
                line.transform.SetParent(parent, false);
                line.AddComponent<RectTransform>().sizeDelta = new Vector2(0, 14);
                var t = line.AddComponent<Text>();
                t.text = "• " + item;
                t.font = _font;
                t.fontSize = 11;
                t.color = color;
            }
        }

        private static void ClearChildren(Transform root)
        {
            if (root == null) return;
            for (var i = root.childCount - 1; i >= 0; i--)
                Object.Destroy(root.GetChild(i).gameObject);
        }
    }
}
