package com.techcavern.wavetact.ircCommands.reference;

import com.google.gson.JsonArray;
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.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.User;

@IRCCMD
public class Search extends IRCCommand {

    public Search() {
        super(GeneralUtils.toArray("search google g goog gsearch"), 1, "search (result #) [string to google]", "Googles something", false);
    }

    @Override
    public void onCommand(String command, User user, PircBotX network, String prefix, 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=" + StringUtils.join(args, "%20")).getAsJsonObject("responseData").getAsJsonArray("results");
        if (results.size() > 0) {
            if (results.size() - 1 >= ArrayIndex) {
                String title = results.get(ArrayIndex).getAsJsonObject().get("titleNoFormatting").getAsString();
                String content = GeneralUtils.stripHTML(results.get(ArrayIndex).getAsJsonObject().get("content").getAsString());
                String url = results.get(ArrayIndex).getAsJsonObject().get("unescapedUrl").getAsString();
                IRCUtils.sendMessage(user, network, channel, "[" + title + "] " + content + " - " + GeneralUtils.shortenURL(url), prefix);
            } else {
                ArrayIndex = ArrayIndex + 1;
                IRCUtils.sendError(user, network, channel, "Search #" + ArrayIndex + " does not exist", prefix);
            }
        } else {
            IRCUtils.sendError(user, network, channel, "Search returned no results", prefix);
        }
    }
}


@@ -362,24 +362,115 @@
 tringUtils;%0A
+import org.jsoup.Jsoup;%0Aimport org.jsoup.nodes.Document;%0Aimport org.jsoup.select.Elements;%0A
 import org.p
@@ -671,20 +671,26 @@
 e g 
-goog gsearch
+dogpile yahoo bing
 %22), 
@@ -1158,51 +1158,33 @@
     
-JsonArray results = GeneralUtils.getJsonObj
+Document doc = Jsoup.conn
 ect(
@@ -1192,155 +1192,184 @@
 http
-s
 ://
-ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%22 + StringUtils.join(args, %22%2520%22)).getAsJsonObject(%22responseData%22).getAsJsonArray(%22r
+www.dogpile.com/dogpilecontrol/search/web?fcoid=417&fcop=topnav&fpid=27&q=thing%22 + StringUtils.join(args, %22%2520%22)).get();%0A        Elements results = doc.select(%22.searchR
 esult
-s
 %22);%0A
@@ -1513,193 +1513,130 @@
 ex).
-getAsJsonObject().get(%22titleNoFormatting%22).getAsString();%0A                String content = GeneralUtils.stripHTML(results.get(ArrayIndex).getAsJsonObject().get(%22content%22).getAsString
+select(%22.resultTitlePane%22).text();%0A                String url = results.get(ArrayIndex).select(%22.resultDisplayUrl%22).text
 ()
-)
 ;%0A  
@@ -1648,35 +1648,39 @@
          String 
-url
+content
  = results.get(A
@@ -1694,57 +1694,41 @@
 ex).
-getAsJsonObject().get(%22unescapedUrl%22).getAsString
+select(%22.resultDescription%22).text
 ();%0A

Switch from Google to DogPile (Resolves #213)