using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HBLConsole.Model;
using HBLConsole.Service;
using HBLConsole.ViewModel;
using Microsoft.Toolkit.Mvvm.Input;

namespace HBLConsole.DialogWindow.ViewModel
{
    public class SimOrderConfitViewModel : ViewModelBase
    {
        bool IsEdit = false;
        SimOrderVisibleData simOrderVisibleData;
        string DeviceType = string.Empty;
        public SimOrderConfitViewModel()
        {
            DeviceType = GVL.GeneralConfig.DeviceType.ToString();
            ActionManage.GetInstance.Register(new Action<object>((o) =>
            {
                if (o != null)
                {
                    if (o is SimOrderVisibleData sim)
                    {
                        Name = sim.Text;
                        Max = sim.MaxValue;
                        Min = sim.MinValue;
                        IsEdit = true;
                        simOrderVisibleData = sim;
                    }
                }
            }), "SendSimData");

            ConfirmCommand = new RelayCommand(() =>
            {
                //if (!Json<KeepDataBase>.Data.simOrderConfig.ContainsKey(DeviceType))
                //{
                //    Json<KeepDataBase>.Data.simOrderConfig.TryAdd(DeviceType, new ObservableCollection<SimOrderVisibleData>());
                //}

                if (!IsEdit)
                {
                    Json<KeepDataBase>.Data.simOrderConfig.Add(new SimOrderVisibleData()
                    {
                        IsEnable = true,
                        IsSelected = true,
                        Text = Name,
                        Loc = Min,
                        MaxValue = Max,
                        MinValue = Min,
                    });
                }
                else
                {
                    int index = Array.FindIndex(Json<KeepDataBase>.Data.simOrderConfig.ToArray(), p => p.Text == simOrderVisibleData.Text);
                    if (index >= 0 && index < Json<KeepDataBase>.Data.simOrderConfig.Count())
                    {
                        Json<KeepDataBase>.Data.simOrderConfig.ElementAt(index).Text = Name;
                        Json<KeepDataBase>.Data.simOrderConfig.ElementAt(index).MinValue = Min;
                        Json<KeepDataBase>.Data.simOrderConfig.ElementAt(index).MaxValue = Max;
                        Json<KeepDataBase>.Data.simOrderConfig.ElementAt(index).IsEnable = simOrderVisibleData.IsEnable;
                        Json<KeepDataBase>.Data.simOrderConfig.ElementAt(index).IsSelected = simOrderVisibleData.IsSelected;
                    }
                }
                ActionManage.GetInstance.Send("SimOrderConfitViewModelExit");
            });
            CancelCommand = new RelayCommand(() => { ActionManage.GetInstance.Send("SimOrderConfitViewModelExit"); });
        }


        public RelayCommand ConfirmCommand { get; set; }

        public RelayCommand CancelCommand { get; set; }

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

        public ushort Min { get { return _mMin; } set { _mMin = value; OnPropertyChanged(); } }
        private ushort _mMin;

        public ushort Max { get { return _mMax; } set { _mMax = value; OnPropertyChanged(); } }
        private ushort _mMax;


    }
}
