using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using LargeScreen.NoodleMorkS.ViewModel;
//using LargeScreen.Model;
using System.Collections.ObjectModel;
//using BPA.Message.Kafka;
using System.Collections.Concurrent;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Toolkit.Mvvm.Messaging;
using System.Threading;
using HBLConsole.ViewModel;
using HBLConsole.Model;
using HBLConsole.Service;

namespace HBLConsole.DialogWindow.ViewModel
{
    public class ListDialogViewModel : ViewModelBase
    {
        public static bool IsAutoClose = false;
        public ListDialogViewModel()
        {

            //for (int i = 0; i < 10; i++)
            //{
            //    Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Add(new OrderData()
            //    {
            //        IsSelected = true,
            //        OrderStatus = BPA.Message.Enum.ORDER_STATUS.WAIT,
            //        OrderPush = new BPA.Message.MorkOrderPush()
            //        {
            //            GoodsName = i.ToString(),
            //            RecipeId = i,
            //            SortNum = i,
            //            SuborderId = i.ToString(),
            //            GoodBatchings = new List<BPA.Message.GoodBatchings>()
            //        }
            //    });
            //}
            //Json<MorkOrderPushPar>.GetInstance.Save();




            CloseCommand = new RelayCommand(() =>
            {
                Json<MorkOrderPushPar>.Data.morkOrderPushes.Clear();
                orderStatusLists.Clear();
                WeakReferenceMessenger.Default.Send("false", "Close");
            });

            CancelCommand = new RelayCommand(() =>
            {
                Json<MorkOrderPushPar>.Data.morkOrderPushes.Clear();
                orderStatusLists.Clear();
                WeakReferenceMessenger.Default.Send("false", "Close");
            });

            ConfirmCommand = new RelayCommand(() =>
            {
                foreach (var item in orderStatusLists)
                {
                    if (!item.IsSelected)
                    {
                        var reslut = Json<MorkOrderPushPar>.Data.morkOrderPushes.FirstOrDefault(p => p.OrderPush.SuborderId == item.OrderPush.SuborderId);
                        if (reslut != null) Json<MorkOrderPushPar>.Data.morkOrderPushes.Remove(reslut);
                    }
                }
                foreach (var item in Json<MorkOrderPushPar>.Data.morkOrderPushes)
                {
                    orderStatusLists.Add(new OrderData() { IsSelected = true, OrderPush = item.OrderPush, OrderStatus = item.OrderStatus });
                }
                WeakReferenceMessenger.Default.Send("true", "Close");
            });

            foreach (var item in Json<MorkOrderPushPar>.Data.morkOrderPushes)
            {
                orderStatusLists.Add(new OrderData() { IsSelected = true, OrderPush = item.OrderPush, OrderStatus = item.OrderStatus });
            }

            if (Json<MorkOrderPushPar>.Data.morkOrderPushes.Count > 0)
            {
                ThreadOperate.GetInstance.Start(new Action(() =>
                {
                    Thread.Sleep(10000);
                    if (!IsAutoClose) WeakReferenceMessenger.Default.Send("false", "Close");
                }), "延时退出");
            }
            AllSelected = true;
        }

        /// <summary>
        /// 全选标志
        /// </summary>
        public bool AllSelected
        {
            get { return _mAllSelected; }
            set
            {
                _mAllSelected = value;
                OnPropertyChanged();
                for (int i = 0; i < orderStatusLists.Count; i++)
                {
                    orderStatusLists.ElementAt(i).IsSelected = value;
                }
            }
        }
        private bool _mAllSelected = true;

        public RelayCommand CloseCommand { get; set; }

        public RelayCommand CancelCommand { get; set; }

        public RelayCommand ConfirmCommand { get; set; }





    }
}
