using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using BPASmartClient.Helper;
using System.IO;
using Newtonsoft.Json;
using BPASmartClient.Model;
using System.Reflection;
using Microsoft.Toolkit.Mvvm.Input;
using System.Windows;
using BPASmartClient.Business;

namespace BPASmartClient.ViewModel
{
    public class DeviceMonitorViewModel : ObservableObject
    {
        public DeviceMonitorViewModel()
        {
            DeviceWindowSwitch = new RelayCommand<object>((o) =>
              {
                  if (o != null && o is string nameSpace)
                  {
                      foreach (var item in Devices)
                      {
                          var fe = item.DeviceMonitors.FirstOrDefault(p => p.Namespace == nameSpace);
                          if (fe != null)
                          {
                              MainContent = fe.fe;
                              break;
                          }
                      }
                  }
              });

            GetData();
        }

        private void GetData()
        {
            var result = Plugin.GetInstance().GetPlugin<ConfigMgr>()?.deviceConfigModelJsons;
            if (result != null)
            {
                foreach (var shop in result)//店铺集合
                {
                    foreach (var device in shop.deviceModels)//设备集合
                    {
                        Devices.Add(new ControlDevice() { Name = device.DeviceName, Namespace = device.DeviceNamespace });
                    }
                }
            }

            if (Devices.Count > 0)
            {
                Devices.ElementAt(0).IsChecked = true;
                for (int i = 0; i < Devices.Count; i++)
                {
                    Devices.ElementAt(i).DeviceMonitors.Clear();
                    Assembly.Load(Devices.ElementAt(i).Namespace.Substring(0, Devices.ElementAt(i).Namespace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) =>
                    {
                        if (type?.BaseType?.Name == "UserControl")
                        {
                            var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement;
                            Devices.ElementAt(i).DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe });
                        }
                    });

                }

                if (Devices.ElementAt(0).DeviceMonitors.Count > 0)
                {
                    Devices.ElementAt(0).DeviceMonitors.ElementAt(0).IsChecked = true;
                    if (Devices.ElementAt(0).DeviceMonitors.ElementAt(0).fe != null) MainContent = Devices.ElementAt(0).DeviceMonitors.ElementAt(0).fe;
                }
            }
        }

        public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
        private FrameworkElement _mMainContent;

        public RelayCommand<object> DeviceSwitch { get; set; }

        public RelayCommand<object> DeviceWindowSwitch { get; set; }

        public ObservableCollection<ControlDevice> Devices { get; set; } = new ObservableCollection<ControlDevice>();


    }

    public class ControlDevice : ObservableObject
    {
        public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
        private string _mName;


        public string Namespace { get { return _mNamespace; } set { _mNamespace = value; OnPropertyChanged(); } }
        private string _mNamespace;


        public bool IsChecked { get { return _mIsChecked; } set { _mIsChecked = value; OnPropertyChanged(); } }
        private bool _mIsChecked;

        public ObservableCollection<Device> DeviceMonitors { get; set; } = new ObservableCollection<Device>();
    }

    public class Device : ObservableObject
    {

        public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
        private string _mName;


        public string Namespace { get { return _mNamespace; } set { _mNamespace = value; OnPropertyChanged(); } }
        private string _mNamespace;


        public bool IsChecked { get { return _mIsChecked; } set { _mIsChecked = value; OnPropertyChanged(); } }
        private bool _mIsChecked;


        public FrameworkElement fe { get; set; }

    }






}
