using Plus.Communication.Packets.Outgoing.Rooms.Notifications;
using Plus.Database.Interfaces;
using Plus.HabboHotel.GameClients;

namespace Plus.HabboHotel.Rooms.Chat.Commands.Administrator
{
    class PointStaffCommand : IChatCommand
    {
        public string PermissionRequired
        {
            get { return "command_pointstaff"; }
        }

        public string Parameters
        {
            get { return "%pseudo% %type% %montant%"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public void Execute(GameClients.GameClient Session, Rooms.Room Room, string[] Params)
        {
            if (Params.Length == 1)
            {
                Session.SendWhisper("Veuillez saisir un type de paramètres correct! (plus, moins).");
                return;
            }

            GameClient Target = PlusEnvironment.GetGame().GetClientManager().GetClientByUsername(Params[1]);
            if (Target == null)
            {
                Session.SendWhisper("Oups, nous n'avons pas pu trouver cet utilisateur!");
                return;
            }

            string UpdateVal = Params[2];
            switch (UpdateVal.ToLower())
            {
                case "plus":
                case "+":
                    {
                        int Amount;
                        if (int.TryParse(Params[3], out Amount))
                        {
                            if (Target.GetHabbo().Rank > Session.GetHabbo().Rank)
                            {
                                Session.SendMessage(new BubbleNotificationComposer("Votre rang n'est pas assez haut.", "notif"));
                                break;
                            }
                            if (Target.GetHabbo().Rank >= 5)
                            {
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("UPDATE `users` SET `points_staff` = points_staff + @montant WHERE id = @id LIMIT 1;");
                                    dbClient.AddParameter("montant", Amount);
                                    dbClient.AddParameter("id", Target.GetHabbo().Id);
                                    dbClient.RunQuery();
                                }
                                if (Target.GetHabbo().Id != Session.GetHabbo().Id)
                                    Session.SendMessage(new BubbleNotificationComposer("Ajouter avec succès.", "notif"));
                                break;
                            }

                        }
                        else
                        {
                            Session.SendWhisper("Montant invalide");
                            break;
                        }
                        return;

                    }

                case "moins":
                case "-":
                    {
                        int Amount;
                        if (int.TryParse(Params[3], out Amount))
                        {
                            if (Target.GetHabbo().Rank > Session.GetHabbo().Rank)
                            {
                                Session.SendMessage(new BubbleNotificationComposer("Votre rang n'est pas assez haut.", "notif"));
                                break;
                            }
                            if (Target.GetHabbo().Rank >= 5)
                            {
                                using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
                                {
                                    dbClient.SetQuery("UPDATE `users` SET `points_staff` = points_staff - @montant WHERE id = @id LIMIT 1;");
                                    dbClient.AddParameter("montant", Amount);
                                    dbClient.AddParameter("id", Target.GetHabbo().Id);
                                    dbClient.RunQuery();
                                }
                                if (Target.GetHabbo().Id != Session.GetHabbo().Id)
                                    Session.SendMessage(new BubbleNotificationComposer("Retirer avec succès.", "notif"));
                                return;
                            }

                        }
                        else
                        {
                            Session.SendWhisper("Montant invalide");
                            return;
                        }
                        return;
                    }
                default:
                    Session.SendWhisper("'" + UpdateVal + "' n'est pas une valeur correct!");
                    break;
            }
        }
    }
}