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

namespace UVC.Twin.MonitoringUI
{
    public class ProcessCardsRowController
    {
        private readonly VisualElement _root;
        private readonly ScrollView _scrollView;
        private readonly List<EquipmentCardController> _cards = new();
        private bool _isValid;

        public event Action<string> OnProcessCardClicked;

        public ProcessCardsRowController(VisualElement root)
        {
            _root = root;
            _scrollView = root.Q<ScrollView>(ElementIds.ProcessCardsScrollView);

            if (_scrollView == null)
            {
                Debug.LogError("[MonitoringUI] ProcessCardsRow: ScrollView not found");
                return;
            }

            _isValid = true;
        }

        public void SetCards(IReadOnlyList<(string TabName, EquipmentCardData Data)> processCards)
        {
            if (!_isValid) return;

            _scrollView.Clear();
            _cards.Clear();

            foreach (var (tabName, data) in processCards)
            {
                var card = new EquipmentCardController(data);

                var capturedTab = tabName;
                card.OnClicked += _ => OnProcessCardClicked?.Invoke(capturedTab);

                _scrollView.Add(card.Root);
                _cards.Add(card);
            }
        }

        public void Show() => _root.style.display = DisplayStyle.Flex;
        public void Hide() => _root.style.display = DisplayStyle.None;
    }
}
