using System;
using System.Text;
using GameData;
using MrWu.Debug;
using Server.Data;

namespace Server
{
    public static class PlayerDataAsycExtension
    {
        public static void SendPack2Client(this PlayerDataAsyc self,PackHead head,byte[] data,bool native,ref string jsonMessage)
        {
            if (self == null)
            {
                Debug.Error("发送给空的玩家!");
                return;
            }

            if (self.isRobot)
            {
                return;
            }

            //Debug.Info($"发送包数据,客户端版本:{self.ClientVer}");
            if (self.ClientVer > 46)
            {
                //新的客户端
                GameNet.SendPack(self,head,data);
            }
            else
            {
                SendPack2ClientV1_Old(self, head, data, native, ref jsonMessage);
            }
        }

        /// <summary>
        /// 过时的版本，给客户端版本小于等于46的玩家发送数据
        /// </summary>
        private static void SendPack2ClientV1_Old(this PlayerDataAsyc self,PackHead head,byte[] data,bool native,ref string jsonMessage)
        {
            if (jsonMessage == null)
            {
                if (native)
                {
                    //原生的
                    jsonMessage = Convert.ToBase64String(data);
                }
                else
                {
                    jsonMessage = Encoding.UTF8.GetString(data);
                }
            }
            
            GameNet.SendPack(self,head,jsonMessage);
        }
    }
}