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;

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

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

            bql = new RelayCommand(() => { ActionOperate.GetInstance.Send("bql"); });
            cfj = new RelayCommand(() => { ActionOperate.GetInstance.Send("cfj"); });

            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);
                }

                ActionOperate.GetInstance.Send("SimOrder", locs);

                //if (simOrderVisibleDatas.ElementAt(0).IsSelected)
                //    simOrderVisibleDatas.ElementAt(0).Loc = (ushort)(new Random().Next(1, 6));

                //if (simOrderVisibleDatas.ElementAt(1).IsSelected)
                //    simOrderVisibleDatas.ElementAt(1).Loc = (ushort)(new Random().Next(6, 11));

                //if (simOrderVisibleDatas.ElementAt(2).IsSelected)
                //    simOrderVisibleDatas.ElementAt(2).Loc = (ushort)(new Random().Next(11, 12));

                //ActionOperate.GetInstance.Send("SimOrder", new SimOrderData()
                //{
                //    NoodleLoc = simOrderVisibleDatas.ElementAt(0).Loc,
                //    SoupLoc = simOrderVisibleDatas.ElementAt(1).Loc,
                //    BowlLoc = simOrderVisibleDatas.ElementAt(2).Loc,
                //});
            });

            InitCommand = new RelayCommand(() =>
            {
                ActionOperate.GetInstance.Send("InitCommand");
            });

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

            });
            //ThreadOperate.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));

            //    //            ActionOperate.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);
            //}), "循环订单");
        }

        //static DebugViewModel()
        //{

        //    simOrderVisibleDatas.Add(new SimOrderVisibleData() { Text = "取面位置：", IsEnable = false, IsSelected = true, Loc = 1 });
        //    simOrderVisibleDatas.Add(new SimOrderVisibleData() { Text = "取浇头位置：", IsEnable = false, IsSelected = true, Loc = 6 });
        //    simOrderVisibleDatas.Add(new SimOrderVisibleData() { Text = "取碗位置：", IsEnable = false, IsSelected = true, Loc = 11 });
        //}

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



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

        public RelayCommand AddSimDataCommand { get; set; }

        public RelayCommand SimOrderCommand { get; set; }

        public RelayCommand InitCommand { get; set; }

        public RelayCommand LoopSimOrderCommand { get; set; }

        public RelayCommand bql { get; set; }
        public RelayCommand cfj { get; set; }


        //public static ObservableCollection<SimOrderVisibleData> simOrderVisibleDatas { get; set; } = new ObservableCollection<SimOrderVisibleData>();

        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"));


    }
}
