package com.techcavern.wavetact.commands.utils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.techcavern.wavetact.annot.CMD;
import com.techcavern.wavetact.annot.GenCMD;
import com.techcavern.wavetact.utils.GeneralUtils;
import com.techcavern.wavetact.utils.IRCUtils;
import com.techcavern.wavetact.utils.objects.GenericCommand;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.User;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;


public class Google extends GenericCommand {
    @CMD
    @GenCMD

    public Google() {
        super(GeneralUtils.toArray("google goo g"), 0, "google [string to google]" ,"googles something");
    }

    @Override
    public void onCommand(User user, PircBotX Bot, Channel channel, boolean isPrivate, int UserPermLevel, String... args) throws Exception {
        int ArrayIndex = 0;
        if(GeneralUtils.isInteger(args[0])){
            ArrayIndex = Integer.parseInt(args[0])-1;
            args = ArrayUtils.remove(args, 0);
        }
        JsonArray results = GeneralUtils.getJsonObject("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + URLEncoder.encode(StringUtils.join(args, " "), "UTF-8")).getAsJsonObject("responseData").getAsJsonArray("results");
        if(results.size()-1 > 0){
        if (results.size() >= ArrayIndex) {
            String title = results.get(ArrayIndex).getAsJsonObject().get("titleNoFormatting").getAsString();
            String url = results.get(ArrayIndex).getAsJsonObject().get("unescapedUrl").getAsString();
            IRCUtils.SendMessage(user, channel, title + " - " + url, isPrivate);
        }else{
            ArrayIndex = ArrayIndex + 1;
            user.send().notice("Search #" + ArrayIndex + " does not exist");
        }
        } else {
            user.send().notice("Search returned no results");
        }
    }
}


@@ -397,24 +397,75 @@
 ArrayUtils;%0A
+import org.apache.commons.lang3.StringEscapeUtils;%0A
 import org.a
@@ -1656,32 +1656,184 @@
 .getAsString();%0A
+            String content = results.get(ArrayIndex).getAsJsonObject().get(%22content%22).getAsString().replaceAll(%22%5C%5C%3C.*?%3E%22,%22%22).replaceAll(%22%5C%5C&.*?;%22, %22%22);%0A
             Stri
@@ -1981,16 +1981,85 @@
  %22 - %22 +
+ content, isPrivate);%0A            IRCUtils.SendMessage(user, channel,
  url, is
@@ -2060,32 +2060,33 @@
 rl, isPrivate);%0A
+%0A
         %7Delse%7B%0A 

Add Content to Google command