using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Business;
using SlnMesnac.Business.@base;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Plc;
using SlnMesnac.WPF.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：14008fcc-0a31-4f1e-bc80-9f9ea84d3de5
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-10 16:18:57
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    /// <summary>
    /// 设备参数监控
    /// </summary>
    internal class DevMonitorViewModel : ViewModelBase
    {
        private BaseBusiness baseBusiness = null;
        private PlcAbsractFactory plc = null;
        //  private readonly PalletStowBusiness _palletStowBusiness;
        //  private readonly ProdCompletionBusiness _prodCompletionBusiness;
        public DevMonitorViewModel()
        {
            baseBusiness = App.ServiceProvider.GetService<BaseBusiness>();
           
            // _palletStowBusiness = App.ServiceProvider.GetService<PalletStowBusiness>();
            //  _prodCompletionBusiness = App.ServiceProvider.GetService<ProdCompletionBusiness>();
            //Task.Run(() =>
            //{
            //    while (true)
            //    {
            //        plc = baseBusiness.GetPlcByKey("plc");
            //        if (plc != null)
            //        {
            //           // RefreshHotDataGrid();
            //           // RefreshUnPackDataGrid();
            //            //RefreshMagNetDataGrid();
            //            //RefreshSpiralDataGrid();
            //        }
                   
            //        Thread.Sleep(2000);
            //    }

            //});

            //Task.Run(() =>
            //{
            //    Thread.Sleep(5000);
            //    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("设备投料"), true);
            //    Thread.Sleep(300);
            //    plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("设备投料"), false);
            //});
            //var palletTask = App.ServiceProvider.GetService<List<RealPalletTask>>();

            //RefreshPalletTaskDataGrid(palletTask);

            //  _palletStowBusiness.RefreshPalletTaskListEvent += RefreshPalletTaskDataGrid;

            //var barCodeTasks = App.ServiceProvider.GetService<List<RealBarCodeTask>>();
            //RefreshBarCodeTaskDataGrid(barCodeTasks);

            // _prodCompletionBusiness.RefreshBarCodeTaskListEvent += RefreshBarCodeTaskDataGrid;
        }

        #region 参数定义
        /// <summary>
        /// 烘干机DataGrid
        /// </summary>
        private ObservableCollection<PLCMachineModel> hotDataGrid;

        public ObservableCollection<PLCMachineModel> HotDataGrid
        {
            get { return hotDataGrid; }
            set { hotDataGrid = value; RaisePropertyChanged(() => HotDataGrid); }

        }

        /// <summary>
        /// 拆包机DataGrid
        /// </summary>
        private ObservableCollection<PLCMachineModel> unPackDataGrid;

        public ObservableCollection<PLCMachineModel> UnPackDataGrid
        {
            get { return unPackDataGrid; }
            set { unPackDataGrid = value; RaisePropertyChanged(() => UnPackDataGrid); }

        }

        
        /// <summary>
        /// 磁选机DataGrid
        /// </summary>
        private ObservableCollection<PLCMachineModel> magNetDataGrid;

        public ObservableCollection<PLCMachineModel> MagNetDataGrid
        {
            get { return magNetDataGrid; }
            set { magNetDataGrid = value; RaisePropertyChanged(() => MagNetDataGrid); }

        }

        /// <summary>
        /// 螺旋机DataGrid
        /// </summary>
        private ObservableCollection<PLCMachineModel> spiralDataGrid;

        public ObservableCollection<PLCMachineModel> SpiralDataGrid
        {
            get { return spiralDataGrid; }
            set { spiralDataGrid = value; RaisePropertyChanged(() => SpiralDataGrid); }

        }


        ///// <summary>
        ///// 条码队列DataGrid
        ///// </summary>
        //private ObservableCollection<RealBarCodeTask> barCodeTaskDataGrid;

        //public ObservableCollection<RealBarCodeTask> BarCodeTaskDataGrid
        //{
        //    get { return barCodeTaskDataGrid; }
        //    set { barCodeTaskDataGrid = value; RaisePropertyChanged(() => BarCodeTaskDataGrid); }

        //}
        #endregion


        ///// <summary>
        ///// 刷新托盘队列信息
        ///// </summary>
        ///// <param name="palletTasks"></param>
        //private void RefreshPalletTaskDataGrid(List<PLCMachineModel> palletTasks)
        //{
        //    hotDataGrid = new ObservableCollection<PLCMachineModel>();
        //    foreach (var task in palletTasks)
        //    {
        //        hotDataGrid.Add(task);
        //    }
        //}

        ///// <summary>
        ///// 刷新条码队列信息
        ///// </summary>
        ///// <param name="barCodeTasks"></param>
        //private void RefreshBarCodeTaskDataGrid(List<RealBarCodeTask> barCodeTasks)
        //{
        //    BarCodeTaskDataGrid = new ObservableCollection<RealBarCodeTask>();
        //    foreach (var task in barCodeTasks)
        //    {
        //        BarCodeTaskDataGrid.Add(task);
        //    }
        //}

        #region 烘干机参数监控
        private async void RefreshHotDataGrid()
        {
            try
            {
                HotDataGrid = new ObservableCollection<PLCMachineModel>();        
                int signal0 = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机风机状态"));
                int signal1 = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机燃烧机状态"));
                int signal2 = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机螺旋状态"));

                int signal3 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机烘干温度"));
                int signal4 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机加热温度"));
                int signal5 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机出气温度"));
                int signal6 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机输出百分比"));
                int signal7 = plc.readByteByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机报警"));

                await App.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机风机状态",
                        Value = ChangeHotStatus(signal0)
                    });
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机燃烧机状态",
                        Value = ChangeHotStatus(signal1)
                    });
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机螺旋状态",
                        Value = ChangeHotStatus(signal2)
                    });
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机烘干温度",
                        Value = ((double)signal3 / 10).ToString()
                    }); ;
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机加热温度",
                        Value = ((double)signal4 / 10).ToString()
                    });
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机出气温度",
                        Value = ((double)signal5 / 10).ToString()
                    });
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机输出百分比",
                        Value = signal6.ToString()
                    });
                    HotDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "烘干机报警",
                        Value = ChangeHotWarn(signal7)
                    });
                }));      
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
             
        }

        private string ChangeHotStatus(int status)
        {
            switch (status) {
                case 0: return "未知";break;
                case 1: return "运行";break;
                case 2: return "停止";break;
                case 3: return "故障";break;
                default: return "未知";break;

            }

        }

        private string ChangeHotWarn(int status)
        {
         
            switch (status)
            {
                case 0: return "无"; break;
                case 1: return "风机故障"; break;
                case 2: return "燃烧机故障"; break;
                case 3: return "传动故障"; break;
                case 4: return "超温保护"; break;
                case 5: return "烘干温度超温"; break;
                case 6: return "温控模块通讯超时"; break;
                default: return "未知"; break;

            }

        }
        #endregion



        #region 拆包机参数监控
        private async void RefreshUnPackDataGrid()
        {
            try
            {
                UnPackDataGrid = new ObservableCollection<PLCMachineModel>();
                bool unPackStatus = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机运行状态系统"));
                bool signal0 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES允许远程"));
                bool signal1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程启动"));
                bool signal2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"));
                bool signal3 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"));
                bool signal4 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("设备叫料"));
                bool signal5 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("物料到位"));
                bool signal6 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("设备投料"));
                bool signal7 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机请求移走空托盘"));             
                await App.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "拆包机运行状态系统",
                        Value = unPackStatus.ToString()
                    }) ;
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "拆包机MES允许远程",
                        Value = signal0.ToString()
                    }) ;
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "拆包机远程启动",
                        Value = signal1.ToString()
                    });
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "拆包机远程停止",
                        Value = signal2.ToString()
                    });
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "拆包机远程急停",
                        Value = signal3.ToString()
                    });
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "设备叫料",
                        Value = signal4.ToString()
                    });
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "物料到位",
                        Value = signal5.ToString()
                    });
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "设备投料",
                        Value = signal6.ToString()
                    });
                    UnPackDataGrid.Add(new PLCMachineModel()
                    {
                        Name = "拆包机请求移走空托盘",
                        Value = signal7.ToString()
                    });
                }));



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private string ChangeUnPackStatus(int status)
        {
            switch (status)
            {
                case 0: return "停止中"; break;
                case 1: return "正在进料"; break;
                case 2: return "正在提升"; break;
                case 3: return "正在出料"; break;
                case 4: return "正在复位"; break;
                default: return "未知"; break;

            }

        }
        #endregion



        #region 磁选机参数监控
        //private async void RefreshMagNetDataGrid()
        //{
        //    try
        //    {
        //        MagNetDataGrid = new ObservableCollection<PLCMachineModel>();
        //        bool signal0 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES允许远程"));
        //        bool signal1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机设备故障反馈"));
        //        bool signal2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机震动启动"));
        //        bool signal3 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机磁选启动"));
        //       // bool signal4 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机心跳包"));

        //        await App.Current.Dispatcher.BeginInvoke((Action)(() =>
        //        {
        //            MagNetDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "磁选机MES允许远程",
        //                Value = signal0.ToString()
        //            }) ;
        //            MagNetDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "磁选机设备故障反馈",
        //                Value = signal1.ToString()
        //            });
        //            MagNetDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "磁选机震动启动",
        //                Value = signal2.ToString()
        //            });
        //            MagNetDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "磁选机磁选启动",
        //                Value = signal3.ToString()
        //            });
        //            //MagNetDataGrid.Add(new PLCMachineModel()
        //            //{
        //            //    Name = "磁选机心跳包",
        //            //    Value = signal4.ToString()
        //            //});
        //        }));
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }

        //}

        #endregion



        #region 螺旋机参数监控
        //private async void RefreshSpiralDataGrid()
        //{
        //    try
        //    {
        //        SpiralDataGrid = new ObservableCollection<PLCMachineModel>();
        //        bool signal0 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES允许远程"));
        //        bool signal1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES允许远程"));
        //        bool signal2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1状态"));
        //        bool signal3 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1变频器状态"));
        //        bool signal4 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2状态"));
        //        bool signal5 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2变频器状态"));
        //        int signal6 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1速度反馈"));
        //        int signal7 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2速度反馈"));

        //        await App.Current.Dispatcher.BeginInvoke((Action)(() =>
        //        {
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋1MES允许远程",
        //                Value = signal0.ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋2MES允许远程",
        //                Value = signal1.ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋1状态",
        //                Value = signal2.ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋1变频器状态",
        //                Value = signal3.ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋2状态",
        //                Value = signal4.ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋2变频器状态",
        //                Value = signal5.ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋1速度反馈",
        //                Value = ((double)signal6 / 100).ToString()
        //            });
        //            SpiralDataGrid.Add(new PLCMachineModel()
        //            {
        //                Name = "螺旋2速度反馈",
        //                Value = ((double)signal7 / 100).ToString()
        //            });
        //        }));
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }

        //}

        #endregion

    }
}
