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

namespace SlnMesnac.WPF.ViewModel
{
    public partial class TonerBoxingViewModel : ViewModelBase
    {
        private ISqlSugarClient? sqlSugarClient;
        private readonly ILogger<TonerBoxingViewModel> _logger;

        public TonerBoxingViewModel()
        {
            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
            SelectWindow.RefreshMsgEvent += RefreshMessage;
            _logger = App.ServiceProvider.GetService<ILogger<TonerBoxingViewModel>>()!;
        }

        #region 界面属性定义

        private string _boxCode = "";

        public string BoxCode
        {
            get { return _boxCode; }
            set
            {
                if (_boxCode != value)
                {
                    _boxCode = value;
                    RaisePropertyChanged(nameof(BoxCode));
                    OnBoxCodeChanged();  // 调用处理逻辑
                }
            }
        }

        //提示信息
        private string _msg = "";

        public string Msg
        {
            get { return _msg; }
            set
            {
                if (_msg != value)
                {
                    _msg = value;
                    RaisePropertyChanged(nameof(Msg));
                }
            }
        }

        #endregion 界面属性定义

        [RelayCommand]
        private void Confirm()
        {
            Msg = "";
            BoxCode = "";
        }

        private void OnBoxCodeChanged()
        {
            if (_boxCode.Length == 12)
            {
                _boxCode = _boxCode.ToUpper();
                if (_boxCode.StartsWith("SH"))
                {
                    SelectWindow selectWindow = new SelectWindow(_boxCode);
                    selectWindow.ShowDialog();
                }
                else
                {
                    Msg = "编码规则有误，请检查是否SHX/SHS开头的12位编码";
                }
            }
        }

        private void RefreshMessage(string Message, string code)
        {
            Msg = Message;
            _boxCode = "";
            WmsPalletInfo? wmsPalletInfo = sqlSugarClient.Queryable<WmsPalletInfo>().First(x => x.PalletInfoCode == code);
            if (wmsPalletInfo != null)
            {
                string flag = wmsPalletInfo.TonerFlag == 1 ? "是" : "否";
                Msg += $"库存:{wmsPalletInfo.Amount},是否携带色粉:{flag}";
            }
            _logger.LogInformation($"料箱{code}人工操作记录：" + Msg);
        }

        //[RelayCommand]
        //private void Confirm()
        //{
        //    if (string.IsNullOrEmpty(BoxCode))
        //    {
        //        MessageBox.Show("请先扫描或输入装色粉料箱条码");
        //        return;
        //    }
        //    //校验料箱编码规则
        //    if (BoxCode.Length != 12 || !BoxCode.StartsWith("SH"))
        //    {
        //        MessageBox.Show("编码规则有误，请检查是否SHX/SHS开头的12位编码");
        //        return;
        //    }

        //    //RFID条码
        //    Console.WriteLine(BoxCode);
        //    WmsPalletInfo palletInfo = new WmsPalletInfo();
        //    palletInfo.PalletInfoCode = BoxCode;
        //    // palletInfo.Barcode = "SF0000001";
        //    palletInfo.UpdatedTime = DateTime.Now;
        //    palletInfo.UpdatedBy = "wpf";
        //    palletInfo.CreatedTime = DateTime.Now;
        //    palletInfo.CreatedBy = "wpf";
        //    int count = sqlSugarClient.Insertable<WmsPalletInfo>(palletInfo).ExecuteCommand();
        //    if (count > 0)
        //    {
        //        MessageBox.Show("装色粉料箱条码录入成功");
        //    }
        //    else
        //    {
        //        MessageBox.Show("录入失败");
        //    }
        //    BoxCode = "";
        //}
    }
}