using HighWayIot.Rfid.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Rfid
{
    public class RfidDataAnalyse
    {
        /// <summary>
        /// 02H时间段盘点发送
        /// </summary>
        /// <param name="millisecond">盘点时间毫秒数</param>
        /// <returns></returns>
        public byte[] Send02H(ushort millisecond)
        {
            byte[] bytes = BitConverter.GetBytes(millisecond);
            bytes = bytes.Reverse().ToArray();
            BaseSendDataEntity entity = new BaseSendDataEntity()
            {
                Code = 0x02,
                Data = bytes
            };

            byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity);

            return result;
        }

        /// <summary>
        /// 接受02H盘点数据 (默认EPC 4位)
        /// </summary>
        /// <param name="data">数据体</param>
        /// <returns></returns>
        public Receive02HEntity Receive02H(byte[] data)
        {
            Receive02HEntity entity = new Receive02HEntity();

            int index = 0;

            if (data.Length != 0)
            {
                //取读到多少个标签
                entity.TagCount = data[index];
                entity.Data = new List<Single02HReceive>();
                index++;
            }
            else
            {
                entity.TagCount = 0;
            }

            //取每一个读到的标签
            for (int i = 0; i < entity.TagCount; i++)
            {
                Single02HReceive EPCData = new Single02HReceive();

                //取单个标签读取的次数
                EPCData.Count = data[index];
                index++;

                //取信号强度
                EPCData.RSSI = data[index];
                index++;

                //取天线端口
                EPCData.Ant = data[index];
                index++;

                //取EPC区域
                EPCData.PC = new byte[2];
                Array.Copy(data, index, EPCData.PC, 0, 2);
                index += 2;

                //取读到标签的EPC
                EPCData.EPC = new byte[12];
                try
                {
                    Array.Copy(data, index, EPCData.EPC, 0, 12);
                }
                catch (Exception ex)
                {
                    Log4net.LogHelper.Instance.Error(ex.Message);
                    return null;
                }
                index += 12;

                entity.Data.Add(EPCData);
            }

            return entity;
        }

        /// <summary>
        /// 发送心跳配置包
        /// </summary>
        /// <returns></returns>
        public byte[] SendBFH(byte second)
        {
            byte[] data = new byte[3];
            data[0] = 0x00;
            data[1] = second;
            data[2] = 0x01;

            BaseSendDataEntity entity = new BaseSendDataEntity()
            {
                Code = 0xBF,
                Data = data
            };

            byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity);

            return result;
        }

        /// <summary>
        /// 接收心跳包
        /// </summary>
        /// <returns></returns>
        public uint ReceiveBFH(byte[] data)
        {
            uint result = BitConverter.ToUInt32(data, 0);
            return result;
        }

        /// <summary>
        /// 发送功率设置包
        /// </summary>
        /// <param name="power"></param>
        /// <returns></returns>
        public byte[] Send42H(int power)
        {
            return new byte[0];
        } 
    }
}
