using HslCommunication.Enthernet.Redis;
using HslCommunication.MQTT;
using MWS.Library;
using MWS.Library.Common;
using MWS.Model.Dto;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace MWS.Domain.Tasks
{
    public class Common
    {

        public static Hashtable InitTagsAD(RedisClient redisClient, List<TagDto> tags)
        {
            Hashtable hashtable = new Hashtable();
            var operateResult = redisClient.ReadHashKey("Tags", tags.Select(c => c.Id.ToString()).ToArray());
            if (operateResult.IsSuccess)
            {
                TagDto tag;
                JToken token;
                for (int i = 0; i < tags.Count; i++)
                {
                    tag = tags[i];
                    if (operateResult.Content[i] != null)
                    {
                        token = JObject.Parse(operateResult.Content[i])["AD"];
                        hashtable.Add(tag.Id, GetObjectValue(tag.SourceDataType, token));
                    }
                    else
                    {
                        hashtable.Add(tag.Id, null);
                    }
                }
            }
            return hashtable;
        }
        private static object GetObjectValue(short dataType, JToken token)
        {
            switch (dataType)
            {
                case 1:
                    return token.ToObject<bool>();
                case 2:
                    return token.ToObject<byte>();
                case 3:
                    return token.ToObject<ushort>();
                case 4:
                    return token.ToObject<short>();
                case 5:
                    return token.ToObject<uint>();
                case 6:
                    return token.ToObject<int>();
                case 7:
                    return token.ToObject<float>();
                default:
                    return 0;
            }
        }
 
    }
}
