package me.despawningbone.discordbot.command.info;

import java.awt.Color;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import me.despawningbone.discordbot.command.Command;
import me.despawningbone.discordbot.command.CommandResult;
import me.despawningbone.discordbot.command.CommandResult.CommandResultType;
import me.despawningbone.discordbot.utils.MiscUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;

public class CityInfo extends Command {
	public CityInfo() {
		this.alias = Arrays.asList("ci", "weather");
		this.desc = "Search for info about a city!"; //"Search for info about the city the address is in!";
		this.usage = "<address/search words>";
		for (String country : Locale.getISOCountries()) {
            Locale locale = new Locale("en", country);
            countryCodes.put(locale.getDisplayCountry(Locale.ENGLISH), locale.getCountry());
		}
		this.examples = Arrays.asList("hong kong", "tokyo");  //"HK", "akihabara");
	} 
	
	HashMap<String, String> countryCodes = new HashMap<>();
	
	NumberFormat formatter = new DecimalFormat("#0.00");
	
	@Override
	public CommandResult execute(TextChannel channel, User author, Message msg, String[] args) {
		if(args.length < 1) {
			return new CommandResult(CommandResultType.INVALIDARGS, "Please input a city name."); //or a address.");
		 } else {
			channel.sendTyping().queue();
			String sword = String.join(" ", args);
			try {
		        boolean hasWeather = true;
		        JSONObject info = null;
		        String wQualifiedName = "",  woeid = "", lng = "", lat = "", region = "", countryShort = ""; TimeZone timezone = null;
		        try {
		        	URLConnection sCon = new URL("https://www.yahoo.com/news/_tdnews/api/resource/WeatherSearch;text=" + URLEncoder.encode(sword, "UTF-8") + "?returnMeta=true").openConnection();
		        	sCon.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
		        	JSONObject wsearch = new JSONObject(new JSONTokener(sCon.getInputStream())).getJSONArray("data").getJSONObject(0);
		        	woeid = String.valueOf(wsearch.getInt("woeid"));  //yahoo scrape
		        	lat = String.valueOf(wsearch.getDouble("lat"));
		        	lng = String.valueOf(wsearch.getDouble("lon"));
		        	wQualifiedName = wsearch.getString("qualifiedName");
		        	countryShort = countryCodes.get(wsearch.getString("country"));
		        	region = wQualifiedName.split(",")[wQualifiedName.split(",").length - 2];  //get second highest level, highest should always be country code
		        	timezone = TimeZone.getTimeZone(new JSONObject(new JSONTokener(new URL("https://api.internal.teleport.org/api/locations/" + lat + "," + lng + "/?embed=location:nearest-cities/location:nearest-city/city:timezone").openStream())).getJSONObject("_embedded").getJSONArray("location:nearest-cities").getJSONObject(0).getJSONObject("_embedded").getJSONObject("location:nearest-city").getJSONObject("_embedded").getJSONObject("city:timezone").getString("iana_name"));
		        	//can use metaweather, but not accurate enough
		        	//can also broaden the scope for yahoo scrape for it to work better
		        	//JSONObject wsearch = new JSONObject(new JSONTokener(new URL("https://api.flickr.com/services/rest/?method=flickr.places.findByLatLon&api_key=bdaafeafab62267931d920dda27a4f90&lat=" + lat + "&lon=" + lng + "&format=json&nojsoncallback=1").openStream())).getJSONObject("places").getJSONArray("place").getJSONObject(0);  //gonna use flickr find instead
		        	URLConnection iCon = new URL("https://www.yahoo.com/news/_tdnews/api/resource/WeatherService;woeids=[" + woeid + "]?lang=en-US&returnMeta=true").openConnection();
		        	iCon.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
		        	info = new JSONObject(new JSONTokener(iCon.getInputStream())).getJSONObject("data").getJSONArray("weathers").getJSONObject(0);
		        } catch(IOException e) {
		        	return new CommandResult(CommandResultType.ERROR, ExceptionUtils.getStackTrace(e));
		        } catch(JSONException e) {
		        	e.printStackTrace();
		        	return new CommandResult(CommandResultType.NORESULT);
		        	//hasWeather = false;
		        }
		        
		        Date date = new Date();
		        //System.out.println(info);
				EmbedBuilder embedmsg = new EmbedBuilder();
		        embedmsg.setAuthor("Info for " + wQualifiedName, null, null);
		        embedmsg.setColor(new Color(100, 0, 255));
		        embedmsg.addField("Country", MiscUtils.countryNameToUnicode(countryShort), true);
		        embedmsg.addField("Region", region, true);
		        embedmsg.addField("Current time", OffsetDateTime.now(timezone.toZoneId()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ssa").withLocale(Locale.ENGLISH)).trim(), true);
		        long hours = (timezone.getOffset(date.getTime())/1000/60/60);
		        embedmsg.addField("Timezone" , timezone.getDisplayName(timezone.inDaylightTime(date), TimeZone.LONG, Locale.ENGLISH) + " (UTC" + (Math.signum(hours) == 1 || Math.signum(hours) == 0 ? "+" + hours : hours) + ")" + "\u1160", true);   //FLICKR DED
		        String footer = "Weather info not available";
		        if (hasWeather) {   //use another api if weather info not available?
		        	JSONObject obs = info.getJSONObject("observation"); JSONObject temp = obs.getJSONObject("temperature"); JSONObject forecast = info.getJSONObject("forecasts").getJSONArray("daily").getJSONObject(0);
		        	if(temp.has("now")) {
			        	embedmsg.addField("Temperature", fToC(temp.getInt("now")) + "°C (↑" + fToC(temp.getInt("high")) + "°C | ↓" + fToC(temp.getInt("low")) + "°C)", true);
				        embedmsg.addField("Humidity", obs.getInt("humidity") + "% (Chance of rain: " + forecast.getInt("precipitationProbability") + "%)", true);
				        embedmsg.addField("Visibility", miToKm(obs.getDouble("visibility")) + "km (" + obs.getString("conditionDescription") + ")", true);
				        embedmsg.addField("Atmospheric pressure", formatter.format(obs.getDouble("barometricPressure") / 0.029530) + "millibars", true);
				        embedmsg.addField("Wind speed", miToKm(obs.getDouble("windSpeed")) + "km/h", true);
				        embedmsg.addField("Wind direction", obs.getInt("windDirection") + "° (" + obs.getString("windDirectionCode") + ")", true);
				        embedmsg.addField("Feels Like", fToC(temp.getInt("feelsLike")) + "°C", true);
				        embedmsg.addField("UV index", obs.getInt("uvIndex") + " (" + obs.getString("uvDescription") + ")", true);
				        embedmsg.addField("Sunrise", MiscUtils.convertMillis(info.getJSONObject("sunAndMoon").getLong("sunrise") * 1000).substring(0, 5), true);
				        embedmsg.addField("Sunset", MiscUtils.convertMillis(info.getJSONObject("sunAndMoon").getLong("sunset") * 1000).substring(0, 5), true);
				        String imgUrl = info.getJSONArray("photos").getJSONObject(0).getJSONArray("resolutions").getJSONObject(0).getString("url");  //seems to have dead urls, how fix
				        embedmsg.setThumbnail(imgUrl.split(":\\/\\/").length > 2 ? "https://" + imgUrl.split(":\\/\\/")[2] : imgUrl);
				        footer = "Weather info last updated: " + OffsetDateTime.parse(obs.getJSONObject("observationTime").getString("timestamp")).format(DateTimeFormatter.RFC_1123_DATE_TIME)
				        		.replace("GMT", timezone.getDisplayName(timezone.inDaylightTime(date), TimeZone.SHORT, Locale.ENGLISH)); //+ " | " + wQualifiedName;
				        //add weather provider to footer?
		        	}
		        }
		        embedmsg.addField("Latitude", lat, true);
		        embedmsg.addField("Longitude", lng, true);
		        embedmsg.setFooter(footer, null);
		        try {
		        	MessageEmbed fmsg = embedmsg.build();
		        	channel.sendMessageEmbeds(fmsg).queue();
		        } catch(InsufficientPermissionException e2) {
		        	return new CommandResult(CommandResultType.FAILURE, "Unfortunately, the bot is missing the permission `MESSAGE_EMBED_LINKS` which is required for this command to work.");
		        }
		        return new CommandResult(CommandResultType.SUCCESS);
			} catch (Exception e) {
				return new CommandResult(CommandResultType.ERROR, ExceptionUtils.getStackTrace(e));
			}
		 }
	}
	
	private String fToC(int f) {  //fucking no metric ree
		return formatter.format((f-32)*5.0/9);
	}
	
	private String miToKm(double mile) {
		return formatter.format(mile*1.609344);
	}
}
