using Fiesta.Databases.Account.StoredProcedures;
using Fiesta.Game.Networking.PacketTypes;
using Fiesta.Networking.Packets;
using Fiesta.Players;
using Fiesta.Players.Networking;
using Login.Players.Networking.Packets.User.Out;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Villain.Networking.Packets;

namespace Login.Players.Networking.Packets.User.In
{
	public static class Login
	{
#if IS_US_BUILD
		[PacketHandler("Player", USER.US_LOGIN_REQ)]
		public static void Handle(PlayerConnection client, InPacket packet)
		{
			string username = packet.ReadString(260);
			string password = packet.ReadString(32);

			usp_User_loginGame_US authentication = new usp_User_loginGame_US(username, password);

			if (authentication.UserNo > 0)
			{
				if (authentication.Block == 0)
				{
					if (authentication.IsLoginable > 0)
					{
						client.Account = new Account((UInt32)authentication.UserNo);

						using (WorldList worldList = new WorldList(false))
						{
							client.Send(worldList);
						}
					}
					else
					{
						using (LoginError loginError = new LoginError(LoginError.Error.WorldServersDownForMaintenance))
						{
							client.Send(loginError);
						}
					}
				}
				else
				{
					using (LoginError loginError = new LoginError(LoginError.Error.Blocked))
					{
						client.Send(loginError);
					}
				}
			}
			else
			{
				using (LoginError loginError = new LoginError(LoginError.Error.InvalidIDOrPassword))
				{
					client.Send(loginError);
				}
			}
		}
#endif

#if IS_TW_BUILD
		[PacketHandler("Player", USER.TW_LOGIN_REQ)]
		public static void Handle(PlayerConnection client, InPacket packet)
		{
			string username = packet.ReadString(36);
			string serial = packet.ReadString(36);
			string uid = packet.ReadString(20);
			string sid = packet.ReadString(8);

			Console.WriteLine("TW LOGIN - INFO:");
			Console.WriteLine($"DATA: {BitConverter.ToString(((MemoryStream)packet.BinaryReader.BaseStream).ToArray())}");
			Console.WriteLine($"Username: {username}");
			Console.WriteLine($"Serial: {serial}");
			Console.WriteLine($"UID: {uid}");
			Console.WriteLine($"SID: {sid}");

			username = "test";
			serial = "test";

			usp_User_loginGame_US authentication = new usp_User_loginGame_US(username, serial);

			if (authentication.UserNo > 0)
			{
				if (authentication.Block == 0)
				{
					if (authentication.IsLoginable > 0)
					{
						client.Account = new Account((UInt32)authentication.UserNo, username);

						using (WorldList worldList = new WorldList(false))
						{
							client.Send(worldList);
						}
					}
					else
					{
						using (LoginError loginError = new LoginError(LoginError.Error.WorldServersDownForMaintenance))
						{
							client.Send(loginError);
						}
					}
				}
				else
				{
					using (LoginError loginError = new LoginError(LoginError.Error.Blocked))
					{
						client.Send(loginError);
					}
				}
			}
			else
			{
				using (LoginError loginError = new LoginError(LoginError.Error.InvalidIDOrPassword))
				{
					client.Send(loginError);
				}
			}
		}
#endif
	}
}