using BPA.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using BPA.Model.Table;
using BPA.SingleDevice.Helper;
using BPA.Model;

namespace BPA.SingleDevice.ViewModel
{
    public class OrderMainViewModel : NotifyBase
    {
        public OrderMainViewModel()
        {
            SqlHelper.GetInstance.GetListAsync<RecipeTB>().Result.OnSuccess(s =>
            {
                s.ForEach(item =>
                {
                    if (item.IsEnable)
                        Goods.Add(new RecipeModel() { Id = item.Id, Name = item.Name, Count = 1 });
                });
            });

            RawMaters = SqlHelper.GetInstance.GetListAsync<RawMaterTB>().Result.Content;

            DownRecipeCommand = new BPARelayCommand<object>(o =>
            {
                if (o != null && !string.IsNullOrEmpty(o.ToString()))
                {
                    var res = Goods.FirstOrDefault(p => p.Id == o.ToString());
                    if (res != null)
                    {
                        List<RawMaterModel> rawMaters = new List<RawMaterModel>();
                        SqlHelper.GetInstance.GetRawMaterIds(res.Id).Result.OnSuccess(s =>
                        {
                            s.ForEach(item =>
                            {
                                var rmtb = RawMaters.FirstOrDefault(p => p.Id == item.RawMaterId);
                                if (rmtb != null)
                                {
                                    rawMaters.Add(new RawMaterModel()
                                    {
                                        Weight = item.Weight,
                                        DeviceNum = rmtb.DeviceNum,
                                        Id = rmtb.Id,
                                        LastModified = rmtb.LastModified,
                                        Name = rmtb.Name,
                                        WarehouseNum = rmtb.WarehouseNum,
                                    });
                                }
                            });
                        });

                        for (int i = 0; i < res.Count; i++)
                        {
                            List<RawMaterModel> temp = new List<RawMaterModel>();
                            rawMaters.ForEach(item =>
                            {
                                temp.Add(new RawMaterModel()
                                {
                                    Weight = item.Weight,
                                    WarehouseNum = item.WarehouseNum,
                                    Name = item.Name,
                                    LastModified = item.LastModified,
                                    Id = item.Id,
                                    DeviceNum = item.DeviceNum
                                });
                            });
                            GlobalData.GoodsModels.Enqueue(new GoodsModel() { RawMaters = temp });
                        }
                    }
                }
            });
        }
        private List<RawMaterTB> RawMaters { get; set; } = new List<RawMaterTB>();
        public ObservableCollection<RecipeModel> Goods { get; set; } = new ObservableCollection<RecipeModel>();

        public BPARelayCommand<object> DownRecipeCommand { get; set; }
    }
}
