package modules;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import bot.Config;
import bot.Message;
import bot.Module;
import bot.Server;

public class WolframAlpha implements Module {

	@Override
	public void parse(Message m) {
		String target = m.param();
		if(!m.param().startsWith("#")) target = m.sender();
		if(m.botCommand().equals("wa")){
			if(m.hasBotParams()){
				String search = m.botParams().replace(" ", "%20");
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				try {
					DocumentBuilder db = dbf.newDocumentBuilder();
					Document dom = db.parse("http://api.wolframalpha.com/v2/query?appid=" + Config.getWolframAPIKey() + "&input=" + search);
					Element ele = dom.getDocumentElement();
					if(ele.getAttribute("success").equals("true")){
						NodeList nl = ele.getElementsByTagName("pod");
						String result = nl.item(1).getFirstChild().getFirstChild().getNodeValue();
						Server.say(target, "[WA] Result :" + result);
						
					}
					
				} catch (ParserConfigurationException | SAXException | IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

@@ -1093,79 +1093,252 @@
 %09%09%09%09
-String result = nl.item(1).getFirstChild().getFirstChild().getNodeValue
+Element pod1 = (Element)nl.item(1);%0A%09%09%09%09%09%09Element subpod = (Element)pod1.getElementsByTagName(%22subpod%22).item(0);%0A%09%09%09%09%09%09Element plaintext = (Element)subpod.getElementsByTagName(%22plaintext%22).item(0);%0A%09%09%09%09%09%09String result = plaintext.getTextContent
 ();%0A
@@ -1380,10 +1380,10 @@
 sult
- 
 :
+ 
 %22 + 

fix parsing with wa