package heufybot.modules;

import heufybot.utils.FileUtils;
import heufybot.utils.URLUtils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class WeatherInterface 
{
	private final static String APIkey = FileUtils.readFile("data/worldweatheronlineapikey.txt").replaceAll("\n", "");
	private final static String APIAddress = "http://api.worldweatheronline.com/free/v1/weather.ashx?";
	private final static String web = "http://www.worldweatheronline.com/v2/weather.aspx?q=";
	private final static String weatherFormat = "Temp: %s C/%s F | Weather: %s | Humidity: %s%c | Wind: %s kmph/%smph %s | Local Observation Time: %s";
	
	public String getWeather(float latitude, float longitude) throws ParseException
	{
		StringBuilder builder = new StringBuilder();
		builder.append(APIAddress);
		builder.append("q=" + latitude + "," + longitude);
		builder.append("&key=" + APIkey);
		builder.append("&format=json");
		builder.append("&extra=localObsTime");
		JSONObject object = getJSON(builder.toString());
		
		return parseJSON(object) + " | More info: " + URLUtils.shortenURL(web + latitude + "," + longitude);
	}
	
	private String parseJSON(JSONObject object)
	{
		JSONObject data = (JSONObject)object.get("data");
		JSONObject currentCondition = (JSONObject) ((JSONArray)data.get("current_condition")).get(0);

		String curTime = currentCondition.get("localObsDateTime").toString();
		String tempC = currentCondition.get("temp_C").toString();
		String tempF = currentCondition.get("temp_F").toString();
		String windspeedMiles = currentCondition.get("windspeedMiles").toString();
		String windspeedKmph = currentCondition.get("windspeedKmph").toString();
		String windDir = currentCondition.get("winddir16Point").toString();
		String desc = ((JSONObject)((JSONArray)currentCondition.get("weatherDesc")).get(0)).get("value").toString();
		String humidity = currentCondition.get("humidity").toString();
		
		return String.format(weatherFormat, tempC, tempF, desc, humidity, '%', windspeedKmph, windspeedMiles, windDir, curTime);
	}
	
	private JSONObject getJSON(String urlString) throws ParseException 
	{
		return (JSONObject)new JSONParser().parse(URLUtils.grab(urlString));
	}
}

@@ -703,43 +703,14 @@
 smph
- %25s %7C Local Observation Time:
  %25s%22;%0A
+
 %09%0A%09p
@@ -995,49 +995,8 @@
 %22);%0A
-%09%09builder.append(%22&extra=localObsTime%22);%0A
 %09%09JS
@@ -1354,80 +1354,8 @@
 );%0A%0A
-%09%09String curTime = currentCondition.get(%22localObsDateTime%22).toString();%0A
 %09%09St
@@ -1867,24 +1867,24 @@
 tring();%0A%09%09%0A
+
 %09%09return Str
@@ -1986,17 +1986,8 @@
 dDir
-, curTime
 );%0A%09

Remove Local Observation Time since it's never accurate