﻿using System;
using System.Text;
using galaxy.core.communication.packets.outgoing.gamecenter;
using galaxy.core.habbolib.gameclients;
using galaxy.core.habbolib.games;

namespace galaxy.core.communication.packets.incoming.gamecenter
{
    class JoinPlayerQueueEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            if (Session == null || Session.GetHabbo() == null)
                return;

            int GameId = Packet.PopInt();

            GameData GameData = null;
            if (GalaxyEnvironment.GetGame().GetGameDataManager().TryGetGame(GameId, out GameData))
            {
                string SSOTicket = "HABBOON-Fastfood-" + GenerateSSO(32) + "-" + Session.GetHabbo().Id;

                Session.SendPacket(new JoinQueueComposer(GameData.GameId));
                Session.SendPacket(new LoadGameComposer(GameData, SSOTicket));
            }
        }

        private string GenerateSSO(int length)
        {
            Random random = new Random();
            string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            StringBuilder result = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                result.Append(characters[random.Next(characters.Length)]);
            }
            return result.ToString();
        }
    }
}
