using HighWayIot.Log4net;
using HighWayIot.Plc;
using HighWayIot.Plc.PlcHelper;
using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
using Timer = System.Threading.Timer;

namespace HighWayIot.Winform.Business
{
    /// <summary>
    /// 排程信号业务类
    /// </summary>
    public class RecipeSendBusiness
    {
        private Timer GetSchedulingTimer;

        private RecipeSignal recipeSignal = new RecipeSignal();

        private ZxSchedulingService zxSchedulingService = ZxSchedulingService.Instance;

        private ZxRecipeParaService zxRecipeParaService = ZxRecipeParaService.Instance;

        private ZxRecipePositionParaService zxRecipePositionParaService = ZxRecipePositionParaService.Instance;

        private RecipeParaHelper recipeParaHelper = new RecipeParaHelper();

        public static int RecipeNeededVehicleNo = 0;
            
        public RecipeSendBusiness()
        {
            //GetSchedulingTimer = new Timer(new System.Threading.TimerCallback(ReadSignal), null, 0, 1000);
        }

        /// <summary>
        /// 重复读取
        /// </summary>
        /// <param name="o"></param>
        public void ReadSignal(object o)
        {
            Dictionary<int, bool> a = recipeSignal.ReadSchedulingSignal();

            if (a == null)
            {
                return;
            }

            foreach (var item in a)
            {
                if (item.Value)
                {
                    var schedulingInfo = zxSchedulingService.GetSchedulingInfo();
                    string recipeNo;
                    ZxRecipeParaEntity recipeParaInfo;
                    List<ZxRecipePositionParaEntity> zxRecipePositionParaEntities;
                    if ((item.Key + 1) <= 62)
                    {
                        recipeNo = schedulingInfo.Single(x => x.Id == item.Key + 1).RecipeCode1;
                        recipeParaInfo = zxRecipeParaService.GetRecipeParaInfoByRecipeCode(recipeNo).FirstOrDefault();
                        zxRecipePositionParaEntities = zxRecipePositionParaService.GetRecipePositionParaInfos(x => x.RecipeCode == recipeNo);
                        if (recipeParaInfo == null)
                        {
                            LogHelper.Instance.Error("配方信息获取失败");
                            return;
                        }
                        if (recipeParaHelper.UploadToPLC(recipeParaInfo, zxRecipePositionParaEntities))
                        {
                            PlcConnect.MelsecInstance2.Write($"B{(item.Key + 0x901).ToString("X")}", false);
                            int deviceNo = schedulingInfo.Single(x => x.Id == item.Key + 1).DeviceNo ?? 0;
                            MonitorInsert(recipeNo, $"{deviceNo}上模");
                        }
                    }
                    else
                    {
                        recipeNo = schedulingInfo.Single(x => x.Id == item.Key + 1).RecipeCode2;
                        recipeParaInfo = zxRecipeParaService.GetRecipeParaInfoByRecipeCode(recipeNo).FirstOrDefault();
                        zxRecipePositionParaEntities = zxRecipePositionParaService.GetRecipePositionParaInfos(x => x.RecipeCode == recipeNo);
                        if (recipeParaInfo == null)
                        {
                            LogHelper.Instance.Error("配方信息获取失败");
                            return;
                        }
                        if (recipeParaHelper.UploadToPLC(recipeParaInfo, zxRecipePositionParaEntities))
                        {
                            PlcConnect.MelsecInstance2.Write($"B{(item.Key + 0x941).ToString("X")}", false);
                            int deviceNo = schedulingInfo.Single(x => x.Id == item.Key + 1 - 62).DeviceNo ?? 0;
                            MonitorInsert(recipeNo, $"{deviceNo}下模");
                        }
                    }
                    return;
                }
            }
        }

        /// <summary>
        /// 监控画面信息插入
        /// </summary>
        public void MonitorInsert(string recipeCode, string deviceNo)
        {
            ZxRecipeEntity recipeEntity = ZxRecipeService.Instance.GetRecipeInfosByRecipeCode(recipeCode).FirstOrDefault();
            if (recipeEntity == null)
            {
                return;
            }

            ZxDailyReportEntity entity = new ZxDailyReportEntity()
            {
                Uuid = Guid.NewGuid().ToString("N"),
                VulcanizationNo = deviceNo,
                StartTime = DateTime.Now,
                RecipeName = recipeEntity.RecipeName,
                RecipeCode = recipeEntity.RecipeCode,
                SpecCode = recipeEntity.RecipeSpecCode,
                DeviceNo = RecipeNeededVehicleNo,
                IsDone = 0
            };

            ZxDailyReportService.Instance.InsertDailyReportInfo(entity);
        }
    }
}
