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

namespace Rdh.SocketServer.Client.Models
{
    public class ElectronicMedicineKitMarshalEndian
    {
        private readonly int ConstLength = 11;   //最小包长
        private List<byte> leftBytes = new List<byte>();  //历史遗留包数据

        public List<ElectronicMedicineKitBetweenAndRequestInfo> GetSendSockets(byte[] buffer, int len)
        {
            //拷贝当前有效数据
            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<ElectronicMedicineKitBetweenAndRequestInfo>();
            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 && bytes[headIndex + 1] == head2 && bytes[headIndex + 2] == head3)
                    //{
                    //    isFind = true;
                    //    break;
                    //}
                    if (bytes.Skip(headIndex).Take(ElectronicMedicineKitBetweenAndRequestInfo.Header.Length).SequenceEqual(ElectronicMedicineKitBetweenAndRequestInfo.Header))
                    {
                        isFind = true;
                        break;
                    }
                    headIndex++;
                    isFind = false;
                }

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

                //寻找foot1的索引
                var footIndex = headIndex;
                while (footIndex < bytes.Length)
                {
                    //if (bytes[footIndex] == foot1 && bytes[footIndex + 1] == foot2 && bytes[footIndex + 2] == foot3)
                    //{
                    //    isFind = true;
                    //    break;
                    //}
                    if (bytes.Skip(footIndex).Take(ElectronicMedicineKitBetweenAndRequestInfo.Footer.Length).SequenceEqual(ElectronicMedicineKitBetweenAndRequestInfo.Footer))
                    {
                        isFind = true;
                        break;
                    }
                    footIndex++;
                    isFind = false;
                }

                if (!isFind || (footIndex - headIndex + ElectronicMedicineKitBetweenAndRequestInfo.Footer.Length) < ConstLength)
                {
                    leftBytes.AddRange(bytes);
                    return sendSockets;
                }

                //提取一个完整包数据 并解析
                var packBuffer = bytes.Skip(headIndex).Take(footIndex + ElectronicMedicineKitBetweenAndRequestInfo.Footer.Length).ToArray();
                var domain = new ElectronicMedicineKitBetweenAndRequestInfo();
                domain.OnParsingBody(packBuffer.Skip(ElectronicMedicineKitBetweenAndRequestInfo.Header.Length).SkipLast(ElectronicMedicineKitBetweenAndRequestInfo.Footer.Length).ToArray());
                sendSockets.Add(domain);

                bytes = bytes.Skip(packBuffer.Length).ToArray();
            }

            return sendSockets;
        }
    }
}
