using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using SmartAquaViewer.Classes;
using SmartAquaViewer.DataAnalysis;
using SmartAquaViewer.Model;
using SmartAquaViewer.ViewModel;

namespace SmartAquaViewer.View
{
    /// <summary>
    /// MonitoringView.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MonitoringView : UserControl
    {
        private MonitoringViewModel? monitoringViewModel;

        private readonly Dictionary<MonitorTab, UIElement> _tabMap;

        public MonitoringView()
        {
            InitializeComponent();

            _tabMap = new Dictionary<MonitorTab, UIElement>
            {
                { MonitorTab.Tank,  dgTanks },
                { MonitorTab.Filter,   dgFilter },
                { MonitorTab.Sterilizer, dgSterilizer }
            };

            Loaded += MonitoringView_Loaded;            
        }

        private void MonitoringView_Loaded(object sender, RoutedEventArgs e)
        {
            monitoringViewModel = this.DataContext as MonitoringViewModel;
            monitoringViewModel.PropertyChanged += VmOnPropertyChanged;
            SetActiveTab(monitoringViewModel.SelectedTab); // 초기 반영
        }

        private void VmOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(MonitoringViewModel.SelectedTab) && sender is MonitoringViewModel vm)
                SetActiveTab(vm.SelectedTab);
        }

        private void SetActiveTab(MonitorTab tab)
        {
            // 전부 Collapsed
            dgTanks.Visibility = Visibility.Collapsed;
            dgFilter.Visibility = Visibility.Collapsed;
            dgSterilizer.Visibility = Visibility.Collapsed;

            // 대상만 Visible
            if (_tabMap.TryGetValue(tab, out var target))
                target.Visibility = Visibility.Visible;
        }
    }
}
