#if COMBAT_SERVER

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Net.Sockets.Kcp;
using System.Threading;
using Com.Fort23.Protocol.Protobuf;
using Fort23.UTool;

namespace Core.KCPTool
{
    public class KCPServerConnection : IKcpCallback, IServerConnection
    {
        // private Thread _udpClientThread;


        private byte[] buffer = new byte[2048];

        private KCPServer kcpServer;

        // public Queue<CombatSynchronizeRequest> CombatSynchronizeRequests { get; set; }
        public bool isConnected { get; set; }
        public int connectionID { get; set; }
        public long playerId { get; set; }

        private long lasetSendTime;

        public KCPServerConnection(KCPServer kcpServer, IPEndPoint endPoint)
        {
            EndPoint = endPoint;
            this.kcpServer = kcpServer;
            _kcp = new SimpleSegManager.Kcp(0, this);
            kcp.NoDelay(1, 40, 2, 0);
            isConnected = true;
        }


        public SimpleSegManager.Kcp kcp
        {
            get { return _kcp; }
        }

        public IPEndPoint EndPoint { get; set; }

        private SimpleSegManager.Kcp _kcp;

        public void Output(IMemoryOwner<byte> buffer, int avalidLength)
        {
            if (EndPoint == null || kcpServer == null)
            {
                LogTool.Log("发送错误，链接没有了：");
                return;
            }

            var s = buffer.Memory.Span.Slice(0, avalidLength).ToArray();
            // LogTool.Log("发送：" + EndPoint.Address + "__" + EndPoint.Port);
            kcpServer.SocketSendTo(s, EndPoint);
            buffer.Dispose();
        }

        public int SendData(byte[] data, int dataSize)
        {
            if (_kcp == null)
            {
                return -255;
            }

            int c = kcp.Send(data.AsSpan().Slice(0, dataSize));
            return c;
        }

        public void Update()
        {
            if (_kcp == null)
            {
                return;
            }

            kcp.Update(DateTimeOffset.UtcNow);
        }

        public void ReceiveAsync()
        {
            if (kcp == null)
            {
                return;
            }

            var (buffer, avalidLength) = kcp.TryRecv();
            if (avalidLength <= 0)
            {
                return;
            }

            var s = buffer.Memory.Span.Slice(0, avalidLength).ToArray();
            if (s == null)
            {
                return;
            }

            SendDataType sendDataType = (SendDataType)s[0];
            switch (sendDataType)
            {
                // case SendDataType.ShakeHands:
                //     byte[] playerData = new byte[8];
                //     playerData[0] = s[1];
                //     playerData[1] = s[2];
                //     playerData[2] = s[3];
                //     playerData[3] = s[4];
                //     playerData[4] = s[5];
                //     playerData[5] = s[6];
                //     playerData[6] = s[7];
                //     playerData[7] = s[8];
                //     long playerId = SocketTool.ByteToInt(playerData);
                //     this.playerId = playerId;
                //     LogTool.Log("收到握手包"+playerId);
                //     kcpServer.AddServerConnection(playerId,0, this);
                //    
                //     break;
                case SendDataType.Data:
                    byte[] protoBuffer = new byte[s.Length - 1];
                    Array.Copy(s, 1, protoBuffer, 0, protoBuffer.Length);
                    CombatSynchronizeRequest combatSynchronizeRequest =
                        CombatSynchronizeRequest.Parser.ParseFrom(protoBuffer);
                    if (combatSynchronizeRequest == null)
                    {
                        LogTool.Error("错误的实例化战斗" + s.Length);
                    }
                    else
                    {
                        // LogTool.Error("收到客户端消息" + combatSynchronizeRequest.CombatSynchronizeType);
                        kcpServer.AddCombatSynchronizeRequest(combatSynchronizeRequest);
                    }

                    break;
                case SendDataType.GetGameFrameByte:
                    int gameFrame = 0;
                    byte[] fdata = new byte[4];
                    Array.Copy(s, 9, fdata, 0, fdata.Length);
                    gameFrame = SocketTool.ByteToInt(fdata);

                    kcpServer.SendGameFrame(gameFrame, this);
                    break;
            }
        }

        public int Input(byte[] data)
        {
            int ok = kcp.Input(data);
            return ok;
        }


        public void Dispose()
        {
            // _udpClientThread = null;
            kcp?.Dispose();
            _kcp = null;
            kcpServer = null;
        }
    }
}
#endif