using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.Plc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SlnMesnac.WPF.Page
{
    /// <summary>
    /// SelectWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SelectWindow : Window
    {
        private ISqlSugarClient? sqlSugarClient;
        private string code;

        public delegate void RefreshMsg(string msg, string palletInfoCode);

        //2#计量室相关点位
        private readonly PlcAbsractFactory? workShop2Plc;

        public static event RefreshMsg? RefreshMsgEvent;

        public SelectWindow(string palletInfoCode)
        {
            workShop2Plc = App.ServiceProvider.GetRequiredService<List<PlcAbsractFactory>>().FirstOrDefault();
            code = palletInfoCode;
            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
            InitializeComponent();
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WmsPalletInfo? wmsPalletInfo = sqlSugarClient.Queryable<WmsPalletInfo>().First(x => x.PalletInfoCode == code);
                if (wmsPalletInfo != null)
                {
                    wmsPalletInfo.Amount = 0;
                    wmsPalletInfo.TonerFlag = 0;
                    int result = sqlSugarClient.Updateable(wmsPalletInfo).ExecuteCommand();
                    if (result > 0)
                    {
                        RefreshMsgEvent?.Invoke($"托盘:{code}清空条码成功！", code);
                    }
                    else
                    {
                        RefreshMsgEvent?.Invoke($"托盘:{code}清空条码失败！", code);
                    }
                    if (workShop2Plc != null && workShop2Plc.IsConnected)
                    {
                        //写入放行信号
                        workShop2Plc.writeBoolByAddress("DB100.DBX34.0", true);
                    }
                }
            }
            catch (Exception ex)
            {
                RefreshMsgEvent?.Invoke($"托盘:{code}清空条码失败！{ex.Message}", code);
            }
            finally
            {
                this.Close();
            }
        }

        private void TonerButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WmsPalletInfo? wmsPalletInfo = sqlSugarClient.Queryable<WmsPalletInfo>().First(x => x.PalletInfoCode == code);
                if (wmsPalletInfo != null)
                {
                    wmsPalletInfo.Amount = 1;
                    wmsPalletInfo.TonerFlag = 1;
                    int result = sqlSugarClient.Updateable(wmsPalletInfo).ExecuteCommand();
                    if (result > 0)
                    {
                        RefreshMsgEvent?.Invoke($"托盘:{code}标记色粉成功！", code);
                    }
                    else
                    {
                        RefreshMsgEvent?.Invoke($"托盘:{code}标记色粉失败！", code);
                    }
                    if (workShop2Plc != null && workShop2Plc.IsConnected)
                    {
                        //写入放行信号
                        workShop2Plc.writeBoolByAddress("DB100.DBX34.0", true);
                    }
                }
            }
            catch (Exception ex)
            {
                RefreshMsgEvent?.Invoke($"托盘:{code}标记色粉失败！{ex.Message}", code);
            }
            finally
            {
                this.Close();
            }
        }
    }
}