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


using Plus.Communication.Packets.Outgoing.Moderation;
using Plus.HabboHotel.Items;
using Plus.Communication.Packets.Outgoing.Notifications;
using Plus.Database.Interfaces;
using System.Data;


namespace Plus.HabboHotel.Rooms.Chat.Commands.Moderator
{
    class FurnitureCommand : IChatCommand
    {
        public string PermissionRequired
        {
            get { return "command_furniture"; }
        }

        public string Parameters
        {
            get { return "(Type) (montant)"; }
        }


        public string Description
        {
            get { return "Correction des mobiliers."; }
        }
        public void Execute(GameClients.GameClient Session, Rooms.Room Room, string[] Params)
        {
            RoomUser RUser = Room.GetRoomUserManager().GetRoomUserByHabbo(Session.GetHabbo().Id);
            List<Item> Items = Room.GetGameMap().GetRoomItemForSquare(RUser.X, RUser.Y);
            if (Params.Length == 1 || Params[1] == "help")
            {
                StringBuilder Lista = new StringBuilder();
                Lista.Append("Avertissement : Cette commande sert à modifier vos mobis depuis la table furniture.): \r\r");
                Lista.Append("1) :item width nombre (Edit item witdh) \r");
                Lista.Append("2) :item length number (Edit item length) \r");
                Lista.Append("3) :item height number (Edit item height) \r");
                Lista.Append("4) :item cansit oui/non (Allow / Disallow Sit on the Item) \r");
                Lista.Append("5) :item canwalk oui/non (Allow / Disallow Walk on the Item) \r");
                Lista.Append("6) :item canstack oui/non (Allow / Disallow Stack on the Item) \r");
                Lista.Append("7) :item market oui/non (Allow / Disallow Market place sell) \r");
                Lista.Append("8) :item interaction nom (Set the Item interaction) \r");
                Lista.Append("9) :item interactioncount nombre (Set the amount of interactions of the Item) \r\r");
                Lista.Append("Info : unload votre appart une fois que ton mobis est modifier.");
                Session.SendMessage(new MOTDNotificationComposer(Lista.ToString()));
                return;
            }
            String Type = Params[1].ToLower();
            int numeroint = 0, FurnitureID = 0;
            double numerodouble = 0;
            DataRow Item = null;
            String opcion = "";
            switch (Type)
            {
                case "width":
                    {
                        try
                        {
                            numeroint = Convert.ToInt32(Params[2]);
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `width` = @NumeroInt WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("NumeroInt", numeroint);
                                    dbClient.AddParameter("id",FurnitureID );
                                    dbClient.RunQuery(); //Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item width: " + FurnitureID + " mise à jour (Nouveau width: " + numeroint.ToString() + ")");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer un nombre valide)");
                        }
                    }
                    break;
                case "length":
                    {
                        try
                        {
                            numeroint = Convert.ToInt32(Params[2]);
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `length` = @NumeroInt WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("NumeroInt", numeroint);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item length: " + FurnitureID + " mise à jour (Nouveau length: " + numeroint.ToString() + ")");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer un nombre valide)");
                        }
                    }
                    break;
                case "height":
                    {
                        try
                        {
                            numerodouble = Convert.ToDouble(Params[2]);
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `stack_height` = @NumeroInt WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("NumeroInt", numeroint);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item height: " + FurnitureID + " mise à jour (Nouveau height: " + numerodouble.ToString() + ")");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer un nombre valide)");
                        }
                    }
                    break;
                case "interactioncount":
                    {
                        try
                        {
                            numeroint = Convert.ToInt32(Params[2]);
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `interaction_modes_count` = @NumeroInt WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("NumeroInt", numeroint);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Interaction count: " + FurnitureID + " mise à jour (Nouvelle interaction count: " + numeroint.ToString() + ")");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer un nombre valide)");
                        }
                    }
                    break;
                case "cansit":
                    {
                        try
                        {
                            opcion = Params[2].ToLower();
                            if (!opcion.Equals("oui") && !opcion.Equals("non"))
                            {
                                Session.SendWhisper("Utilisé : oui ou non.)");
                                return;
                            }
                            if (opcion.Equals("oui"))
                                opcion = "1";
                            else if (opcion.Equals("non"))
                                opcion = "0";
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `can_sit` = @Option WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("Option", opcion);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item can_sit: " + FurnitureID + " mise à jour");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer une valeur)");
                        }
                    }
                    break;
                case "canstack":
                    {
                        try
                        {
                            opcion = Params[2].ToLower();
                            if (!opcion.Equals("oui") && !opcion.Equals("non"))
                            {
                                Session.SendWhisper("Utiliser : oui ou non");
                                return;
                            }
                            if (opcion.Equals("oui"))
                                opcion = "1";
                            else if (opcion.Equals("non"))
                                opcion = "0";
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `can_stack` = @Option WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("Option", opcion);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item can_stack: " + FurnitureID + " mise à jour");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer une valeur)");
                        }
                    }
                    break;
                case "canwalk":
                    {
                        try
                        {
                            opcion = Params[2].ToLower();
                            if (!opcion.Equals("oui") && !opcion.Equals("non"))
                            {
                                Session.SendWhisper("Utiliser : oui ou non");
                                return;
                            }
                            if (opcion.Equals("oui"))
                                opcion = "1";
                            else if (opcion.Equals("non"))
                                opcion = "0";
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `is_walkable` = @Option WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("Option", opcion);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item can_walk: " + FurnitureID + " mise à jour");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer une valeur)");
                        }
                    }
                    break;
                case "market":
                    {
                        try
                        {
                            opcion = Params[2].ToLower();
                            if (!opcion.Equals("oui") && !opcion.Equals("non"))
                            {
                                Session.SendWhisper("Utiliser : oui ou non");
                                return;
                            }
                            if (opcion.Equals("oui"))
                                opcion = "1";
                            else if (opcion.Equals("non"))
                                opcion = "0";
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `is_rare` = @Option WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("Option", opcion);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Market place option: " + FurnitureID + " mise à jour");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer une valeur)");
                        }
                    }
                    break;
                case "interaction":
                    {
                        try
                        {
                            opcion = Params[2].ToLower();
                            foreach (Item IItem in Items.ToList())
                            {
                                if (IItem == null)
                                    continue;
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("SELECT base_item FROM items WHERE id = '" + IItem.Id + "' LIMIT 1");
                                    Item = dbClient.getRow();
                                    if (Item == null)
                                        continue;
                                    FurnitureID = Convert.ToInt32(Item[0]);
                                    dbClient.SetQuery("UPDATE `furniture` SET `interaction_type` = @Option WHERE `id` = @id LIMIT 1");
                                    dbClient.AddParameter("Option", opcion);
                                    dbClient.AddParameter("id", FurnitureID);
                                    dbClient.RunQuery();//Correction Requête by Dodone
                                }
                                Session.SendWhisper("Item interaction: " + FurnitureID + " mise à jour. (Nouvelle interaction: " + opcion + ")");
                            }
                            PlusEnvironment.GetGame().GetItemManager().Init();
                        }
                        catch (Exception)
                        {
                            Session.SendNotification("Oops! Quelque chose ne va pas! (Entrer une valeur)");
                        }
                    }
                    break;
                default:
                    {
                        Session.SendNotification("Oops! Quelque chose ne va pas! Pour vous aider, taper :item help");
                        return;
                    }
            }
        }
    }
}