using System.Buffers.Binary;
using System.Runtime.InteropServices;
namespace System.Net.Sockets.Kcp
{
    /// 
    /// 调整了没存布局,直接拷贝块提升性能。
    /// 结构体保存内容只有一个指针,不用担心参数传递过程中的性能
    /// https://github.com/skywind3000/kcp/issues/118#issuecomment-338133930
    /// 不要对没有初始化的KcpSegment(内部指针为0,所有属性都将指向位置区域) 进行任何赋值操作,可能导致内存损坏。
    /// 出于性能考虑,没有对此项进行安全检查。
    /// 
    public struct KcpSegment : IKcpSegment
    {
        internal readonly unsafe byte* ptr;
        public unsafe KcpSegment(byte* intPtr, uint appendDateSize)
        {
            this.ptr = intPtr;
            len = appendDateSize;
        }
        /// 
        /// 使用完必须显示释放,否则内存泄漏
        /// 
        /// 
        /// 
        public static KcpSegment AllocHGlobal(int appendDateSize)
        {
            var total = LocalOffset + HeadOffset + appendDateSize;
            IntPtr intPtr = Marshal.AllocHGlobal(total);
            unsafe
            {
                ///清零    不知道是不是有更快的清0方法?
                Span span = new Span(intPtr.ToPointer(), total);
                span.Clear();
                return new KcpSegment((byte*)intPtr.ToPointer(), (uint)appendDateSize);
            }
        }
        /// 
        /// 释放非托管内存
        /// 
        /// 
        public static void FreeHGlobal(KcpSegment seg)
        {
            unsafe
            {
                Marshal.FreeHGlobal((IntPtr)seg.ptr);
            }
        }
        /// 以下为本机使用的参数
        /// 
        /// offset = 0
        /// 
        public uint resendts
        {
            get
            {
                unsafe
                {
                    return *(uint*)(ptr + 0);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(ptr + 0) = value;
                }
            }
        }
        /// 
        /// offset = 4
        /// 
        public uint rto
        {
            get
            {
                unsafe
                {
                    return *(uint*)(ptr + 4);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(ptr + 4) = value;
                }
            }
        }
        /// 
        /// offset = 8
        /// 
        public uint fastack
        {
            get
            {
                unsafe
                {
                    return *(uint*)(ptr + 8);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(ptr + 8) = value;
                }
            }
        }
        /// 
        /// offset = 12
        /// 
        public uint xmit
        {
            get
            {
                unsafe
                {
                    return *(uint*)(ptr + 12);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(ptr + 12) = value;
                }
            }
        }
        ///以下为需要网络传输的参数
        public const int LocalOffset = 4 * 4;
        public const int HeadOffset = KcpConst.IKCP_OVERHEAD;
        /// 
        /// offset = 
        /// 
        /// https://github.com/skywind3000/kcp/issues/134
        public uint conv
        {
            get
            {
                unsafe
                {
                    return *(uint*)(LocalOffset + 0 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(LocalOffset + 0 + ptr) = value;
                }
            }
        }
        /// 
        /// offset =  + 4
        /// 
        public byte cmd
        {
            get
            {
                unsafe
                {
                    return *(LocalOffset + 4 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(LocalOffset + 4 + ptr) = value;
                }
            }
        }
        /// 
        /// offset =  + 5
        /// 
        public byte frg
        {
            get
            {
                unsafe
                {
                    return *(LocalOffset + 5 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(LocalOffset + 5 + ptr) = value;
                }
            }
        }
        /// 
        /// offset =  + 6
        /// 
        public ushort wnd
        {
            get
            {
                unsafe
                {
                    return *(ushort*)(LocalOffset + 6 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(ushort*)(LocalOffset + 6 + ptr) = value;
                }
            }
        }
        /// 
        /// offset =  + 8
        /// 
        public uint ts
        {
            get
            {
                unsafe
                {
                    return *(uint*)(LocalOffset + 8 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(LocalOffset + 8 + ptr) = value;
                }
            }
        }
        /// 
        ///  SendNumber? 
        /// offset =  + 12
        /// 
        public uint sn
        {
            get
            {
                unsafe
                {
                    return *(uint*)(LocalOffset + 12 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(LocalOffset + 12 + ptr) = value;
                }
            }
        }
        /// 
        /// offset =  + 16
        /// 
        public uint una
        {
            get
            {
                unsafe
                {
                    return *(uint*)(LocalOffset + 16 + ptr);
                }
            }
            set
            {
                unsafe
                {
                    *(uint*)(LocalOffset + 16 + ptr) = value;
                }
            }
        }
        /// 
        ///  AppendDateSize 
        /// offset =  + 20
        /// 
        public uint len
        {
            get
            {
                unsafe
                {
                    return *(uint*)(LocalOffset + 20 + ptr);
                }
            }
            private set
            {
                unsafe
                {
                    *(uint*)(LocalOffset + 20 + ptr) = value;
                }
            }
        }
        /// 
        /// 
        /// 
        /// https://github.com/skywind3000/kcp/issues/35#issuecomment-263770736
        public Span data
        {
            get
            {
                unsafe
                {
                    return new Span(LocalOffset + HeadOffset + ptr, (int)len);
                }
            }
        }
        /// 
        /// 将片段中的要发送的数据拷贝到指定缓冲区
        /// 
        /// 
        /// 
        public int Encode(Span buffer)
        {
            var datelen = (int)(HeadOffset + len);
            ///备用偏移值 现阶段没有使用
            const int offset = 0;
            if (KcpConst.IsLittleEndian)
            {
                if (BitConverter.IsLittleEndian)
                {
                    ///小端可以一次拷贝
                    unsafe
                    {
                        ///要发送的数据从LocalOffset开始。
                        ///本结构体调整了要发送字段和单机使用字段的位置,让报头数据和数据连续,节约一次拷贝。
                        Span sendDate = new Span(ptr + LocalOffset, datelen);
                        sendDate.CopyTo(buffer);
                    }
                }
                else
                {
                    BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset), conv);
                    buffer[offset + 4] = cmd;
                    buffer[offset + 5] = frg;
                    BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(offset + 6), wnd);
                    BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 8), ts);
                    BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 12), sn);
                    BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 16), una);
                    BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 20), len);
                    data.CopyTo(buffer.Slice(HeadOffset));
                }
            }
            else
            {
                if (BitConverter.IsLittleEndian)
                {
                    BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset), conv);
                    buffer[offset + 4] = cmd;
                    buffer[offset + 5] = frg;
                    BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(offset + 6), wnd);
                    BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 8), ts);
                    BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 12), sn);
                    BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 16), una);
                    BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 20), len);
                    data.CopyTo(buffer.Slice(HeadOffset));
                }
                else
                {
                    ///大端可以一次拷贝
                    unsafe
                    {
                        ///要发送的数据从LocalOffset开始。
                        ///本结构体调整了要发送字段和单机使用字段的位置,让报头数据和数据连续,节约一次拷贝。
                        Span sendDate = new Span(ptr + LocalOffset, datelen);
                        sendDate.CopyTo(buffer);
                    }
                }
            }
            return datelen;
        }
    }
}