using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Business;
using SlnMesnac.Business.@base;
using SlnMesnac.Plc;
using SlnMesnac.WPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;

namespace SlnMesnac.WPF.Page
{
    /// <summary>
    /// DevMonitorPage.xaml 的交互逻辑
    /// </summary>
    public partial class DevMonitorPage : UserControl
    {
        private BaseBusiness baseBusiness = null;
        PlcAbsractFactory plc = null;

        private readonly ILogger<DevMonitorPage> _logger;

   
        public DevMonitorPage()
        {
            _logger = App.ServiceProvider.GetService<ILogger<DevMonitorPage>>();
            baseBusiness = App.ServiceProvider.GetService<BaseBusiness>();
            plc = baseBusiness.GetPlcByKey("plc");
            InitializeComponent();

        }

        /// <summary>
        /// 发送脉冲信号方法封装
        /// </summary>
        /// <param name="addressKey">plc地址对应的key</param>
       
        private void SendPulseSignal(string addressKey)
        {
            int  PulseSleep = Convert.ToInt32(baseBusiness.GetPlcAddressByConfigKey("脉冲间隔"));
            plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("addressKey"), true);
            Thread.Sleep(PulseSleep);
            plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("addressKey"), false);
        }
        
        /// <summary>
        /// 一键启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
                {
                    MessageBox.Show("拆包机MES屏蔽，未切换远程");
                    return;
                }
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
                {
                    MessageBox.Show("磁选机MES屏蔽，未切换远程");
                    return;
                }
                StartButton.IsEnabled = false;
                StopButton.IsEnabled = true;
                StopUrgentButton.IsEnabled = true;
                Task.Run(() =>
                {
                    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
                    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"), false);

                    #region 按顺序依次启动设备


                    #endregion

                });
                //var aaa =  baseBusiness.GetPlcAddressByConfigKey("设备叫料");
            }catch(Exception ex)
            {
                _logger.LogError($"一键启动:{ex.Message}");
            }
        }


        /// <summary>
        /// 一键停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
                {
                    MessageBox.Show("拆包机MES屏蔽，未切换远程");
                    return;
                }
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
                {
                    MessageBox.Show("磁选机MES屏蔽，未切换远程");
                    return;
                }
                #region 依次按序停止
                Task.Run(() =>
                {

                });
                #endregion
                StopButton.IsEnabled = false;
                StopUrgentButton.IsEnabled = false;
                StartButton.IsEnabled = true;
            }catch (Exception ex)
            {
                _logger.LogError($"一键停止:{ex.Message}");
            }
        }

        /// <summary>
        /// 急停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopUrgentButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (StopUrgentButton.Content.ToString() == "急停")
                {
                    StopButton.IsEnabled = false;
                    StopUrgentButton.Content = "复位";

                    SendPulseSignal("拆包机远程急停");

                }
                else if (StopUrgentButton.Content.ToString() == "复位")
                {
                    StopButton.IsEnabled = true;
                    StopUrgentButton.Content = "急停";
                    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
                }
            }catch(Exception ex)
            {
                _logger.LogError($"拆包机远程急停:{ex.Message}");
            }
        }

        #region 拆包机
        /// <summary>
        /// 拆包机启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartUnPackButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
                {
                    MessageBox.Show("拆包机MES屏蔽，未切换远程");
                    return;
                }

                Task.Run(() =>
                {
                    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
                    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"), false);

                    SendPulseSignal("拆包机远程启动");
                   
                });
                StartUnPackButton.IsEnabled = false;
                StopUnPackButton.IsEnabled = true;
            }catch (Exception ex)
            {
                _logger.LogError($"拆包机远程启动:{ex.Message}");
            }

        }

        /// <summary>
        /// 拆包机停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopUnPackButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
                {
                    MessageBox.Show("拆包机MES屏蔽，未切换远程");
                    return;
                }

                Task.Run(() =>
                {

                    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程启动"), false);

                    SendPulseSignal("拆包机远程停止");
                   
                });
                StartUnPackButton.IsEnabled = true;
                StopUnPackButton.IsEnabled = false;
            }
            catch (Exception ex)
            {
                _logger.LogError($"拆包机停止:{ex.Message}");
            }
        }
        #endregion


        #region 磁选机
        /// <summary>
        /// 磁选启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartMagNetButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
                {
                    MessageBox.Show("磁选机MES屏蔽，未切换远程");
                    return;
                }
                Task.Run(() =>
                {
                    SendPulseSignal("磁选机一键启动");
                });
            }catch (Exception ex)
            {
                _logger.LogError($"磁选启动:{ex.Message}");
            }

        }

        /// <summary>
        /// 磁选停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopMagNetButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
                {
                    MessageBox.Show("磁选机MES屏蔽，未切换远程");
                    return;
                }

                Task.Run(() =>
                {
                    //  plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机一键启动"), false);

                    SendPulseSignal("磁选机一键停止");
                   
                });
            }
            catch (Exception ex)
            {
                _logger.LogError($"磁选停止:{ex.Message}");
            }
        }

        #endregion

        #region 螺旋机
        /// <summary>
        /// 螺旋1启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartSpiral1Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES屏蔽")) == false)
                {
                    MessageBox.Show("螺旋1MES屏蔽，未切换远程");
                    return;
                }
                plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), true);
            }
            catch (Exception ex)
            {
                _logger.LogError($"螺旋1启动:{ex.Message}");
            }

        }

      
        /// <summary>
        /// 螺旋2启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartSpiral2Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES屏蔽")) == false)
                {
                    MessageBox.Show("螺旋2MES屏蔽，未切换远程");
                    return;
                }
                plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), true);
            }catch (Exception ex)
            {
                 _logger.LogError($"螺旋2启动:{ex.Message}");
            }
        }

        /// <summary>
        /// 螺旋1停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopSpiral1Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES屏蔽")) == false)
                {
                    MessageBox.Show("螺旋1MES屏蔽，未切换远程");
                    return;
                }
                plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), false);
            }
            catch (Exception ex)
            {
                _logger.LogError($"螺旋1停止:{ex.Message}");
            }
        }

        /// <summary>
        /// 螺旋2停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopSpiral2Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES屏蔽")) == false)
                {
                    MessageBox.Show("螺旋2MES屏蔽，未切换远程");
                    return;
                }
                plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), false);
            }
            catch (Exception ex)
            {
                _logger.LogError($"螺旋2停止:{ex.Message}");
            }
        }

        #endregion


        /// <summary>
        /// 启动烘干机螺旋1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartDryerSpiralButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task.Run(() =>
                {
                    SendPulseSignal("烘干机螺旋启动");
                });
            }
            catch (Exception ex)
            {
                _logger.LogError($"烘干机螺旋启动异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 启动烘干机风机2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartDryerFanButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task.Run(() =>
                {
                    SendPulseSignal("烘干机风机启动");
                });
               
            }
            catch (Exception ex)
            {
                _logger.LogError($"烘干机风机启动:{ex.Message}");
            }
          
        }

        /// <summary>
        /// 启动烘干机燃烧3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartDryerHotButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task.Run(() =>
                {
                    SendPulseSignal("烘干机燃烧启动");
                });

               
            }
            catch (Exception ex)
            {
                _logger.LogError($"烘干机燃烧启动:{ex.Message}");
            }
        }

        /// <summary>
        /// 停止烘干机螺旋
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopDryerSpiralButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task.Run(() =>
                {
                    SendPulseSignal("烘干机螺旋停止");
                });
               
            }
            catch (Exception ex)
            {
                _logger.LogError($"烘干机螺旋停止:{ex.Message}");
            }
        }


        /// <summary>
        /// 停止烘干机燃烧
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopDryerHotButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task.Run(() =>
                {
                    SendPulseSignal("烘干机燃烧停止");
                });
              
            }
            catch (Exception ex)
            {
                _logger.LogError($"烘干机燃烧停止:{ex.Message}");
            }
        }

     

        /// <summary>
        /// 停止烘干机风机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopDryerFanButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task.Run(() =>
                {
                    SendPulseSignal("烘干机风机停止");
                });
               
            }
            catch (Exception ex)
            {
                _logger.LogError($"烘干机风机停止:{ex.Message}");
            }
        }
    }
}
