using System.Data.Common;
using System.IO;
using ActorCore;
using MessagePack;
using Server.Core;

namespace NetWorkMessage
{
    /// <summary>
    /// 外部消息
    /// </summary>
    [Server.Core.Message(MessageOpcode.Client2ServerMessage)]
    [MessagePackObject]
    public class Client2ServerMessage : RouterMessage
    {
        // 客户端->Router 包长度 4 5 消息体
        /// <summary>
        /// 远端IP地址
        /// </summary>
        [Key(0)]
        public string RemoteIpAddress { get; set; }

        /// <summary>
        /// 包序列化
        /// </summary>
        [Key(1)]
        public int OpCode { get; set; }

        //
        [Key(2)]
        public byte ver { get; set; }

        /// <summary>
        /// 是否压缩
        /// </summary>
        [Key(3)]
        public byte Compress { get; set; }

        /// <summary>
        /// 源数据
        /// </summary>
        [Key(4)]
        public byte[] SourceData { get; set; }

        public static Client2ServerMessage Create(string remoteIpAddress,byte ver,int opcode,byte compress,byte[] bodyData)
        {
            Client2ServerMessage client2ServerMessage = ReferencePool.Fetch<Client2ServerMessage>();
            client2ServerMessage.RemoteIpAddress = remoteIpAddress;
            client2ServerMessage.ver = ver;
            client2ServerMessage.OpCode = opcode;
            client2ServerMessage.Compress = compress;
            client2ServerMessage.SourceData = bodyData;
            return client2ServerMessage;
        }

        protected override void VDispose()
        {
            this.RemoteIpAddress = null;
        }
    }

    [Server.Core.Message(MessageOpcode.Server2ClientMessage)]
    [MessagePackObject]
    public class Server2ClientMessage : RouterMessage
    {
        /// <summary>
        /// 压缩的缓存
        /// </summary>
        [IgnoreMember]
        public readonly MemoryStream CompressStream = new MemoryStream(2048);
        
        [IgnoreMember] public readonly MemoryStream MemoryStream = new MemoryStream(1024);
        
        /// <summary>
        /// 发送数据
        /// </summary>
        [Key(0)]
        public byte[] Data;

        public static Server2ClientMessage Create()
        {
            Server2ClientMessage message = ReferencePool.Fetch<Server2ClientMessage>();
            return message;
        }

        protected override void VDispose()
        {
            if (IsFromPool)
            {
                Data = null;
            }
            base.VDispose();
        }
    }

    /// <summary>
    /// 这个不用对象池，因为执行消息可能需要异步，但是没有等待
    /// </summary>
    [Server.Core.Message(MessageOpcode.ClientMessage)]
    public class ClientMessage : MessageObject, IMessage
    {
        /// <summary>
        /// 远端ip地址
        /// </summary>
        public string RemoteIpAddress;
        
        /// <summary>
        /// 消息ID
        /// </summary>
        public int OpCode;

        /// <summary>
        /// 消息包体
        /// </summary>
        public byte[] SourceData;

        public static ClientMessage Create(string remoteIpAddress,int opCode, byte[] data)
        {
            ClientMessage message = new ClientMessage();
            message.RemoteIpAddress = remoteIpAddress;
            message.OpCode = opCode;
            message.SourceData = data;
            return message;
        }

        protected override void VDispose()
        {
            if (IsFromPool)
            {
                this.RemoteIpAddress = null;
                this.SourceData = null;
            }
        }
    }
}