using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Device
{
    public class DeviceStatus
    {
        //字典值
        public Dictionary<string, string> keyValues = new Dictionary<string, string>()
        {
             {"PLC","煮面机机器人" },
             {"Lebai","乐白机器人" },
             {"SCChip","单片机" },
             {"DRCoffee","咖博士咖啡机器" },
             {"KLMCoffee","伽乐美咖啡机器" },
             {"GSIceCream","乐白机器人" },
             {"IsConnected","连接状态" },
             {"IsWork","工作状态" },

             {"CompletedOpen_SE_1","舵机1打开" },
             {"CompletedOpen_SE_2","舵机2打开" },
             {"CompletedOpen_SE_3","舵机3打开" },
             {"CompletedClose_SE_1","舵机1关闭" },
             {"CompletedClose_SE_2","舵机2关闭" },
             {"CompletedClose_SE_3","舵机3关闭" },

            {"False","异常" },
            {"True","正常" },

            {"Status","状态" },
            {"AppStatus","应用状态" },
            {"Warning","告警" },
            {"Fault","故障" },
            {"drinkType","饮品类型" },
            {"taskIndex","任务序号" },
            {"Keep","维修保护" },
            {"progress","工作进度" }


        };

        public ConcurrentDictionary<string, object> status = new ConcurrentDictionary<string, object>();

        public void Update(string key, object value)
        {
            status.AddOrUpdate(key, value, (key, value) => value);
        }

        public Dictionary<string, object> GetStatus()
        {
            return status.ToDictionary(x => x.Key, x => x.Value);
        }

        public object GetIOTStatus()
        {
            List<DevStatus> StatusALL = new List<DevStatus>();
            foreach (var item in status)
            {
                string sta = string.Empty;
                string name = item.Key;
                string Ms = string.Empty;
                string id = string.Empty;
                if (item.Key.Contains("."))
                {
                    sta = item.Key.Split('.')[0];
                    string sop = item.Key.Split('.')[item.Key.Split('.').Length - 1]; name = sop;
                    string value = item.Value?.ToString();
                    if (keyValues.ContainsKey(sta)) sta = keyValues[sta];
                    if (keyValues.ContainsKey(sop)) sop = keyValues[sop];
                    if (keyValues.ContainsKey(value)) value = keyValues[value];

                    Ms = $"[{sta}]-[{sop}]-[{value}]";
                    id = $"[{sta}]-[{sop}]";
                }
                StatusALL.Add(new DevStatus { id = id, Name = name, type = sta, Status = item.Value.ToString(), Ms = Ms });
            }
            StatusALL = StatusALL?.OrderBy(x => x.type).ToList();
            return new { data = StatusALL };
        }

        public List<DevStatus> GetStatusT()
        {
            List<DevStatus> StatusALL = new List<DevStatus>();
            foreach (var item in status)
            {
                string name = item.Key;
                string Ms = string.Empty;
                string id = string.Empty;
                string sta = string.Empty;
                if (item.Key.Contains("."))
                {
                    sta = item.Key.Split('.')[0];
                    string sop = item.Key.Split('.')[item.Key.Split('.').Length - 1]; name = sop;
                    string value = item.Value?.ToString();
                    if (keyValues.ContainsKey(sta)) sta = keyValues[sta];
                    if (keyValues.ContainsKey(sop)) sop = keyValues[sop];
                    if (keyValues.ContainsKey(value)) value = keyValues[value];

                    Ms = $"[{sta}]-[{sop}]-[{value}]";
                    id = $"[{sta}]-[{sop}]";
                }
                StatusALL.Add(new DevStatus { id = id, Name = name, type = sta, Status = item.Value.ToString(), Ms = Ms });
            }
            StatusALL = StatusALL?.OrderBy(x => x.type).ToList();
            return StatusALL;
        }
    }

    public class DevStatus
    {
        public string type { get; set; }
        public string id { get; set; }
        public string Name { get; set; }
        private string _status { get; set; }
        public string Status
        {
            get { return _status; }
            set
            {
                _status = value;
                if (_status == "False" || _status == "True")
                {
                    if (_status == "True")
                        StatusColor = new { r = 51, g = 232, b = 34, a = 1 };
                    else
                        StatusColor = new { r = 255, g = 0, b = 0, a = 1 };
                }
            }
        }
        public string Ms { get; set; }
        public object StatusColor { get; set; }
        public DevStatus()
        {
            StatusColor = new { r = 51, g = 232, b = 34, a = 1 };
        }
    }
}
