using System;
using System.Collections.Generic;
using System.Linq;

namespace Rdh.SocketServer.Client.Models
{
    public class RfidMarshalEndian
    {
        private readonly int ConstLength = 15;   //最小包长
        private List<byte> leftBytes = new List<byte>();  //历史遗留包数据
        private readonly byte head1 = 0xA5;  //自定义头1


        public byte[] Encode(RfidFixedHeaderRequestInfo msg)
        {
            var bytes = new List<byte>();
            bytes.AddRange(msg.Header);
            bytes.AddRange(msg.Body);
            return bytes.ToArray();
        }

        public List<RfidFixedHeaderRequestInfo> GetSendSockets(byte[] buffer, int len)
        {
            if (buffer == null) return new List<RfidFixedHeaderRequestInfo>();
            //拷贝当前有效数据
            var bytes = new byte[len];
            Buffer.BlockCopy(buffer, 0, bytes, 0, len);
            //合并上个包的遗留数据
            if (leftBytes.Count > 0)
            {
                leftBytes.AddRange(bytes);
                bytes = leftBytes.ToArray();
                leftBytes.Clear();
                leftBytes = new List<byte>();
            }

            var sendSockets = new List<RfidFixedHeaderRequestInfo>();

            while (bytes.Length > 0 || leftBytes.Count > 0)
            {
                if (bytes.Length < ConstLength)
                {
                    leftBytes.AddRange(bytes);
                    return sendSockets;
                }

                //寻找head1的索引
                var isFind = false;
                var headIndex = 0;
                while (headIndex < bytes.Length)
                {
                    if (bytes[headIndex] == head1)
                    {
                        isFind = true;
                        break;
                    }
                    headIndex++;
                    isFind = false;
                }

                if (!isFind)
                {
                    leftBytes.AddRange(bytes);
                    return sendSockets;
                }

                if (headIndex + 1 == bytes.Length)
                {
                    leftBytes.AddRange(bytes);
                    return sendSockets;
                }

                //寻找foot1的索引
                var lenTmp = bytes[headIndex + 1];
                if (bytes.Length < lenTmp + 2)
                {
                    leftBytes.AddRange(bytes);
                    return sendSockets;
                }

                //提取一个完整包数据 并解析
                var packBuffer = bytes.Skip(headIndex).Take(lenTmp + 2).ToArray();
                //if (Check(packBuffer))
                {
                    //将byte数组转为数据实体
                    var sendSocket = new RfidFixedHeaderRequestInfo();
                    sendSocket.OnParsingHeader(packBuffer.Take(5).ToArray());
                    sendSocket.OnParsingBody(packBuffer.Skip(5).ToArray());
                    sendSockets.Add(sendSocket);

                    //留存剩余的包数据
                    bytes = bytes.Skip(headIndex + lenTmp + 2).ToArray();
                }
            }

            return sendSockets;
        }

        public bool Check(byte[] buffer)
        {
            var sum = buffer.SkipLast(1).Select(x => (int)x).Sum();
            var bytes = Convert.ToString(sum, 2)
                .Select(x => byte.Parse(x.ToString()))
                .Skip(1)
                .Select(x => x == 1 ? 0 : 1);
            var res = Convert.ToInt32(String.Join("", bytes), 2);
            return buffer[buffer.Length - 1] == res + 1;
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool flag)
        {
            if (flag)
            {
                IDisposable disposable = this.leftBytes as IDisposable;
                if (disposable != null) { disposable.Dispose(); }
            }
        }

    }
}
