using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.KLMCoffee.Protocal
{
    public class SystemStatusModel :BitValue
    {
        /// <summary>
        /// 系统状态
        /// </summary>
        public K95SysTemStatus temStatus { get; set; }

        /// <summary>
        /// 饮品类型
        /// </summary>
        public DrinkType drinkType { get; set; }

        /// <summary>
        /// 制作状态
        /// </summary>
        public MakeStatus makeStatus { get; set; }

        /// <summary>
        /// 当前任务
        /// </summary>
        public TaskIndex taskIndex { get; set; }

        /// <summary>
        /// 当前进度 10%
        /// </summary>
        public int progress { get; set; }

        /// <summary>
        ///  故障信息
        /// </summary>
        public FaultMessage faultMessage { get; set; }

        /// <summary>
        /// 保养信息
        /// </summary>
        public UpkeepMessage upkeepMessage { get; set; }

        public SystemStatusModel(string cmd)
        {
            // 去掉字符串首尾处的空格
            if (string.IsNullOrEmpty(cmd) || cmd.Length < 32)
                return;
            string str = cmd.Trim().Replace(@"\","").Replace("r","").Replace(@"R","").Replace(@"n","").Replace(@"N","");
            int length = str.Length;
            str = str.Substring(5,length - 7);
            try
            {
                string systemStatus = str.Substring(0,4);//系统状态
                string makeSta = str.Substring(4,4);//制作状态
                string makeProgress = str.Substring(8,4);//制作进度
                string makeProgress1 = str.Substring(12,4);//制作进度
                byte ErrorH = (byte)Convert.ToInt32(str.Substring(16,2),16);//故障信息
                byte ErrorL = (byte)Convert.ToInt32(str.Substring(18,2),16);//故障信息
                byte InFormation = (byte)Convert.ToInt32(str.Substring(20,1),16);//保养信息

                System.Array values = System.Enum.GetValues(typeof(K95SysTemStatus));
                string[] nameList = System.Enum.GetNames(typeof(K95SysTemStatus));

                string[] sds = Enum.GetNames(typeof(K95SysTemStatus));

                foreach (var item in Enum.GetNames(typeof(K95SysTemStatus)))
                {
                    if (systemStatus.Substring(0,1) == ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)).GetString())
                        temStatus = ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item));
                }

                foreach (var item in Enum.GetNames(typeof(DrinkType)))
                {
                    if ("0" + makeSta.Substring(0,1) == ((DrinkType)Enum.Parse(typeof(DrinkType),item)).GetString())
                        drinkType = ((DrinkType)Enum.Parse(typeof(DrinkType),item));
                }

                foreach (var item in Enum.GetNames(typeof(MakeStatus)))
                {
                    if (makeSta.Substring(1,1) == ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)).GetString())
                        makeStatus = ((MakeStatus)Enum.Parse(typeof(MakeStatus),item));
                }

                foreach (var item in Enum.GetNames(typeof(TaskIndex)))
                {
                    if (makeSta.Substring(2,1) == ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)).GetString())
                        taskIndex = ((TaskIndex)Enum.Parse(typeof(TaskIndex),item));
                }

                if (makeProgress1 != "0000")
                    progress = Convert.ToInt32(makeProgress,16) / Convert.ToInt32(makeProgress1,16);
                faultMessage = new FaultMessage(ErrorL,ErrorH);
                upkeepMessage = new UpkeepMessage(InFormation);
            }
            catch (Exception ex)
            {

            }

            
        }
    }
}
