using BPASmartClient.GSIceCream;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartClient.Model.冰淇淋.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BPASmartClient.GSIceCream.MessageDefine;

namespace BPASmartClient.GSIceCream
{
    public class MorkIStatus : Singleton<MorkIStatus>
    {
        private DateTime lastRefreshTime = DateTime.MinValue;
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
        /// <summary>
        /// 预冷温度
        /// </summary>
        public short YLWD { get; set; }
        /// <summary>
        /// 回气温度
        /// </summary>
        public short HQWD { get; set; }
        /// <summary>
        /// 环境温度
        /// </summary>
        public short HJWD { get; set; }
        /// <summary>
        /// 电流
        /// </summary>
        public short DL { get; set; }
        /// <summary>
        /// 电压
        /// </summary>
        public short DY { get; set; }
        /// <summary>
        /// 当前模式
        /// </summary>
        public MORKI_MODE CurrentMode { get; set; }
        /// <summary>
        /// 故障
        /// </summary>
        public MORKI_FAULT Fault { get; set; }
        /// <summary>
        /// 成型比
        /// </summary>
        public byte CXB { get; set; }
        /// <summary>
        /// 成型比(门限)
        /// </summary>
        public byte CXB_Threshold { get; set; }
        /// <summary>
        /// 打料完成（完成为true，正在打料为false）
        /// </summary>
        public bool DLCompleted { get; set; }


        public bool CanDo
        {
            get
            {
                if (!OnLine)
                    return false;
                if (Fault != MORKI_FAULT.未发生故障)
                    return false;
                if (CXB < CXB_Threshold)
                    return false;
                return true;
            }
        }

        private void ProcessHeart(ICMSG_Heart_UP heartUpMsg)
        {
            CurrentMode = heartUpMsg.MS;
            YLWD = BitConverter.ToInt16(new byte[] { heartUpMsg.YLWD_L, heartUpMsg.YLWD_H }, 0);
            HQWD = BitConverter.ToInt16(new byte[] { heartUpMsg.HQWD_L, heartUpMsg.HQWD_H }, 0);
            HJWD = BitConverter.ToInt16(new byte[] { heartUpMsg.HJWD_L, heartUpMsg.HJWD_H }, 0);
            DL = BitConverter.ToInt16(new byte[] { heartUpMsg.DL_L, heartUpMsg.DL_H }, 0);
            Fault = (MORKI_FAULT)BitConverter.ToInt16(new byte[] { heartUpMsg.GZ_L, heartUpMsg.GZ_H }, 0);
            CXB = heartUpMsg.CXB;
            DLCompleted = (heartUpMsg.DLTJ >> 4 & 1) == 1;

            if (RTrig.GetInstance("打料完成检测").Start(DLCompleted))
            {
                MessageLog.GetInstance.Show("打料完成");
            }

            if (RTrig.GetInstance("打料中检测").Start(!DLCompleted))
            {
                MessageLog.GetInstance.Show("打料中");
            }
            //MessageLog.GetInstance.Show(string.Format("当前模式为：{0}", CurrentMode));

        }

        private void ProcessModeUp(ICMSG_MODE_UP modeUpMsg)
        {
            MessageLog.GetInstance.Show(string.Format("模式返回为：{0}", modeUpMsg.Mode));
        }

        public void ProcessMsg(byte[] data)
        {
            lastRefreshTime = DateTime.Now;
            try
            {
                if (data.Length < 5)
                    return;
                switch (data[2])
                {
                    case (byte)IC_CMD.HEART:
                        var msg = IcPack.ByteToStructure<ICMSG_Heart_UP>(data.ToArray());
                        ProcessHeart(msg);
                        break;
                    case (byte)IC_CMD.MODE:
                        var modeUp = IcPack.ByteToStructure<ICMSG_MODE_UP>(data.ToArray());
                        ProcessModeUp(modeUp);
                        break;
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
