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;

namespace BPASmartClient.ViewModel
{
    public class DeviceMonitorViewModel : ObservableObject
    {
        public DeviceMonitorViewModel()
        {
            DeviceSwitch = new RelayCommand<object>((o) =>
            {
                if (o is string nameSpace)
                {
                    DeviceMonitors.Clear();
                    Assembly.Load(nameSpace.Substring(0, nameSpace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) =>
                     {
                         if (type?.BaseType?.Name == "UserControl")
                         {
                             var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement;
                             DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe });
                         }
                     });
                }
            });

            DeviceWindowSwitch = new RelayCommand<object>((o) =>
              {
                  if (o != null && o is string nameSpace)
                  {
                      var fe = DeviceMonitors.FirstOrDefault(p => p.Namespace == nameSpace);
                      if (fe != null) MainContent = fe.fe;
                  }
              });

            GetData();
        }



        private void GetData()
        {
            var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
            string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json";
            if (File.Exists(path))
            {
                string JsonString = File.ReadAllText(path);
                var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
                if (result != null)
                {
                    foreach (var shop in result)//店铺集合
                    {
                        foreach (var device in shop.deviceModels)//设备集合
                        {
                            Devices.Add(new Device() { Name = device.DeviceName, Namespace = device.DeviceNamespace });
                        }
                    }
                }
            }
        }

        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<Device> Devices { get; set; } = new ObservableCollection<Device>();

        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 FrameworkElement fe { get; set; }
    }






}
