using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.Input;
using HBLConsole.Service;
using System.Threading;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using HBLConsole.Communication;
using HBLConsole.Model;
using System.Reflection;
using System.Collections.Concurrent;
using HBLConsole.Attributes;
using System.Collections.ObjectModel;
using System.Windows.Media;
using HBLConsole.DialogWindow.View;
using System.Diagnostics;

namespace HBLConsole.ViewModel
{
    public class DebugViewModel : ViewModelBase
    {
        public DebugViewModel()
        {
            WindowName = "调试模式";

            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("InitCommand");
                //初始化按钮
                Trace.WriteLine("已点击初始化按钮。。。");
            });

            LoopSimOrderCommand = new RelayCommand(() =>
            {
                LoopButton = LoopButton == "循环跑单" ? "停止跑单" : "循环跑单";

            });
            //ThreadManage.GetInstance.StartLong(new Action(() =>
            //{
            //    //try
            //    //{
            //    //    System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)async delegate
            //    //    {
            //    //        if (IsLoop && !IsLoopzc)
            //    //        {
            //    //            IsLoopzc = true;
            //    //            if (simOrderVisibleDatas.ElementAt(0).IsSelected)
            //    //                simOrderVisibleDatas.ElementAt(0).Loc = (ushort)(new Random().Next(1,6));

            //    //            ActionManage.GetInstance.Send("SimOrder",new SimOrderData()
            //    //            {
            //    //                NoodleLoc = simOrderVisibleDatas.ElementAt(0).Loc,
            //    //                SoupLoc = simOrderVisibleDatas.ElementAt(1).Loc,
            //    //                BowlLoc = 10,
            //    //            });
            //    //            await Task.Delay(50000);
            //    //            IsLoopzc = false;
            //    //        }
            //    //    });

            //    //}
            //    //catch (Exception ex)
            //    //{

            //    //}
            //    Thread.Sleep(1000);
            //}), "循环订单");

            EditCommand = new RelayCommand<object>((o) =>
            {
                if (o != null)
                {
                    //if (Json<KeepDataBase>.Data.simOrderConfig.ContainsKey(GVL.GeneralConfig.DeviceType.ToString()))
                    //{
                    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)
                {
                    //if (Json<KeepDataBase>.Data.simOrderConfig.ContainsKey(GVL.GeneralConfig.DeviceType.ToString()))
                    //{
                    var res = Json<KeepDataBase>.Data.simOrderConfig.FirstOrDefault(p => p.Text == o.ToString());
                    if (res != null) Json<KeepDataBase>.Data.simOrderConfig.Remove(res);
                    //}
                }
            });
        }

        public ObservableCollection<SimOrderVisibleData> simOrderConfig
        {
            get
            {
                //if (!Json<KeepDataBase>.Data.simOrderConfig.ContainsKey(GVL.GeneralConfig.DeviceType.ToString()))
                //{
                //    Json<KeepDataBase>.Data.simOrderConfig.TryAdd(GVL.GeneralConfig.DeviceType.ToString(), new ObservableCollection<SimOrderVisibleData>());
                //}
                return Json<KeepDataBase>.Data.simOrderConfig;
            }
            set
            {
                //if (!Json<KeepDataBase>.Data.simOrderConfig.ContainsKey(GVL.GeneralConfig.DeviceType.ToString()))
                //{
                //    Json<KeepDataBase>.Data.simOrderConfig.TryAdd(GVL.GeneralConfig.DeviceType.ToString(), new ObservableCollection<SimOrderVisibleData>());
                //}
                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 bool IsLoop = false;
        public static bool IsLoopzc = false;

        public static string LoopButton
        {
            get { return _LoopButton; }
            set
            {
                _LoopButton = value;
                if (value != "循环跑单") { IsLoop = true; LoopColor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("Red")); }
                else { IsLoop = false; LoopColor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00c2f4")); }
                OnStaticPropertyChanged();
            }
        }
        private static string _LoopButton = "循环跑单";
        public static Brush LoopColor { get { return _LoopColor; } set { _LoopColor = value; OnStaticPropertyChanged(); } }
        private static Brush _LoopColor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00c2f4"));


    }
}
