using BPASmartClient.Helper;
using BPASmartClient.Model;
using BPASmartClient.MorkM.Model;
using BPASmartClient.MorkM.View;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.MorkM.ViewModel
{
    internal class DebugViewModel : ObservableObject
    {
        public DebugViewModel()
        {


            AddSimDataCommand = new RelayCommand(() =>
            {
                SimOrderConfitView simOrderConfitView = new SimOrderConfitView();
                simOrderConfitView.Show();
            });

            SimOrderCommand = new RelayCommand(() =>
            {
                List<ushort> locs = new List<ushort>();
                foreach (var item in simOrderConfig)
                {
                    if (item.IsSelected)
                        locs.Add((ushort)(new Random().Next(item.MinValue, item.MaxValue + 1)));
                    else
                        locs.Add(item.Loc);
                }
                if (locs.Count == 0)
                {
                    locs.Add((ushort)(new Random().Next(1, 5)));
                    locs.Add(10);
                }

                ActionManage.GetInstance.Send("SimOrder", locs);
                //下发模拟订单
                Trace.WriteLine("下发模拟订单。。。");
            });

            InitCommand = new RelayCommand(() =>
            {
                ActionManage.GetInstance.Send("InitDevice");
                //初始化按钮
                Trace.WriteLine("已点击初始化按钮。。。");
            });

            LoopSimOrderCommand = new RelayCommand(() =>
            {
                if (LoopOrderButtonContent == "循环跑单") ActionManage.GetInstance.Send("EnableForOrder");
                if (LoopOrderButtonContent == "停止跑单") ActionManage.GetInstance.Send("StopForOrder");
                LoopOrderButtonContent = LoopOrderButtonContent == "循环跑单" ? "停止跑单" : "循环跑单";

            });


            EditCommand = new RelayCommand<object>((o) =>
            {
                if (o != null)
                {
                    var res = Json<KeepDataBase>.Data.simOrderConfig.FirstOrDefault(p => p.Text == o.ToString());
                    if (res != null)
                    {
                        SimOrderConfitView simOrderConfitView = new SimOrderConfitView();
                        simOrderConfitView.Show();
                        ActionManage.GetInstance.Send("SendSimData", res);
                    }
                }

            });

            RemoveCommand = new RelayCommand<object>((o) =>
            {
                if (o != null)
                {
                    var res = Json<KeepDataBase>.Data.simOrderConfig.FirstOrDefault(p => p.Text == o.ToString());
                    if (res != null) Json<KeepDataBase>.Data.simOrderConfig.Remove(res);
                }
            });
        }

        public static bool EnableLocalSimOrder { get { return GeneralConfig.EnableLocalSimOrder; } set { GeneralConfig.EnableLocalSimOrder = value; OnStaticPropertyChanged(); } }

        public ObservableCollection<SimOrderVisibleData> simOrderConfig
        {
            get
            {
                return Json<KeepDataBase>.Data.simOrderConfig;
            }
            set
            {
                Json<KeepDataBase>.Data.simOrderConfig = value;
            }
        }

        public RelayCommand AddSimDataCommand { get; set; }

        public RelayCommand SimOrderCommand { get; set; }

        public RelayCommand InitCommand { get; set; }

        public RelayCommand LoopSimOrderCommand { get; set; }

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

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


        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
        public static void OnStaticPropertyChanged([CallerMemberName] string PropName = "")
        {
            StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(PropName));
        }

        public static string LoopOrderButtonContent { get { return _mLoopOrderButtonContent; } set { _mLoopOrderButtonContent = value; OnStaticPropertyChanged(); } }
        private static string _mLoopOrderButtonContent = "循环跑单";





    }
}

