using System;
using CommunityToolkit.Mvvm.Input;
using Aucma.Core.Palletiz.Business;
using Admin.Core.Model;
using Aucma.Core.Palletiz.Models;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.ObjectModel;
using System.Linq;
using Aucma.Core.Palletiz.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Threading.Tasks;
using static Aucma.Core.Palletiz.ViewModels.StatisticsPageViewModel;

/*
 * 成品分垛入库首页信息
 * 2024-02-28
 */
namespace Aucma.Core.Palletiz.ViewModels
{
    internal class IndexControlViewModel : ObservableObject
    {
        private InStoreBusiness inStoreBusiness;

        public IndexControlViewModel()
        {

            new InStoreBusiness().Init();
            SelectTypeViewModel.RefreshPageEvent += Init;
            StatisticsPageViewModel.CountInstoreEvent += CountInstore;
            this.inStoreBusiness = App.ServiceProvider.GetService<InStoreBusiness>();
            InStoreBusiness.RefreshMsgEvent += RefreshMsg;
            InStoreBusiness.RefreshProductInfoEvent += RefreshProductInfo;
            UpdateInStoreFlagCommand = new RelayCommand<Int32>(obj =>
            {
                Console.WriteLine($"{obj};设置");
                BaseSpaceInfo space = inStoreBusiness.GetSpaceinfosById(obj);
                if (space != null)
                {
                    SelectType direct = new SelectType(space);
                    direct.ShowDialog();
                }

            });


            this.Init();

            InStoreTaskContent = new StatisticsPageView();
        }

        public void CountInstore(int count)
        {
            InStoreAmount = "入库数量:";
            InStoreAmount = inStoreAmount + count;
        }

        #region 参数定义

        public string productSNCode = string.Empty;
        public string ProductSNCode
        {
            get { return this.productSNCode; }
            set => SetProperty(ref productSNCode, value);

        }

        public string productModel = string.Empty;
        public string ProductModel
        {
            get { return this.productModel; }
            set => SetProperty(ref productModel, value);

        }

        public string orderCode = string.Empty;
        public string OrderCode
        {
            get { return this.orderCode; }
            set => SetProperty(ref orderCode, value);


        }

        public string productScanTime = string.Empty;
        public string ProductScanTime
        {
            get { return this.productScanTime; }

            set => SetProperty(ref productScanTime, value);
        }

        public string msg = string.Empty;
        public string Msg
        {
            get { return this.msg; }
            set => SetProperty(ref msg, value);

        }
        public string inStoreAmount = "入库数量:";
        public string InStoreAmount
        {
            get { return this.inStoreAmount; }
            set => SetProperty(ref inStoreAmount, value);


        }

        public System.Windows.Controls.UserControl _content;

        public System.Windows.Controls.UserControl InStoreTaskContent
        {
            get { return this._content; }
            set => SetProperty(ref _content, value);



        }

        /// <summary>
        /// A区域货道集合
        /// </summary>
        public ObservableCollection<BaseSpaceInfo> _areaA_SpaceInfo;

        public ObservableCollection<BaseSpaceInfo> AreaA_SpaceInfo
        {
            get { return _areaA_SpaceInfo; }
            set => SetProperty(ref _areaA_SpaceInfo, value);

        }

        /// <summary>
        /// B区域货道集合
        /// </summary>
        public ObservableCollection<BaseSpaceInfo> _areaB_SpaceInfo;

        public ObservableCollection<BaseSpaceInfo> AreaB_SpaceInfo
        {
            get { return _areaB_SpaceInfo; }
            set => SetProperty(ref _areaB_SpaceInfo, value);

        }
        #endregion

        public RelayCommand<Int32> UpdateInStoreFlagCommand { get; set; }

        private ObservableCollection<BaseSpaceInfo> spaceItems = new ObservableCollection<BaseSpaceInfo>();

        private ObservableCollection<BaseSpaceInfo> spaceItemsB = new ObservableCollection<BaseSpaceInfo>();


        /// <summary>
        /// 刷新界面提示信息
        /// </summary>
        /// <param name="msg"></param>
        public void RefreshMsg(string msg)
        {
            Msg = msg;
        }
        /// <summary>
        /// 刷新界面扫码信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <param name="productModel"></param>
        /// <param name="orderCode"></param>
        public void RefreshProductInfo(string productCode, string productModel, string orderCode)
        {
            ProductSNCode = productCode;
            ProductModel = productModel;
            OrderCode = orderCode;
            ProductScanTime = DateTime.Now.ToString();
        }
        private async Task Init()
        {
            await Task.Run(() =>
            {
                var info = inStoreBusiness.GetBaseSpaceinfos("A");
                info = info.OrderByDescending(x => x.ObjId).ToList();
                if (info != null)
                {
                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        if (spaceItems.Count > 0)
                        {
                            spaceItems.Clear();
                        }
                        foreach (var item in info)
                        {
                            spaceItems.Add(item);
                        }
                        AreaA_SpaceInfo = spaceItems;
                    }));
                }

                var info2 = inStoreBusiness.GetBaseSpaceinfos("A");
                info2 = info2.OrderBy(x => x.ObjId).ToList();
                if (info2 != null)
                {
                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        if (spaceItemsB.Count > 0)
                        {
                            spaceItemsB.Clear();
                        }
                        foreach (var item in info2)
                        {
                            spaceItemsB.Add(item);
                        }
                        AreaB_SpaceInfo = spaceItemsB;
                    }));
                }

            });

           

        }




    }
}