using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using DbCommon.Extenions;
using DbCommon.Function;
using Newtonsoft.Json;
using PmsSecondaryPackaging.Interface.Model.Carrier;
using PmsSecondaryPackaging.Interface.Model.TaskBook;
using PmsSecondaryPackaging.Interface.Service;
using ProjectManagementSystem.Common.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProjectManagementSystem.OrderLogic
{
    public class COrderLogic
    {
        private static COrderLogic m_instance;
        private LocationPropertyManager m_locationManager = new LocationPropertyManager();
        public InterfaceService interfaceService { get; set; } = new InterfaceService();

        public static COrderLogic Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new COrderLogic();
                }
                return m_instance;
            }
        }
        public List<IOrderLogic> Logics { get; private set; } = new List<IOrderLogic>();


        private COrderLogic()
        {
            Logics.Add(new Logic_3F还原车间());
            Logics.Add(new Logic_3F空车缓存区());

            Task.Factory.StartNew(TaskThread);
        }

        private void TaskAdd(TaskAddInfoModel data)
        {
            bool result = interfaceService.TaskAdd(data, out string Message);
            CLog.Instance.GetLog("Task").WriteInfo($"{data.TaskID} {data.ParentTaskId} {data.TemplateName} {data.Parameters} 任务添加{result.ToChineseString()}");
            if (result)
            {
                string[] posArray = data.ParametersDic.Values.ToArray().Distinct().ToArray();

                for (int i = 0; i < posArray.Length; i++)
                {
                    string location = posArray[i];

                    var locationData = m_locationManager.QueryData(location);
                    if (locationData.Status != LocationStatus.Locked
                        && locationData.Status != LocationStatus.LineCall)
                    {
                        //锁定库位状态并备份状态
                        m_locationManager.UpdateStatusAndStatusBackup(location, LocationStatus.Locked, locationData.Status);
                    }

                    if (locationData.Status == LocationStatus.LineCall)
                    {
                        m_locationManager.UpdateStatusAndStatusBackup(location, LocationStatus.Locked, Tools.ParseStatus(locationData.StatusBackup));
                    }
                }
            }
        }

        public void LogicsRunProc()
        {
            for (int i = 0; i < Logics.Count; i++)
            {
                var logic = Logics[i];

                //检查叫料状态
                if (logic.FirstCheck())
                {
                    if (logic.SecondCheck())
                    {
                        //检查是否满足任务条件
                        var data = logic.GetTask();
                        if (data != null)
                        {
                            TaskAdd(data);
                        }
                        else
                        {
                            //成功叫料但不满足条件时的处理
                            logic.CallTaskCheckFailProc();
                        }
                    }
                    else
                    {
                        logic.SecondCheckFailProc();
                    }
                }
            }
        }

        public void TaskThread()
        {
            byte couter = 0;
            while (true)
            {
                try
                {
                    LogicsRunProc();
                }
                catch (Exception ex)
                {
                    CLog.Instance.GetLog("Task").WriteException("Exception", ex);
                    Thread.Sleep(5000);
                }

                couter++;
                Thread.Sleep(500);
            }
        }
    }
}
