using CommunityToolkit.Mvvm.Input;
using GalaSoft.MvvmLight;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.WPF.DTO;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SlnMesnac.WPF.ViewModel
{
    public partial class LocationAndStockViewModel : ViewModelBase
    {
        private ISqlSugarClient? sqlSugarClient;

        public LocationAndStockViewModel()
        {
            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
        }

        #region 界面属性定义

        /// <summary>
        /// 色粉库库存
        /// </summary>
        private ObservableCollection<TonerLocationInfo> tonerLocationInfoList = new ObservableCollection<TonerLocationInfo>();

        public ObservableCollection<TonerLocationInfo> TonerLocationInfoList
        {
            get { return tonerLocationInfoList; }
            set
            {
                tonerLocationInfoList = value;
                RaisePropertyChanged(() => TonerLocationInfoList);
            }
        }

        private ObservableCollection<WmsBaseLocationInfo> wmsBaseLocationInfoList = new ObservableCollection<WmsBaseLocationInfo>();

        public ObservableCollection<WmsBaseLocationInfo> WmsBaseLocationInfoList
        {
            get { return wmsBaseLocationInfoList; }
            set
            {
                wmsBaseLocationInfoList = value;
                RaisePropertyChanged(() => WmsBaseLocationInfoList);
            }
        }

        #endregion 界面属性定义

        [RelayCommand]
        private void RefreshData()
        {
            RefreshTonerInfo();

            RefreshLoction();
        }

        private void RefreshTonerInfo()
        {
            try
            {
                //查询色粉库位
                List<TonerLocationInfo> tonerLocationInfos = sqlSugarClient.Queryable<WcsBaseEquip>()
               .LeftJoin<WmsPalletInfo>((weq, wpi) => weq.ContainerCode == wpi.PalletInfoCode)
               .Where(weq => weq.EquipType == 7)
              .Select((weq, wpi) => new TonerLocationInfo
              {
                  EquipName = weq.EquipName,
                  WarehouseId = weq.WarehouseId,
                  AgvPositionCode = weq.AgvPositionCode,
                  ContainerCode = string.IsNullOrEmpty(weq.ContainerCode) ? "" : weq.ContainerCode,
                  TonerFlag = string.IsNullOrEmpty(weq.ContainerCode)
                           ? "无"
                           : (wpi != null && wpi.TonerFlag == 1 ? "有色粉" : "无")
              })
            .ToList();

                App.Current.Dispatcher.Invoke(() =>
                {
                    TonerLocationInfoList.Clear();
                    foreach (var item in tonerLocationInfos)
                    {
                        TonerLocationInfoList.Add(item);
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void RefreshLoction()
        {
            try
            {
                //查询1-12机台库位库存
                List<WmsBaseLocationInfo> wmsBaseLocationInfos = sqlSugarClient.Queryable<WmsBaseLocation>()
                                                                 .LeftJoin<WmsPalletInfo>((wbl, wpi) => wbl.ContainerCode == wpi.PalletInfoCode)
                                                                 .LeftJoin<WmsMachineInfo>((wbl, wpi, wmi) => wbl.MachineId == wmi.MachineId)
                                                                 .Where(wbl => wbl.MachineId <= 12)
                                                                 .Select((wbl, wpi, wmi) => new WmsBaseLocationInfo
                                                                 {
                                                                     LocationCode = wbl.LocationCode,
                                                                     MachineCode = wmi.MachineCode,
                                                                     AgvPositionCode = wbl.AgvPositionCode,
                                                                     ContainerCode = wbl.ContainerCode,
                                                                     Amount = wpi.Amount,
                                                                     LocationStatus = wbl.LocationStatus == 0 ? "正常" : "锁定"
                                                                 }).ToList();
                App.Current.Dispatcher.Invoke(() =>
                {
                    WmsBaseLocationInfoList.Clear();
                    foreach (var item in wmsBaseLocationInfos)
                    {
                        WmsBaseLocationInfoList.Add(item);
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}