using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Toolkit.Mvvm.Messaging;
using System.Diagnostics;
using HBLConsole.Model;
using HBLConsole.Service;
using System.Reflection;
using HBLConsole.Interface;
using HBLConsole.ViewModel;

namespace HBLConsole.DialogWindow.ViewModel
{
    public class DeviceManagermentSetViewModel : ObservableObject
    {
        public DeviceManagermentSetViewModel()
        {
            Init();
            CancelCommand = new RelayCommand(() =>
            {
                ActionManage.GetInstance.Send("Cancel", devcieManagerResult);
            });
            ConfirmCommand = new RelayCommand(() =>
            {
                var obj = DeviceManageViewModel.communicationSets.FirstOrDefault(P => P.DeviceName.ToUpper() == devcieManagerResult.DeviceName.ToUpper());
                if (obj == null)
                {
                    CommunicationSet communicationObj = new CommunicationSet();
                    EDeviceType eDeviceType = (EDeviceType)Enum.Parse(typeof(EDeviceType), devcieManagerResult.DeviceType);
                    foreach (var item in Enum.GetNames(typeof(EDeviceTypeSort)))
                    {
                        var res = DeviceTypeSort.GetInstance.DevceTypes[item].FirstOrDefault(p => p == devcieManagerResult.DeviceType);
                        if (res != null)
                        {
                            string device = string.Empty;
                            switch ((EDeviceTypeSort)Enum.Parse(typeof(EDeviceTypeSort), item))
                            {
                                case EDeviceTypeSort.Siemens:
                                    device = EDeviceType.Siemens.ToString();
                                    break;
                                case EDeviceTypeSort.TCP:
                                    device = EDeviceType.ModbusTcp.ToString();
                                    break;
                                case EDeviceTypeSort.Serial:
                                    device = EDeviceType.ModbusRtu.ToString();
                                    break;
                                default:
                                    break;
                            }

                            string NameSpace = "HBLConsole.Model";//Load 加载的是dll的名称，GetType获取的是全命名空间下的类
                            var type = Assembly.Load(NameSpace)?.GetType($"{NameSpace}.{device}");
                            if (type != null) communicationObj.Device = Activator.CreateInstance(type) as IDeviceType;
                            communicationObj.DeviceName = $"{devcieManagerResult.DeviceName}【{eDeviceType}】";
                            communicationObj.deviceType = eDeviceType;
                            DeviceManageViewModel.communicationSets.Add(communicationObj);
                            break;
                        }
                    }
                }
                else
                {
                    LogInfo = $"警告：设备【{devcieManagerResult.DeviceName}】已存在，请重试";
                }
                ActionManage.GetInstance.Send("Confirm", devcieManagerResult);
            });
        }

        public ObservableCollection<string> DeviceType { get; set; } = new ObservableCollection<string>();

        public RelayCommand CancelCommand { get; set; }
        public RelayCommand ConfirmCommand { get; set; }

        private void Init()
        {
            DeviceType.Clear();
            foreach (var item in Enum.GetNames(typeof(EDeviceType))) { DeviceType.Add(item); }
        }

        public DeviceManagermentResult devcieManagerResult { get { return _mdevcieManagerResult; } set { _mdevcieManagerResult = value; OnPropertyChanged(); } }
        private DeviceManagermentResult _mdevcieManagerResult = new DeviceManagermentResult();


        public string LogInfo { get { return _mLogInfo; } set { _mLogInfo = value; OnPropertyChanged(); } }
        private string _mLogInfo;




    }
}
