/*
 * This file is part of PV-Star for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


package com.jcwhatever.pvs.commands.admin.arena;

import com.jcwhatever.nucleus.commands.CommandInfo;
import com.jcwhatever.nucleus.commands.arguments.CommandArguments;
import com.jcwhatever.nucleus.commands.exceptions.CommandException;
import com.jcwhatever.nucleus.commands.response.CommandRequests;
import com.jcwhatever.nucleus.commands.response.ResponseType;
import com.jcwhatever.nucleus.managed.language.Localizable;
import com.jcwhatever.pvs.Lang;
import com.jcwhatever.pvs.api.PVStarAPI;
import com.jcwhatever.pvs.api.arena.Arena;
import com.jcwhatever.pvs.api.commands.AbstractPVCommand;
import com.jcwhatever.nucleus.utils.observer.result.FutureSubscriber;
import com.jcwhatever.nucleus.utils.observer.result.Result;

import org.bukkit.command.CommandSender;

@CommandInfo(
        parent="arena",
        command="del",
        staticParams={"arenaName"},
        description="Delete the specified arena.",

        paramDescriptions = {
                "arenaName= The name of the arena to delete."})

public class DelSubCommand extends AbstractPVCommand {

    @Localizable static final String _DISABLE_FIRST =
            "You must disable the arena before it can be deleted.";

    @Localizable static final String _CONFIRM =
            "{WHITE}Please type '{YELLOW}/confirm{WHITE}' to delete arena '{0: arena name}'.";

    @Localizable static final String _SUCCESS =
            "Arena '{0: arena name}' deleted.";

    @Override
    public void execute(final CommandSender sender, CommandArguments args) throws CommandException {

        String arenaName = args.getString("arenaName");

        final Arena arena = getArena(sender, arenaName);
        if (arena == null)
            return; // finish

        if (arena.getSettings().isEnabled()) {
            tellError(sender, Lang.get(_DISABLE_FIRST));
            return; // finish
        }

        tell(sender, Lang.get(_CONFIRM, arena.getName()));

        // get confirmation
        CommandRequests.request(PVStarAPI.getPlugin(), "delete_" + arena.getName(), sender, ResponseType.CONFIRM)
                .onSuccess(new FutureSubscriber<ResponseType>() {
                    @Override
                    public void on(Result<ResponseType> result) {

                        PVStarAPI.getArenaManager().removeArena(arena.getId());

                        Arena selectedArena = PVStarAPI.getArenaManager().getSelectedArena(sender);

                        if (selectedArena != null && selectedArena.equals(arena)) {
                            PVStarAPI.getArenaManager().setSelectedArena(sender, null);
                        }

                        tellSuccess(sender, Lang.get(_SUCCESS, arena.getName()));
                    }
                });
    }
}



@@ -1493,31 +1493,98 @@
 esponse.
-Command
+IRequestContext;%0Aimport com.jcwhatever.nucleus.commands.response.Response
 Request
-s
+or
 ;%0Aimport
@@ -1699,16 +1699,86 @@
 izable;%0A
+import com.jcwhatever.nucleus.utils.observer.update.UpdateSubscriber;%0A
 import c
@@ -1946,138 +1946,8 @@
 and;
-%0Aimport com.jcwhatever.nucleus.utils.observer.result.FutureSubscriber;%0Aimport com.jcwhatever.nucleus.utils.observer.result.Result;
 %0A%0Aim
@@ -3155,59 +3155,40 @@
     
-// get confirmation%0A        CommandRequests.request
+ResponseRequestor.contextBuilder
 (PVS
@@ -3205,18 +3205,36 @@
 Plugin()
-, 
+)%0A            .name(
 %22delete_
@@ -3256,76 +3256,138 @@
 me()
-, sender, ResponseType.CONFIRM)%0A                .onSuccess(new Futur
+)%0A            .timeout(15)%0A            .response(ResponseType.CONFIRM)%0A            .build(sender)%0A            .onRespond(new Updat
 eSub
@@ -3398,30 +3398,29 @@
 ber%3C
+I
 Re
-sponseType
+questContext
 %3E() %7B%0A
-    
     
@@ -3457,20 +3457,16 @@
         
-    
 public v
@@ -3476,44 +3476,146 @@
  on(
+I
 Re
-sult%3CResponseType%3E result) %7B%0A%0A    
+questContext context) %7B%0A%0A                    if (!ResponseType.CONFIRM.equals(context.getResponse()))%0A                        return;%0A%0A
     
@@ -3707,20 +3707,16 @@
         
- 
-   
 Arena se
@@ -3800,28 +3800,24 @@
             
-    
 if (selected
@@ -3860,28 +3860,24 @@
 s(arena)) %7B%0A
-    
             
@@ -3964,35 +3964,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -4065,21 +4065,32 @@
         
+%7D%0A
     
-%7D
+        %7D)
 %0A       
@@ -4094,21 +4094,29 @@
         
-    %7D
+.sendRequest(
 );%0A    %7D

refactor command response requests