using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Windows;
using BPASmartClient.Helper;
using Microsoft.Toolkit.Mvvm.Input;
using BPASmartClient.DosingSystem.Model;
using System.Threading;

namespace BPASmartClient.DosingSystem.ViewModel
{
    public class RecipeControlViewModel : ObservableObject
    {
        ConcurrentQueue<string> devices = new ConcurrentQueue<string>();

        public RecipeControlViewModel()
        {
            Recipes = Json<LocaPar>.Data.Recipes;
            StartCommand = new RelayCommand<object>((o) =>
            {
                if (o != null && o is string deviceName)
                {
                    //Task.Run(new Action(() =>
                    //{
                    int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
                    if (index >= 0 && index < Recipes.Count)
                    {
                        Recipes.ElementAt(index).IsEnable = false;
                        //foreach (var item in Recipes.ElementAt(index).RawMaterials)
                        //{
                        //    DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动写入
                        //}
                    }
                    //}));
                    devices.Enqueue(deviceName);
                }
            });


            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                if (devices.Count > 0)
                {
                    int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));
                    if (index >= 0 && index < Recipes.Count)
                    {
                        Recipes.ElementAt(index).Are.Reset();
                        Recipes.ElementAt(index).IsEnable = false;
                        foreach (var item in Recipes.ElementAt(index).RawMaterials)
                        {
                            DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动写入
                        }
                        Recipes.ElementAt(index).Are.WaitOne();
                        devices.TryDequeue(out string deviceName);
                    }

                }
                Thread.Sleep(100);
            }), "启动配发下发");


            ThreadManage.GetInstance().StartLong(new Action(() =>
            {
                for (int i = 0; i < Recipes.Count; i++)
                {
                    for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
                    {
                        var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
                        Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus;
                        var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList();
                        if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)
                        {
                            for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++)
                            {
                                DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();
                            }
                            Recipes.ElementAt(i).IsEnable = true;
                            Recipes.ElementAt(i).Are.Set();
                        }
                    }
                }
                Thread.Sleep(100);
            }), "RecipeControlViewModelStatusInquire");

        }

        public RelayCommand<object> StartCommand { get; set; }
        public ObservableCollection<RecipeModel> Recipes { get; set; }
    }
}
