using System;
using System.Collections.Generic;
using UnityEngine.UIElements;

namespace UVC.Twin.MonitoringUI
{
    public class EquipmentCardController
    {
        public VisualElement Root { get; }

        public event Action<string> OnClicked;
        public event Action<string> OnInfoClicked;

        private readonly string _cardId;
        private readonly List<BarChartElement> _barCharts = new();
        private readonly VisualElement _chartsContainer;
        private readonly Label _currentWoLabel;
        private readonly Label _nextWoLabel;

        public EquipmentCardController(EquipmentCardData data)
        {
            _cardId = data.Id;

            Root = new VisualElement();
            Root.AddToClassList(CssClasses.EquipmentCard);
            Root.userData = data.Id;

            if (data.IsSummaryCard)
                Root.AddToClassList(CssClasses.EquipmentCardSummary);

            // Header
            var header = new VisualElement();
            header.AddToClassList(CssClasses.CardHeader);

            var title = new Label { text = data.Title };
            title.AddToClassList(CssClasses.CardTitle);

            var infoBtn = new Button { text = "\u24D8" }; // ⓘ
            infoBtn.AddToClassList("info-circle-button");
            infoBtn.AddToClassList(CssClasses.CardInfoButton);
            infoBtn.clicked += () => OnInfoClicked?.Invoke(_cardId);

            header.Add(title);
            header.Add(infoBtn);
            Root.Add(header);

            // Charts
            _chartsContainer = new VisualElement();
            _chartsContainer.AddToClassList(CssClasses.CardChartsContainer);

            foreach (var chartData in data.Charts)
            {
                var chart = new BarChartElement();
                chart.SetData(chartData);
                _chartsContainer.Add(chart);
                _barCharts.Add(chart);
            }

            Root.Add(_chartsContainer);

            // W/O Info (non-summary cards only)
            if (!data.IsSummaryCard && (!string.IsNullOrEmpty(data.CurrentWorkOrder) || !string.IsNullOrEmpty(data.NextWorkOrder)))
            {
                var infoSection = new VisualElement();
                infoSection.AddToClassList(CssClasses.CardInfoSection);

                _currentWoLabel = new Label { text = $"\ud604\uc7ac W/O: {data.CurrentWorkOrder}" };
                _currentWoLabel.AddToClassList(CssClasses.CardWoLabel);
                infoSection.Add(_currentWoLabel);

                _nextWoLabel = new Label { text = $"\ub2e4\uc74c W/O: {data.NextWorkOrder}" };
                _nextWoLabel.AddToClassList(CssClasses.CardWoLabel);
                infoSection.Add(_nextWoLabel);

                Root.Add(infoSection);
            }

            Root.RegisterCallback<ClickEvent>(evt =>
            {
                evt.StopPropagation();
                OnClicked?.Invoke(_cardId);
            });
        }

        public void SetActive(bool active)
        {
            if (active)
                Root.AddToClassList(CssClasses.EquipmentCardActive);
            else
                Root.RemoveFromClassList(CssClasses.EquipmentCardActive);
        }
    }
}
