using Fiesta.Game.Networking.PacketTypes;
using Fiesta.Networking.Packets;
using Fiesta.Players.Networking;
using Fiesta.Worlds;
using Fiesta.Worlds.Networking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Villain.Networking.Packets;

namespace Login.Players.Networking.Packets.User.In
{
	public static class WorldSelect
	{
		[PacketHandler("Player", USER.WORLDSELECT_REQ)]
		public static void Handle(PlayerConnection client, InPacket packet)
		{
			byte worldID = packet.ReadByte();

			WorldConnection worldConnection = WorldManager.GetWorldConnection(worldID);

			if (worldConnection != null)
			{
				string guid = Guid.NewGuid().ToString().Replace("-", "");
				
				using (Worlds.Networking.Packets.Misc.Out.WorldTransfer worldTransferInformation = new Worlds.Networking.Packets.Misc.Out.WorldTransfer(guid, client.Account.ID))
				{
					worldConnection.Send(worldTransferInformation);
				}

				using (Misc.Out.WorldTransfer worldTransfer = new Misc.Out.WorldTransfer(worldConnection, guid))
				{
					client.Send(worldTransfer);
				}
			}
			else
			{
				client.Disconnect();
			}
		}
	}
}