/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-04-02
 * 时间: 21:11
 *
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
#if Server
using System;
using System.Text;
using Newtonsoft.Json;
using Server.Core;

namespace GameData {
    /// <summary>
    /// 
    /// </summary>
    //[ProtoBuf.ProtoContract]
    public class HeadWrapper : IReference {

        [JsonIgnore]
        public bool IsFromPool
        {
            get;
            set;
        }

        [JsonIgnore]
        public long ReferenceId
        {
            get;
            set;
        }
        
        public static HeadWrapper Create(PackHead head,string jsonMessage)
        {
            HeadWrapper wrapper = ReferencePool.Fetch<HeadWrapper>();
            wrapper.head = head;
            wrapper.jsonMessage = jsonMessage;
            wrapper.IsText = true;
            wrapper.IsBinary = false;
            return wrapper;
        }

        public static HeadWrapper Create(PackHead head, byte[] data)
        {
            HeadWrapper wrapper = ReferencePool.Fetch<HeadWrapper>();
            wrapper.head = head;
            wrapper.Data = data;
            wrapper.IsBinary = true;
            wrapper.IsText = false;
            return wrapper;
        }
        
        /// <summary>
        /// 
        /// </summary>
        public HeadWrapper() {

        }
        
        public void Dispose()
        {
            head = null;
            jsonMessage = null;
            Data = null;
            
            ReferencePool.Recycle(this);
        }
        
        /// <summary>
        /// 
        /// </summary>
        public PackHead head
        {
            get;
            set;
        }
        
        [JsonIgnore]
        public bool IsText
        {
            get;
            set;
        }

        [JsonIgnore]
        public bool IsBinary
        {
            get;
            private set;
        }

        [JsonIgnore]
        public string Data2Text
        {
            get
            {
                return Encoding.UTF8.GetString(Data);
            }
        }
        
        [JsonIgnore]
        public byte[] Data
        {
            get;
            private set;
        }
        
        /// <summary>
        /// 
        /// </summary>
        public string jsonMessage
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            return string.Format("head: {0}, jsonMesage: {1}", head, jsonMessage);
        }
    }
}
#endif
