using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Protocol.Interfaces;

namespace JiShe.CollectBus.Protocol.T37612012.Appendix
{
    public class Appendix_A23 : IAnalysisStrategy<List<string>>
    {

        public async Task<bool> ExecuteAsync(List<string> data, Action<dynamic>? result = null)
        {
            decimal value = 0.000M;
            if (data[0].Check3761Byte())
                value += GetValue(0, 4, 1000M, 4, 4, 10000M, data[0], 1);
            if (data[1].Check3761Byte())
                value += GetValue(0, 4, 10M, 4, 4, 100M, data[1], 1);
            if (data[2].Check3761Byte())
                value += GetValue(0, 4, 10M, 4, 4, 1M, data[2], 0);
            result?.Invoke(value);
            return await Task.FromResult(true);
        }
        /// <summary>
        /// 解析值
        /// </summary>
        /// <param name="startIndex">二进制低位字符串截取位置</param>
        /// <param name="startLength">截取长度</param>
        /// <param name="startQuantile">分位值，1000M</param>
        /// <param name="endIndex">二进制高位字符串截取位置</param>
        /// <param name="endLength">截取长度</param>
        /// <param name="endQuantile">分位值，100M</param>
        /// <param name="data">数据</param>
        /// <param name="calType">计算类型,0:乘法，1：除法</param>
        /// <returns></returns>
        private decimal GetValue(int startIndex, int startLength, decimal startQuantile, int endIndex, int endLength, decimal endQuantile, string data, int calType = 0)
        {
            var value = 0M;
            string binString = data.HexTo4BinZero();
            switch (calType)
            {
                case 0: 
                    value = (binString.Substring(startIndex, startLength).BinToDec() * startQuantile) + (binString.Substring(endIndex, endLength).BinToDec() * endQuantile);
                    break;
                case 1:
                    value = (binString.Substring(startIndex, startLength).BinToDec() / startQuantile) + (binString.Substring(endIndex, endLength).BinToDec() / endQuantile);
                    break;
            }
            return value;
        }

   
    }
}
