/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-11-16
 * 时间: 10:52
 *
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */

using System.Text;
using Server.Data;
using Server.Core;
using GameData;

namespace Server.Pack
{
	/// <summary>
	/// 游戏包
	/// </summary>
	public class GamePack : Reference
	{
        
		/// <summary>
		/// 发包的玩家
		/// </summary>
		public PlayerDataAsyc pl;

        /// <summary>
        /// 包头
        /// </summary>
        public PackHead Head
        {
            get;
            private set;
        }

        private string m_JsonData;

        /// <summary>
        /// 包数据
        /// </summary>
        public string JsonData
        {
            get
            {
                if (m_JsonData == null && IsBinary && Data != null)
                {
                    m_JsonData = Encoding.UTF8.GetString(Data);
                }

                return m_JsonData;
            }
        }

        public byte[] Data
        {
            get;
            private set;
        }

        public bool IsText
        {
            get;
            private set;
        }

        public bool IsBinary
        {
            get;
            private set;
        }

        public static GamePack Create(PackHead head)
        {
            GamePack gamePack = ReferencePool.Fetch<GamePack>();
            gamePack.Head = head;
            gamePack.Data = null;
            gamePack.IsBinary = true;
            gamePack.IsText = false;
            return gamePack;
        }

        public static GamePack Create(PackHead head,byte[] data)
        {
            GamePack gamePack = ReferencePool.Fetch<GamePack>();
            gamePack.Head = head;
            gamePack.Data = data;
            gamePack.IsBinary = true;
            gamePack.IsText = false;
            return gamePack;
        }

        public static GamePack Create(PackHead head,string jsonData)
        {
            GamePack gamePack = ReferencePool.Fetch<GamePack>();
            gamePack.Head = head;
            gamePack.m_JsonData = jsonData;
            gamePack.IsText = true;
            gamePack.IsBinary = false;
            return gamePack;
        }

        public override void Dispose()
        {
            this.pl = null;
            this.m_JsonData = null;
            this.Head = null;
            this.Data = null;
            ReferencePool.Recycle(this);
        }
    }
}
