using HBLConsole.DialogWindow.View;
using HBLConsole.Interface;
using HBLConsole.Model;
using HBLConsole.Service;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using BPA.Message.Enum;

namespace HBLConsole.ViewModel
{
    public class DeviceManageViewModel : ViewModelBase
    {
        public DeviceManageViewModel()
        {
            WindowName = "设备管理";

            foreach (var item in Enum.GetNames(typeof(DeviceClientType)))
            {
                ClientDevices.Add(item);
            }

            NewConnectCommand = new RelayCommand(() => { NewConnect(); });


            RemoveCommand = new RelayCommand<object>((o) => { RemoveDevice(o); });

            SaveConnectSetCommand = new RelayCommand(() =>
            {
                TextHelper.GetInstance.WriteTextInfo(ClientDeviceType, "DeviceType");
                LocaPath.GetInstance.FilePath = $"AccessFile\\{ClientDeviceType}\\";
                Json<CommunicationPar>.Data.communicationSets.Clear();
                foreach (var item in communicationSets) { Json<CommunicationPar>.Data.communicationSets.Add(item); }
                Json<CommunicationPar>.Save();
                LocaPath.GetInstance.FilePath = $"AccessFile\\{GVL.GeneralConfig.DeviceType.ToString()}\\";
            });

            ClientDeviceType = GVL.GeneralConfig.DeviceType.ToString();
        }

        public string ClientDeviceType
        {
            get { return _mClientDeviceType; }
            set
            {
                _mClientDeviceType = value;
                LocaPath.GetInstance.FilePath = $"AccessFile\\{value}\\";
                communicationSets.Clear();
                Json<CommunicationPar>.Read();
                foreach (var item in Json<CommunicationPar>.Data.communicationSets) { communicationSets.Add(item); }
                LocaPath.GetInstance.FilePath = $"AccessFile\\{GVL.GeneralConfig.DeviceType.ToString()}\\";
                OnPropertyChanged();
            }
        }
        private string _mClientDeviceType = string.Empty;

        public static ObservableCollection<CommunicationSet> communicationSets { get; set; } = new ObservableCollection<CommunicationSet>();

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

        public RelayCommand NewConnectCommand { get; set; }

        public RelayCommand SaveConnectSetCommand { get; set; }

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

        /// <summary>
        /// 创建新连接
        /// </summary>
        private void NewConnect()
        {
            DeviceManagermentSetView deviceManagermentSetView = new DeviceManagermentSetView();
            deviceManagermentSetView.Show();
        }

        private void RemoveDevice(object o)
        {
            if (o == null) return;
            int index = Array.FindIndex(communicationSets.ToArray(), p => p.DeviceName == o.ToString());
            if (index >= 0 && index < communicationSets.Count) communicationSets.RemoveAt(index);
        }



    }
}
