package org.transtruct.cmthunes.ircbot.applets;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Pattern;

import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;

import org.transtruct.cmthunes.irc.*;
import org.transtruct.cmthunes.util.URLBuilder;

public class UrbanDictionaryApplet implements BotApplet {
    private String textWithBreaks(Element element) {
        StringBuilder buffer = new StringBuilder();

        for(Node node : element.childNodes()) {
            if(node instanceof TextNode) {
                buffer.append(((TextNode) node).text().replace("\n", ""));
            } else if(node instanceof Element) {
                if(((Element) node).tagName().equals("br")) {
                    buffer.append("\n");
                } else {
                    buffer.append(this.textWithBreaks(((Element) node)));
                }
            }
        }

        return buffer.toString();
    }

    public void run(IRCChannel channel, IRCUser from, String command, String[] args, String unparsed) {
        Document doc;
        URLBuilder url;
        int definitionIndex = 0;
        
        if(unparsed.trim().length() == 0) {
            channel.write("Missing search term");
            return;
        }
        
        if(args.length >= 2 && args[0].startsWith("-")) {
            String s = args[0].substring(1);
            try {
                definitionIndex = Integer.valueOf(s) - 1;
                unparsed = unparsed.replaceFirst(Pattern.quote(args[0]), "");
            } catch(NumberFormatException e) {
                // -
            }
        }

        try {
            Connection con;

            url = new URLBuilder("http://www.urbandictionary.com/define.php");
            url.setParameter("term", unparsed);

            while(true) {
                con = Jsoup.connect(url.toString());
                con.followRedirects(false);
                con.execute();
            
                if(con.response().statusCode() == 200) {
                    doc = con.response().parse();
                    break;
                } else if(con.response().statusCode() == 302) {
                    url = new URLBuilder(con.response().header("Location"));
                } else {
                    channel.write("Error loading page");
                    return;
                }
            }
        } catch (IOException e) {
            channel.write("Unknown error occured");
            e.printStackTrace();
            return;
        }

        if(!doc.select("div#not_defined_yet").isEmpty()) {
            channel.write("No definitions found");
        } else {
            try {
                Elements elements = doc.select("div.definition");

                if(elements.size() > definitionIndex) {
                    Element def = elements.get(definitionIndex);
                    String definition = this.textWithBreaks(def);
                    String[] lines = definition.split("\n");
                    String longestLine = lines[0];
                    String response;
                    
                    for(int i = 1; i < lines.length && longestLine.length() < 15; i++) {
                        if(lines[i].length() > longestLine.length()) {
                            longestLine = lines[i];
                        }
                    }

                    response = unparsed + ": " + longestLine;
                    channel.writeMultiple(BotAppletUtil.blockFormat(response, 400, 10));
                } else {
                    channel.write("Can not get definition");
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -1179,16 +1179,48 @@
 ex = 0;%0A
+        boolean random = false;%0A
         
@@ -1212,32 +1212,32 @@
 false;%0A        %0A
-
         if(unpar
@@ -1280,45 +1280,88 @@
     
-channel.write(%22Missing search term%22);
+random = true;%0A        %7D%0A%0A        if(args.length == 1 && args%5B0%5D.equals(%22-r%22)) %7B
 %0A   
@@ -1362,37 +1362,44 @@
  %7B%0A            r
-eturn
+andom = true
 ;%0A        %7D%0A    
@@ -1796,16 +1796,149 @@
 n con;%0A%0A
+            if(random) %7B%0A                url = new URLBuilder(%22http://www.urbandictionary.com/random.php%22);%0A            %7D else %7B%0A    
         
@@ -2020,16 +2020,20 @@
         
+    
 url.setP
@@ -2059,16 +2059,30 @@
 parsed);
+%0A            %7D
 %0A%0A      
@@ -3671,23 +3671,44 @@
 onse = u
-nparsed
+rl.getQueryParameter(%22term%22)
  + %22: %22 

Add random search