// Copyright 2018 RED Software, LLC. All Rights Reserved.

using GameLogServer.Services;
using IgniteEngine;
using IgniteEngine.Networking;
using System.Net;

namespace GameLogServer.Handlers
{
    internal static class LogHandlers
	{
		internal static void NC_LOG_USER_LOGINFAIL(NetworkMessage message, NetworkConnection connection)
		{
			var userId = message.ReadString(256);
			var userPassword = message.ReadString(32);
			var userIp = new IPAddress(message.ReadBytes(4)).ToString();
			var errorCode = message.ReadUInt16();

			AccountLogService.AddLoginFail(userId, userPassword, userIp, errorCode);
		}

		internal static void NC_LOG_USER_LOGIN(NetworkMessage message, NetworkConnection connection)
		{
			var userNo = (int) message.ReadUInt32();
			var worldNo = message.ReadByte();
			var userIp = new IPAddress(message.ReadBytes(4)).ToString();

			AccountLogService.AddLogin(userNo, worldNo, userIp);
		}

		internal static void NC_LOG_USER_LOGOUT(NetworkMessage message, NetworkConnection connection)
		{
			var userNo = (int) message.ReadUInt32();
			var worldNo = message.ReadByte();
			var playMin = message.ReadUInt16();
			var errorCode = message.ReadUInt16();

			AccountLogService.AddLogout(userNo, worldNo, playMin, errorCode);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_0(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();
			var nBigint1 = message.ReadInt64();

			GameLogService.AddDataType0(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetCharNo, nTargetID, nItemKey,
				nInt1, nInt2, nInt3, nBigint1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_1(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();

			GameLogService.AddDataType1(nType, nCharNo, sMap, nMapX, nMapY, nMapZ);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_2(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataType2(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_3(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nInt1 = message.ReadInt32();

			GameLogService.AddDataType3(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetCharNo, nTargetID, nInt1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_4(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();

			GameLogService.AddDataType4(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nInt1, nInt2, nInt3);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_5(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataType5(nType, nCharNo, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_6(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nInt1 = message.ReadInt32();
			var nBigint1 = message.ReadInt64();

			GameLogService.AddDataType6(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nInt1, nBigint1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_7(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataType7(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetCharNo, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_8(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nInt1 = message.ReadInt32();

			GameLogService.AddDataType8(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nInt1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_9(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataType9(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nItemKey, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_A(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();

			GameLogService.AddDataTypeA(nType, nCharNo, nTargetCharNo, nTargetID, nItemKey, nInt1, nInt2, nInt3);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_B(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();

			GameLogService.AddDataTypeB(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetCharNo, nTargetID, nItemKey,
				nInt1, nInt2, nInt3);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_C(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();

			GameLogService.AddDataTypeC(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetID, nItemKey, nInt1, nInt2,
				nInt3);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_D(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataTypeD(nType, nCharNo, nItemKey, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_E(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataTypeE(nType, sMap, nMapX, nMapY, nMapZ, nTargetID, nItemKey, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_F(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();
			var nBigint1 = message.ReadInt64();

			GameLogService.AddDataTypeF(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetID, nItemKey, nInt1, nInt2,
				nInt3, nBigint1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_G(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nBigint1 = message.ReadInt64();

			GameLogService.AddDataTypeG(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetID, nItemKey, nInt1, nInt2,
				nBigint1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_H(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();

			GameLogService.AddDataTypeH(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetCharNo);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_I(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();

			GameLogService.AddDataTypeI(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetCharNo, nTargetID);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_J(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();

			GameLogService.AddDataTypeJ(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nTargetID, nItemKey, nInt1, nInt2);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_K(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nBigint1 = message.ReadInt64();

			GameLogService.AddDataTypeK(nType, nCharNo, sMap, nMapX, nMapY, nMapZ, nInt1, nInt2, nBigint1);
		}

		internal static void NC_LOG_GAME_DATA_TYPE_L(NetworkMessage message, NetworkConnection connection)
		{
			var nType = (GameLogType) message.ReadInt32();
			var nCharNo = message.ReadInt32();
			var sMap = message.ReadString(12);
			var nMapX = message.ReadInt32();
			var nMapY = message.ReadInt32();
			var nMapZ = message.ReadInt32();
			var nTargetCharNo = message.ReadInt32();
			var nTargetID = message.ReadInt32();
			var nItemKey = message.ReadInt64();
			var nInt1 = message.ReadInt32();
			var nInt2 = message.ReadInt32();
			var nInt3 = message.ReadInt32();
			var nBigint1 = message.ReadInt64();

			GameLogService.AddDataTypeL(nType, nCharNo, nTargetCharNo, nTargetID, nItemKey, nInt1, nInt2, nInt3,
				nBigint1);
		}

		public static void NC_LOG_GAME_CREATE_AVATAR(NetworkMessage message, NetworkConnection connection)
		{
			var charNo = message.ReadInt32();

			GameLogService.AddCreateAvatar(charNo);
		}

		public static void NC_LOG_GAME_DELETE_AVATAR(NetworkMessage message, NetworkConnection connection)
		{
			var charNo = message.ReadInt32();

			GameLogService.AddDeleteAvatar(charNo);
		}
	}
}