/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-11-15
 * 时间: 16:19
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Diagnostics;
using Server.Data;
using Server.MQ;
using Server.Pack;
using GameData;
using Server.Data.Module;
using Game.Player;
using Server.Core;
using Server.Net;
using Debug = MrWu.Debug.Debug;

namespace Server
{
	/// <summary>
	/// Description of GameNet.
	/// </summary>
	public static class GameNet
	{
        private static void InternalSendPack(int userid, PackHead head, byte[] data)
        {
            IUserSession session = GameSessionManager.Instance.FindSession(userid);
            if (session == null)
            {
                Debug.Warning($"发送失败，没找到玩家的链接:{userid}");
                return;
            }
            
            //Debug.Log($"发送数据包:{head.packlx}");
            session.SendPack(head,data);
        }

        private static void InternalSendPack(int userid,PackHead head,object data = null)
        {
            IUserSession session = GameSessionManager.Instance.FindSession(userid);
            if (session == null)
            {
                Debug.Warning($"发送失败，没找到玩家的链接:{userid}");
                return;
            }
            
            //Debug.Log($"发送数据包:{head.packlx}");
            session.SendPack(head,data);
        }

        private static void InternalSendPack(int userid, PackHead head, string jsonMessage)
        {
            IUserSession session = GameSessionManager.Instance.FindSession(userid);
            if (session == null)
            {
                Debug.Warning($"发送失败，没找到玩家的链接:{userid}");
                return;
            }
            
            session.SendPack(head,jsonMessage);
        }

        public static void SendPack(PlayerDataAsyc pl, PackHead head, byte[] data)
        {
            if(pl.userid < 0 || pl.outline == 1)	//玩家离线 或者是机器人
                return;
            
            head.userid = pl.userid;

            if (GameBase.instance.myUtil.Equals(pl.socketUtil))
            {
                //Debug.Info($"不是转发的!{data == null}");
                InternalSendPack(pl.userid,head,data);
                return;
            }
            Debug.Error($"怎么会有转发给玩家的情况:{head.packlx}");
        }

        /// <summary>
        /// 发送MessagePack 包裹
        /// </summary>
        /// <param name="pl"></param>
        /// <param name="head"></param>
        /// <param name="data"></param>
        public static void SendMessagePack(PlayerDataAsyc pl, PackHead head, object data)
        {
            if (pl.userid < 0 || pl.outline == 1 || pl.socketUtil == null) return;

            byte[] bts = MessagePackHelper.Serialize(data);
            InternalSendPack(pl.userid, head, bts);
        }
        
        /// <summary>
		/// 发包给玩家
		/// </summary>
		/// <param name="pl">玩家</param>
		/// <param name="head">包头</param>
		/// <param name="data">包数据</param>
		public static void SendPack(PlayerDataAsyc pl,PackHead head,object data = null,bool enableTrans = true){
			if(pl.userid < 0 || pl.outline == 1 || pl.socketUtil == null)	//玩家离线 或者是机器人
				return;
            
            //Debug.Log($"发送数据! {head.packlx}");
            head.userid = pl.userid;

            if (GameBase.instance.myUtil.Equals(pl.socketUtil))
            {
                //Debug.Info("不是转发的!");
                InternalSendPack(pl.userid,head,data);
                return;
            }

            if (!enableTrans)
            {
                return;
            }
            
            Debug.Error($"怎么会有转发给玩家的情况! {head.packlx} {pl.userid}");
			head.transfer = head.packlx;
			head.packlx = ServerPackAgreement.TransFerPack;
            if(pl.socketUtil != null)
				GlobalMQ.instance.DeliveryOne(pl.socketUtil,GlobalMQ.CreateMessage(head,data,FailTags.net_error,(int)ModuleType.SocketModule),true);
		}
		
		/// <summary>
		/// 发包给玩家
		/// </summary>
		/// <param name="pl">玩家</param>
		/// <param name="head">包头</param>
		/// <param name="jsonmessage">包字符串</param>
		public static void SendPack(PlayerDataAsyc pl,PackHead head,string jsonmessage){
			if(pl.userid < 0 || pl.outline == 1 || pl.socketUtil == null) {
                //if (pl.outline == 1)
                //    Debug.Warning("发包失败:玩家不在线:" + pl.userid);
                return;
            }
            head.userid = pl.userid;

            if (GameBase.instance.myUtil.Equals(pl.socketUtil))
            {
                //Debug.Info("不是转发的!");
                InternalSendPack(pl.userid,head,jsonmessage);
                return;
            }
            
            Debug.Error($"怎么还有转发的情况: {head.packlx}");
            
			head.transfer = head.packlx;
			head.packlx = ServerPackAgreement.TransFerPack;
			
            if (pl != null && pl.socketUtil != null) 
                GlobalMQ.instance.DeliveryOne(pl.socketUtil,GlobalMQ.CreateMessage(head, jsonmessage,FailTags.net_error_netRe,(int)ModuleType.SocketModule),true);
            else
                Debug.Warning("发包失败:玩家Socket不存在!!!");
		}
		
		/// <summary>
		/// 发包给玩家
		/// </summary>
		/// <param name="userid">userid</param>
		/// <param name="head">包头</param>
		/// <param name="jsonmessage">包字符</param>
		public static void SendPack_Userid(int userid,PackHead head,string jsonmessage){
			if(userid < 0)
				return;

            InternalSendPack(userid, head, jsonmessage);
		}
        
        /// <summary>
        /// 发包给玩家
        /// </summary>
        /// <param name="userid">userid</param>
        /// <param name="head">包头</param>
        public static void SendPack_Userid(int userid,PackHead head,object data){
            if(userid < 0)
                return;
            
            InternalSendPack(userid,head,data);
        }
	}
}
