using IceCoffee.Common.Extensions;
using System.Reflection;
namespace langguanApi.Model
{
    /// 
    /// CP指令
    /// 
    public class CpCommand
    {
        public ResponseCode ExeRtn { get; set; }
        /// 
        /// 数据时间信息
        /// 
        public DateTime DataTime { get; set; }
        /// 
        /// 污染物信息
        /// 
        public List PollutantInfo { get; set; }
        /// 
        /// 解析
        /// 
        /// 
        /// 
        public static CpCommand Parse(string cp)
        {
            var cpCommand = new CpCommand();
            cpCommand.PollutantInfo = new List();
            cpCommand.DataTime = DateTime.ParseExact(cp.GetMidStr("DataTime=", ";"), "yyyyMMddHHmmss", null);
            cp = cp.Substring(24);
            foreach (string project in cp.Split(';', StringSplitOptions.RemoveEmptyEntries))
            {
                var pollutantInfo = new PollutantInfo();
                string[] classes = project.Split(',');
                foreach (string @class in classes)
                {
                    string[] keyValue = @class.Split('=');
                    string key = keyValue[0];
                    string value = keyValue[1];
                    string[] factorCodeType = key.Split('-');
                    string factorCode = factorCodeType[0];
                    string type = factorCodeType[1];
                    pollutantInfo.FactorCode = (FactorCode)Enum.Parse(typeof(FactorCode), factorCode);
                    switch (type)
                    {
                        case nameof(Model.PollutantInfo.Rtd):
                            if (string.IsNullOrEmpty(value) == false && decimal.TryParse(value, out decimal rtd))
                            {
                                pollutantInfo.Rtd = rtd;
                            }
                            break;
                        case nameof(Model.PollutantInfo.Avg):
                            if (string.IsNullOrEmpty(value) == false && decimal.TryParse(value, out decimal avg))
                            {
                                pollutantInfo.Avg = avg;
                            }
                            break;
                        case nameof(Model.PollutantInfo.Max):
                            if (string.IsNullOrEmpty(value) == false && decimal.TryParse(value, out decimal max))
                            {
                                pollutantInfo.Max = max;
                            }
                            break;
                        case nameof(Model.PollutantInfo.Min):
                            if (string.IsNullOrEmpty(value) == false && decimal.TryParse(value, out decimal min))
                            {
                                pollutantInfo.Min = min;
                            }
                            break;
                        case nameof(Model.PollutantInfo.Cou):
                            if (string.IsNullOrEmpty(value) == false && decimal.TryParse(value, out decimal cou))
                            {
                                pollutantInfo.Cou = cou;
                            }
                            break;
                        case nameof(Model.PollutantInfo.Flag):
                            pollutantInfo.Flag = (InstrumentationDataFlag)Enum.Parse(typeof(InstrumentationDataFlag), value);
                            break;
                        default:
                            throw new Exception("无效的CP指令字段名");
                    }
                }
                cpCommand.PollutantInfo.Add(pollutantInfo);
            }
            return cpCommand;
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine($"Error in CpCommand.Parse:{ex.Message}");
            //    throw new Exception("Error in CpCommand.Parse", ex);
            //}
        }
        /// 
        /// 序列化
        /// 
        /// 
        public string Serialize()
        {
            return "ExeRtn=" + (int)ExeRtn;
        }
    }
}