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

namespace UVC.Twin.MonitoringUI
{
    public class TopNavBarController
    {
        private readonly VisualElement _root;
        private readonly VisualElement _tabContainer;
        private readonly Button _hamburgerButton;
        private readonly List<Button> _bayTabs = new();
        private Button _activeTab;
        private bool _isValid;

        public event Action OnHamburgerClicked;
        public event Action<string> OnBayTabSelected;
        public event Action OnSettingsClicked;
        public event Action OnVisibilityClicked;
        public event Action OnUserClicked;
        public event Action OnExportClicked;

        public TopNavBarController(VisualElement root)
        {
            _root = root;

            _tabContainer = _root.Q<VisualElement>(ElementIds.BayTabContainer);
            _hamburgerButton = _root.Q<Button>(ElementIds.BtnHamburger);

            if (_tabContainer == null || _hamburgerButton == null)
            {
                Debug.LogError($"[MonitoringUI] TopNavBar: required elements not found " +
                    $"(tabContainer={_tabContainer != null}, hamburger={_hamburgerButton != null})");
                return;
            }

            _isValid = true;
            Bind();
        }

        private void Bind()
        {
            _hamburgerButton.clicked += () => OnHamburgerClicked?.Invoke();

            BindButton(ElementIds.BtnSettings, () => OnSettingsClicked?.Invoke());
            BindButton(ElementIds.BtnVisibility, () => OnVisibilityClicked?.Invoke());
            BindButton(ElementIds.BtnUser, () => OnUserClicked?.Invoke());
            BindButton(ElementIds.BtnExport, () => OnExportClicked?.Invoke());
        }

        private void BindButton(string elementId, Action handler)
        {
            var btn = _root.Q<Button>(elementId);
            if (btn != null)
                btn.clicked += handler;
            else
                Debug.LogWarning($"[MonitoringUI] TopNavBar: optional element '{elementId}' not found");
        }

        public void SetTabs(IReadOnlyList<NamedItemData> tabs, string activeTabId = null)
        {
            if (!_isValid) return;

            _tabContainer.Clear();
            _bayTabs.Clear();
            _activeTab = null;

            foreach (var tabData in tabs)
            {
                var tab = new Button { text = tabData.DisplayName };
                tab.AddToClassList(CssClasses.BayTab);
                tab.userData = tabData.Id;

                var capturedId = tabData.Id;
                tab.clicked += () => SelectTab(capturedId);

                _tabContainer.Add(tab);
                _bayTabs.Add(tab);

                if (tabData.Id == activeTabId || (activeTabId == null && _activeTab == null))
                {
                    SetActiveTab(tab);
                }
            }
        }

        public void SelectTab(string bayId)
        {
            foreach (var tab in _bayTabs)
            {
                if ((string)tab.userData == bayId)
                {
                    if (tab == _activeTab) return;
                    SetActiveTab(tab);
                    OnBayTabSelected?.Invoke(bayId);
                    return;
                }
            }
        }

        private void SetActiveTab(Button tab)
        {
            _activeTab?.RemoveFromClassList(CssClasses.BayTabActive);
            tab.AddToClassList(CssClasses.BayTabActive);
            _activeTab = tab;
        }
    }
}
