using System.Collections.Generic;
using Aitex.Core.RT.Log;

namespace MECF.Framework.Common.MultiProcess.ProcessCenter
{
    /// <summary>
    /// 使用加热基数作为逻辑判断条件，S栋目前最多支持4个加热基数，   一个ProcessIdle是一个加热基数，Process是两个加热基数
    /// </summary>
    public class PMProcessStateManager
    {
        /// <summary>
        /// 返回PM每模块加热基数
        /// </summary>
        /// <returns></returns>
        public static int GetHeatBaseNum(PMProcessData pmProcessData)
        {
            return pmProcessData.Heat;
        }

        /// <summary>
        /// 返回设备当前加热状态， Process加热  ProcessIdle保温  OffHeart降温
        /// </summary>
        /// <param name="pmProcessData"></param>
        /// <returns></returns>
        public static string GetSicHeatState(PMProcessData pmProcessData)
        {
            return pmProcessData.PmRunState;
        }

        public static void ResetHeartState(PMProcessData pmProcessData)
        {
            pmProcessData.Reset();
        }

        public static List<string> GetProcessInformation(List<PMProcessData> pmProcessDataList)
        {
            List<string> inforList = new List<string>();
            int process = 0;
            int processIdle = 0;
            int heat = 0;
            foreach (var pmProcessData in pmProcessDataList)
            {
                if (pmProcessData.Heat == 2)
                    process++;
                else if (pmProcessData.Heat == 1)
                    processIdle++;
                heat += pmProcessData.Heat;
                inforList.Add($"State.{pmProcessData.SicMachineName}.{pmProcessData.PmName}.{pmProcessData.Heat}");
            }

            inforList.Add($"Total.{pmProcessDataList.Count}");
            inforList.Add($"Process.{process}");
            //inforList.Add($"ProcessIdle.{processIdle}");
            inforList.Add($"HeatBase.{heat}");
            return inforList;
        }

        public static string QueryMyProcessInformation(List<PMProcessData> pmProcessDataList, string sicMachineName, string pmName)
        {
            foreach (var pmProcessData in pmProcessDataList)
            {
                if (pmProcessData.SicMachineName == sicMachineName && pmProcessData.PmName == pmName)
                {
                    return pmProcessData.PmRunState;
                }
            }
            return "";
        }

        public static bool ProcessStateChangeUpdate(PMProcessData pmProcessDataNew, List<PMProcessData> pmProcessDataList)
        {
            for (int i = 0; i < pmProcessDataList.Count; i++)
            {
                if (pmProcessDataList[i].SicMachineID == pmProcessDataNew.SicMachineID)//根据设备ID筛选出对应的腔体
                {
                    if (pmProcessDataList[i].PmState == pmProcessDataNew.PmState) //状态无变化时，刷新心跳数据然后退出
                    {
                        pmProcessDataList[i].StateChangeTime = pmProcessDataNew.StateChangeTime;
                        pmProcessDataList[i].HeartBeatTime = pmProcessDataNew.HeartBeatTime;
                        return true;
                    }

                    //添加日志，记录状态变化
                    LOG.Write($"{pmProcessDataList[i].SicMachineID} state change from {pmProcessDataList[i].PmState} to {pmProcessDataNew.PmState}");
                    pmProcessDataList[i] = (PMProcessData)pmProcessDataNew.Clone();
                    return true;
                }
            }

            LOG.Write($"Add new state => {pmProcessDataNew.SicMachineID} : {pmProcessDataNew.PmState} state ");
            pmProcessDataList.Add((PMProcessData)pmProcessDataNew.Clone());
            return true;
        }
    }
}