package de.iani.cubequest.commands;

import de.iani.cubequest.CubeQuest;
import de.iani.cubequest.QuestManager;
import de.iani.cubequest.exceptions.QuestDeletionFailedException;
import de.iani.cubequest.quests.Quest;
import de.iani.cubequest.util.ChatAndTextUtil;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;


public class DeleteQuestCommand extends SubCommand {
    
    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias,
            String commandString, ArgsParser args) {
        
        if (!args.hasNext()) {
            Quest quest = CubeQuest.getInstance().getQuestEditor().getEditingQuest(sender);
            if (quest != null) {
                Bukkit.dispatchCommand(sender, "cubequest delete " + quest.getId());
                return true;
            }
            ChatAndTextUtil.sendWarningMessage(sender, "Bitte gib eine Quest an.");
            return true;
        }
        
        // String questIdString = args.seeNext("");
        // String questString = args.getAll("");
        // Quest quest;
        // try {
        // int id = Integer.parseInt(questIdString);
        // quest = QuestManager.getInstance().getQuest(id);
        // if (quest == null) {
        // // ChatAndTextUtil.sendWarningMessage(sender,
        // // "Es gibt keine Quest mit der ID " + id + ".");
        // // return true;
        // throw new NumberFormatException();
        // }
        // } catch (NumberFormatException e) {
        // Set<Quest> quests = QuestManager.getInstance().getQuests(questString);
        // if (quests.isEmpty()) {
        // ChatAndTextUtil.sendWarningMessage(sender,
        // "Es gibt keine Quest mit dem Namen " + questString + ".");
        // return true;
        // } else if (quests.size() > 1) {
        // ChatAndTextUtil.sendWarningMessage(sender,
        // "Es gibt mehrere Quests mit diesem Namen, bitte wähle eine aus:");
        // for (Quest q: quests) {
        // if (sender instanceof Player) {
        // HoverEvent he = new HoverEvent(HoverEvent.Action.SHOW_TEXT,
        // new ComponentBuilder("Info zu Quest " + q.getId()).create());
        // ClickEvent ce = new ClickEvent(ClickEvent.Action.RUN_COMMAND,
        // "/cubequest questInfo " + q.getId());
        // String msg = CubeQuest.PLUGIN_TAG + " " + ChatColor.GOLD + q.getTypeName()
        // + " " + q.getId();
        // ComponentBuilder cb =
        // new ComponentBuilder("").append(msg).event(ce).event(he);
        // ((Player) sender).spigot().sendMessage(cb.create());
        // } else {
        // ChatAndTextUtil.sendWarningMessage(sender,
        // QuestType.getQuestType(q.getClass()) + " " + q.getId());
        // }
        // }
        // return true;
        // }
        // quest = Iterables.getFirst(quests, null);
        // }
        
        String questString = args.seeAll("");
        Quest quest = ChatAndTextUtil.getQuest(sender, args, "/cubequest questInfo ", "",
                "Info zu Quest ", "");
        
        if (quest == null) {
            return true;
        }
        
        if (!questString.equals(quest.getId() + " DELETE")) {
            Bukkit.dispatchCommand(sender, "cubequest info " + quest.getId());
            ChatAndTextUtil.sendWarningMessage(sender, "Soll die Quest " + quest.getId()
                    + " wirklich unwiderruflich gelöscht werden? Dann nutze den Befehl /cubequest delete "
                    + quest.getId() + " DELETE");
            return true;
        }
        
        try {
            QuestManager.getInstance().deleteQuest(quest);
        } catch (QuestDeletionFailedException e) {
            ChatAndTextUtil.sendErrorMessage(sender,
                    "Quest konnte nicht gelöscht werden. Fehlermeldung:");
            ChatAndTextUtil.sendErrorMessage(sender, ChatAndTextUtil.exceptionToString(e));
            return true;
        }
        
        ChatAndTextUtil.sendNormalMessage(sender, "Quest " + quest + " gelöscht.");
        return true;
    }
    
    @Override
    public String getRequiredPermission() {
        return CubeQuest.EDIT_QUESTS_PERMISSION;
    }
    
    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias,
            ArgsParser args) {
        return Collections.emptyList();
    }
    
    @Override
    public String getUsage() {
        return "<Quest> [DELETE]";
    }
    
}

@@ -315,24 +315,57 @@
 util.List;%0D%0A
+import java.util.logging.Level;%0D%0A
 import org.b
@@ -3886,37 +3886,39 @@
 AndTextUtil.send
-Error
+Warning
 Message(sender,%0D
@@ -4030,13 +4030,15 @@
 send
-Error
+Warning
 Mess
@@ -4053,46 +4053,636 @@
 er, 
-ChatAndTextUtil.exceptionToS
+e.getLocalizedMessage());%0D%0A            %0D%0A            Throwable reason = e;%0D%0A            while (reason instanceof QuestDeletionFailedException) %7B%0D%0A                if (reason == reason.getCause()) %7B%0D%0A                    break;%0D%0A                %7D%0D%0A                reason = reason.getCause();%0D%0A            %7D%0D%0A            %0D%0A            if (reason != null) %7B%0D%0A                CubeQuest.getInstance().getLogger().log(Level.SEVERE,%0D%0A                        %22An unexpected exception occured while 
 tr
+y
 ing
-(e));
+ to delete quest with id %22%0D%0A                                + quest.getId(),%0D%0A                        e);%0D%0A            %7D%0D%0A            
 %0D%0A  

Improve exception-handling in DeleteQuestCommand.