/*
 * This file is part of Nucleus, licensed under the MIT License (MIT). See the LICENSE.txt file
 * at the root of this project for more details.
 */
package io.github.nucleuspowered.nucleus.argumentparsers;

import com.google.common.collect.Lists;
import io.github.nucleuspowered.nucleus.Nucleus;
import io.github.nucleuspowered.nucleus.Util;
import io.github.nucleuspowered.nucleus.api.data.WarpLocation;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.ArgumentParseException;
import org.spongepowered.api.command.args.CommandArgs;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class HomeParser extends CommandElement {

    private final Nucleus plugin;

    public HomeParser(@Nullable Text key, Nucleus plugin) {
        super(key);
        this.plugin = plugin;
    }

    @Nullable
    @Override
    protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
        if (!(source instanceof User)) {
            throw args.createError(Util.getTextMessageWithFormat("command.playeronly"));
        }

        return getHome((User) source, args.next(), args);
    }

    protected WarpLocation getHome(User user, String home, CommandArgs args) throws ArgumentParseException {
        try {
            Optional<WarpLocation> owl = plugin.getUserLoader().getUser(user).getHome(home.toLowerCase());
            if (owl.isPresent()) {
                return owl.get();
            }

            throw args.createError(Util.getTextMessageWithFormat("args.home.nohome", home.toLowerCase()));
        } catch (Exception e) {
            e.printStackTrace();
            throw args.createError(Text.of(TextColors.RED, "An unspecified error occured"));
        }
    }

    @Override
    public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
        if (!(src instanceof User)) {
            return null;
        }

        User u = (User) src;
        Set<String> s;
        try {
            s = plugin.getUserLoader().getUser(u).getHomes().keySet();
        } catch (Exception e) {
            return null;
        }

        try {
            String n = args.peek();
            return s.stream().filter(x -> n.toLowerCase().startsWith(x.toLowerCase())).collect(Collectors.toList());
        } catch (ArgumentParseException e) {
            return Lists.newArrayList(s);
        }
    }
}

@@ -398,24 +398,84 @@
 rpLocation;%0A
+import io.github.nucleuspowered.nucleus.internal.ConfigMap;%0A
 import org.s
@@ -1897,28 +1897,24 @@
       %7D%0A
-%0A   
         
  throw a
@@ -1909,136 +1909,116 @@
     
- throw args.createError(Util.getTextMessageWithFormat(%22args.home.nohome%22, home.toLowerCase()));%0A        %7D catch (Exception e) %7B%0A
+%7D catch (Exception e) %7B%0A            if (plugin.getConfig(ConfigMap.MAIN_CONFIG).get().getDebugMode()) %7B%0A    
     
@@ -2042,24 +2042,39 @@
 ackTrace();%0A
+            %7D%0A%0A
             
@@ -2159,24 +2159,128 @@
 );%0A        %7D
+%0A%0A        throw args.createError(Util.getTextMessageWithFormat(%22args.home.nohome%22, home.toLowerCase()));
 %0A    %7D%0A%0A    

Fix home parser throwing the wrong exception when no homes exist.