package com.techcavern.wavetact.ircCommands.dnsinfo;

import com.google.gson.JsonObject;
import com.techcavern.wavetact.annot.IRCCMD;
import com.techcavern.wavetact.objects.IRCCommand;
import com.techcavern.wavetact.utils.GeneralUtils;
import com.techcavern.wavetact.utils.IRCUtils;
import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.User;

import java.util.HashSet;
import java.util.Set;

@IRCCMD
public class FindIP extends IRCCommand {

    public FindIP() {
        super(GeneralUtils.toArray("findip locate fip find loc geo geoip"), 1, "findip (+)(IP)(domain)(user)", "GeoIPs a user", false);
    }

    @Override
    public void onCommand(String command, User user, PircBotX network, String prefix, Channel channel, boolean isPrivate, int userPermLevel, String... args) throws Exception {
        boolean IPv6Priority = false;
        String nick = user.getNick();
        if (args.length > 0) {
            if (args[0].equalsIgnoreCase("+")) {
                IPv6Priority = true;
            } else if (args[0].startsWith("+")) {
                nick = args[0].replaceFirst("\\+", "");
                IPv6Priority = true;
            } else {
                nick = args[0];
            }
        }
        String IP = GeneralUtils.getIP(nick, network, IPv6Priority);
        if (IP == null) {
            IRCUtils.sendError(user, network, channel, "Please enter in an ip/user/domain as argument #1", prefix);
            return;
        }
        JsonObject objectJson = GeneralUtils.getJsonObject("http://ip-api.com/json/" + IP);
        Set<String> results = new HashSet<>();
        if (objectJson.get("status").getAsString().equalsIgnoreCase("success")) {
            results.add(objectJson.get("city").getAsString());
            results.add(objectJson.get("regionName").getAsString());
            results.add(objectJson.get("country").getAsString());
            results.add(objectJson.get("zip").getAsString());
            results.add(objectJson.get("isp").getAsString());
            results.add(objectJson.get("timezone").getAsString());
            String message = "";
            for (String res : results) {
                if (!res.isEmpty()) {
                    if (message.isEmpty()) {
                        message = res;
                    } else {
                        message += ", " + res;
                    }
                }
            }
            if (!message.isEmpty()) {
                IRCUtils.sendMessage(user, network, channel, "[" + IP + "] " + message, prefix);
            } else {
                IRCUtils.sendError(user, network, channel, "Unable to determine location (or you entered an invalid ip)", prefix);
            }
        } else {
            IRCUtils.sendError(user, network, channel, "Unable to determine location (or you entered an invalid ip)", prefix);
        }


    }
}

@@ -362,16 +362,44 @@
 .User;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1598,18 +1598,24 @@
         
-Se
+ArrayLis
 t%3CString
@@ -1630,22 +1630,24 @@
 s = new 
-HashSe
+ArrayLis
 t%3C%3E();%0A 

Switch FindIP output back to ArrayList (Resolves #209)