using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

using Plus.HabboHotel.Rooms;
using Plus.HabboHotel.Groups;
using Plus.HabboHotel.Items.Wired;

using Plus.Communication.Packets.Outgoing.Rooms.Engine;
using Plus.Communication.Packets.Outgoing.Rooms.Chat;
using Plus.Communication.Packets.Outgoing.Users;
using Plus.Communication.Packets.Outgoing.Navigator;
using Plus.Communication.Packets.Outgoing.Inventory.Purse;
using Plus.HabboHotel.Rooms.AI.Speech;
using Plus.HabboHotel.Rooms.AI;
using System.Data;
using Plus.Communication.Packets.Outgoing.Inventory.Furni;
using Plus.Communication.Packets.Outgoing;
using Plus.Communication.Packets.Outgoing.Rooms.Nux;
using Plus.Communication.Packets.Outgoing.Notifications;

namespace Plus.Communication.Packets.Incoming.Rooms.Engine
{
    class GetRoomEntryDataEvent : IPacketEvent
    {
        public void Parse(HabboHotel.GameClients.GameClient Session, ClientPacket Packet)
        {
            if (Session == null || Session.GetHabbo() == null)
                return;

            Room Room = Session.GetHabbo().CurrentRoom;
            if (Room == null)
                return;

            if (Session.GetHabbo().InRoom)
            {
                Room OldRoom;

                if (!PlusEnvironment.GetGame().GetRoomManager().TryGetRoom(Session.GetHabbo().CurrentRoomId, out OldRoom))
                    return;

                if (OldRoom.GetRoomUserManager() != null)
                    OldRoom.GetRoomUserManager().RemoveUserFromRoom(Session, false, false);
            }

            if (!Room.GetRoomUserManager().AddAvatarToRoom(Session))
            {
                Room.GetRoomUserManager().RemoveUserFromRoom(Session, false, false);
                return;
            }

            Room.SendObjects(Session);


            try
            {
                if (Session.GetHabbo().GetMessenger() != null)
                    Session.GetHabbo().GetMessenger().OnStatusChanged(true);
            }
            catch { }

            if (Session.GetHabbo().GetStats().QuestID > 0)
                PlusEnvironment.GetGame().GetQuestManager().QuestReminder(Session, Session.GetHabbo().GetStats().QuestID);

            Session.SendMessage(new RoomEntryInfoComposer(Room.RoomId, Room.CheckRights(Session, true)));
            Session.SendMessage(new RoomVisualizationSettingsComposer(Room.WallThickness, Room.FloorThickness, PlusEnvironment.EnumToBool(Room.Hidewall.ToString())));

            RoomUser ThisUser = Room.GetRoomUserManager().GetRoomUserByHabbo(Session.GetHabbo().Username);

            if (ThisUser != null && Session.GetHabbo().PetId == 0)
                Room.SendMessage(new UserChangeComposer(ThisUser, false));

            Session.SendMessage(new RoomEventComposer(Room.RoomData, Room.RoomData.Promotion));
            Session.GetHabbo().GetInventoryComponent().UpdateItems(false);

            if (Room.GetWired() != null)
                Room.GetWired().TriggerEvent(WiredBoxType.TriggerRoomEnter, Session.GetHabbo());

            if (PlusEnvironment.GetUnixTimestamp() < Session.GetHabbo().FloodTime && Session.GetHabbo().FloodTime != 0)
                Session.SendMessage(new FloodControlComposer((int)Session.GetHabbo().FloodTime - (int)PlusEnvironment.GetUnixTimestamp()));
            // i think is good idea? huh
            if (Room.OwnerId == Session.GetHabbo().Id)
            {
                Session.GetHabbo().CurrentRoom.GetRoomItemHandler().Setz(0);
                DataRow dFrank = null;
                using (var dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                {
                    dbClient.SetQuery("SELECT bot_frank FROM users WHERE id = '" + Session.GetHabbo().Id + "'");
                    dFrank = dbClient.getRow();
                }

                if (Convert.ToBoolean(dFrank["bot_frank"]) == false)
                {
                    using (var dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                    {
                        dbClient.RunQuery("UPDATE users SET bot_frank = 'true' WHERE id = " + Session.GetHabbo().Id + ";");
                    }
                    string Chat1 = PlusEnvironment.GetDBConfig().DBData["frank.chat.1"];
                    string Chat2 = PlusEnvironment.GetDBConfig().DBData["frank.chat.2"];
                    string Chat3 = PlusEnvironment.GetDBConfig().DBData["frank.chat.3"];
                    string Chat4 = PlusEnvironment.GetDBConfig().DBData["frank.chat.4"];
                    string Chat5 = PlusEnvironment.GetDBConfig().DBData["frank.chat.5"];
                    string Credits = PlusEnvironment.GetDBConfig().DBData["frank.give.credits"];
                    string Diamonds = PlusEnvironment.GetDBConfig().DBData["frank.give.diamonds"];
                    string Duckets = PlusEnvironment.GetDBConfig().DBData["frank.give.duckets"];
                    string Gotws = PlusEnvironment.GetDBConfig().DBData["frank.give.gotws"];
                    string Furni = PlusEnvironment.GetDBConfig().DBData["frank.give.furni"];

                    List<RandomSpeech> BotSpeechList = new List<RandomSpeech>();
                    int X = 0;
                    int Y = 0;
                    string hola = "false";
                    RoomUser BotUser = Room.GetRoomUserManager().DeployBot(new RoomBot(0, Session.GetHabbo().CurrentRoomId, "generic", "freeroam", "Frank", "Manageur de l'hôtel", "hr-3194-38-36.hd-180-1.ch-220-1408.lg-285-73.sh-906-90.ha-3129-73.fa-1206-73.cc-3039-73", X, Y, 0, 4, 0, 0, 0, 0, ref BotSpeechList, "", 0, 0, false, 0, Convert.ToBoolean(hola), 1), null);
                    System.Threading.Thread.Sleep(5000);
                    BotUser.Chat(Chat1, false, 0);
                    System.Threading.Thread.Sleep(8000);
                    BotUser.Chat(Chat2, false, 0);
                    System.Threading.Thread.Sleep(8000);
                    if (!string.IsNullOrWhiteSpace(Credits))
                    {
                        BotUser.Chat("Dans ma poche j'ai " + Credits + " Crédits, " + Diamonds + " Diamants et euh .. " + Duckets + " Duckets! Tu as de la chance! Voilà pour toi.", false, 0);
                        Session.GetHabbo().Credits = Session.GetHabbo().Credits + Convert.ToInt32(Credits);
                        Session.GetHabbo().Diamonds = Session.GetHabbo().Diamonds + Convert.ToInt32(Diamonds);
                        Session.GetHabbo().Duckets = Session.GetHabbo().Duckets + Convert.ToInt32(Duckets);
                        Session.SendMessage(new CreditBalanceComposer(Session.GetHabbo().Credits));
                        Session.SendMessage(new HabboActivityPointNotificationComposer(Session.GetHabbo().Duckets, Convert.ToInt32(Duckets)));
                        Session.SendMessage(new HabboActivityPointNotificationComposer(Session.GetHabbo().Diamonds, Convert.ToInt32(Diamonds), 5));
                        Session.SendMessage(new ActivityPointsComposer(Session.GetHabbo().Duckets, Session.GetHabbo().Diamonds, Session.GetHabbo().GOTWPoints));

                    }
                    else
                    {
                        BotUser.Chat("Aujourd'hui, je n'ai rien à vous donner!", false, 0);
                    }
                    if (!string.IsNullOrWhiteSpace(Chat4) || !string.IsNullOrEmpty(Furni))
                    {
                        System.Threading.Thread.Sleep(8000);
                        BotUser.Chat(Chat4, false, 0);
                        DataRow dFurni = null;
                        using (var dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                        {
                            DataTable Table = null;
                            dbClient.SetQuery("SELECT * FROM catalog_items WHERE page_id = '9214' ORDER BY RAND() LIMIT 1");
                            Table = dbClient.getTable();
                            foreach (DataRow dRow in Table.Rows)
                            {
                                dbClient.SetQuery("SELECT public_name FROM furniture WHERE id = '" + Convert.ToInt32(dRow["item_id"]) + "'");
                                dFurni = dbClient.getRow();
                                Session.GetHabbo().GetInventoryComponent().AddNewItem(0, Convert.ToInt32(dRow["item_id"]), Convert.ToString(dFurni["public_name"]), 1, true, false, 0, 0);
                            }
                        }
                        Session.GetHabbo().GetInventoryComponent().UpdateItems(false);
                    }
                    System.Threading.Thread.Sleep(8000);
                    BotUser.Chat(Chat5, false, 0);
                    Room.GetGameMap().RemoveUserFromMap(BotUser, new System.Drawing.Point(0, 0));
                    Room.GetRoomUserManager().RemoveBot(BotUser.VirtualId, false);
                } else
                {
                    Session.GetHabbo().GetInventoryComponent().UpdateItems(false);
                    Session.SendMessage(new FurniListUpdateComposer());
                }
                if (Convert.ToBoolean(dFrank["bot_frank"]) == true)
                {
                    Session.SendMessage(new FurniListUpdateComposer());
                    Session.GetHabbo().GetInventoryComponent().UpdateItems(false);
                }
                if (Session.GetHabbo()._NUX)
                {
                    var nuxStatus = new ServerPacket(ServerPacketHeader.NuxUserStatus);
                    nuxStatus.WriteInteger(2);
                    Session.SendMessage(nuxStatus);
                    Session.SendMessage(new EventNotificationComposer("nux/lobbyoffer/hide"));
                    var habbo = Session.GetHabbo();
                    if (!habbo.PassedNuxNavigator && !habbo.PassedNuxCatalog && !habbo.PassedNuxItems && !habbo.PassedNuxMMenu && !habbo.PassedNuxChat && !habbo.PassedNuxCredits && !habbo.PassedNuxDuckets)
                    {
                        Session.SendMessage(new NuxAlertComposer("helpBubble/add/BOTTOM_BAR_NAVIGATOR/nux.bot.info.navigator.1"));
                        habbo.PassedNuxNavigator = true;
                    }
                }
            }
        }
    }
}