package io.github.likcoras.ssbot.backends;

import io.github.likcoras.ssbot.ConfigParser;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class MuHandler {
	
	private final String base;
	private final String linkBase =
			"https://www.mangaupdates.com/series.html?id=";
	
	public MuHandler(final ConfigParser cfg) {
		
		base = cfg.getProperty("muurl");
		
	}
	
	public String[] getData(final int id) {
		
		final String[] out = new String[7];
		
		try {
			
			final Document doc = Jsoup.connect(base + id).get();
			
			final Elements title = doc.select("h2");
			out[0] = title.get(1).text();
			
			final Elements info = doc.select("dt");
			out[1] = info.get(0).text();
			
			try {
				
				out[2] = info.get(3).text();
				
			} catch (final IndexOutOfBoundsException e) {
				
				out[2] = info.get(2).text();
				
			}
			
			final Elements last = doc.select("td");
			out[3] = last.get(0).text();
			
			final Date releaseDate =
					new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH)
							.parse(last.get(1).text());
			final long diff =
					(System.currentTimeMillis() - releaseDate.getTime())
							/ (1000 * 60 * 60 * 24);
			
			out[4] = Long.toString(diff);
			out[5] = last.get(2).text();
			
			out[6] = linkBase + id;
			
		} catch (final Exception e) {
			
			System.out.println("Error: Exception while fetching data!");
			System.out
					.println("Please send the following error message to the author:");
			e.printStackTrace();
			
		}
		
		return out;
		
	}
	
}

@@ -815,18 +815,31 @@
 %0A%09%09%09
-try %7B%0A%09%09%09%09
+if (info.size() == 4) %7B
 %0A%09%09%09
@@ -875,63 +875,16 @@
 %0A%09%09%09
-%09%0A%09%09%09%7D catch (final IndexOutOfBoundsException e) %7B%0A%09%09%09%09
+%7D else %7B
 %0A%09%09%09
@@ -917,13 +917,8 @@
 ();%0A
-%09%09%09%09%0A
 %09%09%09%7D

Handle series without publications better