using Microsoft.Extensions.Logging;
using Serilog.Core;
using SlnMesnac.Config;
using SlnMesnac.Model.AirportApiEntity;
using SlnMesnac.TouchSocket.Entity;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Xml;
using TouchSocket.Sockets;

namespace SlnMesnac.TouchSocket
{

    public class BufferDataAnalysis
    {
        /// <summary>
        /// 拆包接收数据
        /// </summary>
        /// <param name="bytes">TCP接受的原始数据</param>
        /// <returns></returns>
        public static TcpVisionEntity BufferRootAnalysis(byte[] bytes)
        {
            TcpVisionEntity entity;
            //一帧正常的数据最少12位
            if (bytes.Length < 12)
            {
                return null;
            }
            //数据校验，从起始符开始到数据位，按字节求和得出的结果对256求余
            int checkDatalength = bytes.Length - 2;
            byte checksum = bytes[checkDatalength];
            byte[] checkData = new byte[checkDatalength];
            Array.Copy(bytes, 0, checkData, 0, checkDatalength);
            int sum = 0;
            foreach (byte b in checkData)
            {
                sum += b;
            }
            int count = sum % 256;
            if (count == checksum)
            {
                entity = new TcpVisionEntity();
                entity.Checksum = checksum;
            }
            else
            {
                return null;
            }
            int index = 2;

            //取序列号
            entity.SN = new byte[2] { bytes[index], bytes[index + 1] };
            index += 2;

            //取时间戳
            entity.Timestamp = new byte[4] { bytes[index], bytes[index + 1], bytes[index + 2], bytes[index + 3] };
            index += 4;

            //取命令字
            entity.Command = bytes[index];
            index += 1;

            //取数据位长度
            entity.DataLength = bytes[index];
            index += 1;

            //取数据位
            byte[] dataBytes = new byte[entity.DataLength];
            Array.Copy(bytes, index, dataBytes, 0, entity.DataLength);
            entity.DataBytes = dataBytes;
            index += entity.DataLength;
            return entity;
        }

        /// <summary>
        /// 装包发送数据
        /// </summary>
        /// <param name="SN">序列号</param>
        /// <param name="Command">命令字</param>
        /// <param name="data">数据体</param>
        /// <returns></returns>
        public static byte[] DataCombine(byte[] SN, byte Command, byte[] data)
        {
            byte[] message = new byte[12 + data.Length];
            int index = 0;
            //head
            message[index++] = 0x55;
            message[index++] = 0xAA;

            //SN
            message[index++] = SN[0];
            message[index++] = SN[1];

            //timestamp
            // 获取当前时间的Unix时间戳，表示自1970-01-01以来的秒数
            int unixTimestamp = (int)(DateTimeOffset.UtcNow.ToUnixTimeSeconds());
            // 将时间戳转换为字节数组
            byte[] timestampBytes = BitConverter.GetBytes(unixTimestamp);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(timestampBytes);
            }
            message[index++] = timestampBytes[0];
            message[index++] = timestampBytes[1];
            message[index++] = timestampBytes[2];
            message[index++] = timestampBytes[3];

            //command
            message[index++] = Command;

            //datalength
            message[index++] = (byte)data.Length;

            foreach (byte b in data) message[index++] = b;

            //CRC
            int checkDatalength = message.Length - 2;
            byte[] checkData = new byte[checkDatalength];
            Array.Copy(message, 0, checkData, 0, checkDatalength);
            int sum = 0;
            foreach (byte b in checkData) sum += b;
            int count = sum % 256;
            message[index++] = (byte)count;
            
            //end
            message[index++] = 0xEE;

            return message;
        }

        public static byte[] GetCurrentUnixTimestampAsBytes()
        {
            // 获取当前的Unix时间戳，表示自1970-01-01以来的秒数
            int unixTimestamp = (int)(DateTimeOffset.UtcNow.ToUnixTimeSeconds());
            // 将时间戳转换为字节数组
            byte[] bytes = BitConverter.GetBytes(unixTimestamp);
            return bytes;
        }

    }
}
