package xyz.thastertyn.Scrape;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.regex.Pattern;

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

import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Option;
import xyz.thastertyn.Types.Options;

public class Timetable extends JecnaScrape {
	
	private xyz.thastertyn.Types.Timetable timetable;

	private Options schoolYearOptions;
	private Options timetableOptions;

	@Override
	public void download() throws IOException
	{
		download("https://www.spsejecna.cz/timetable/class");
	}

	@Override
	public void download(Choice choice) throws IOException {
		download(String
		.format("https://www.spsejecna.cz/timetable/class?schoolYearId=%s&timetableId=%s",
		choice.getChoices().get(0),
		choice.getChoices().get(1)));
	}

	private void download(String url) throws IOException
	{
		schoolYearOptions = new Options("Skolni R.");
		timetableOptions = new Options("Obdobi");

		timetable = new xyz.thastertyn.Types.Timetable();
		Document timetableHTMLDocument = Downloader.download(url);

		Elements[] timetableHTMLRow = timetableHTMLDocument
			.select("table.timetable")
			.select("tr")
			.stream()
			.map(Element::children)
			.toArray(Elements[]::new);

		for(int i = 0; i < 5; i++) // Days
		{
			for(int j = 0; j < 10; j++) // Individual hours
			{
				String subject = timetableHTMLRow[i+1].get(j+1).select("span.subject").text();

				// Subjects like CEL are thrice, even though everyone has them, make it single
				String[] split = subject.split(" ");
				HashSet<String> set = new HashSet<>(Arrays.asList(split));
				String subj = String.join("/", set);

				timetable.get(i).set(j, subj);
			}
		}

		Element optionsPanel = timetableHTMLDocument.selectFirst("form.listConfigure");

		Elements schoolYear = optionsPanel.select("select[id=schoolYearId]").select("option");
		Elements timetableId = optionsPanel.select("select[id=timetableId]").select("option");

		for(Element e : schoolYear)
		{
			boolean isDefault = e.hasAttr("selected");
			schoolYearOptions.addOption(new Option(e.text(), e.attr("value"), isDefault));
		}

		for(Element e : timetableId)
		{
			// Try to extract the precise school years to make the text shorter
			String optionDisplayText = Pattern
				.compile("(Od .* do .*)")
				.matcher(e.text())
				.results()
				.findFirst()
				.map(m -> m.group(1))
				.orElse(e.text()); // Just use the whole thing if it doesn't follow the pattern

			boolean isDefault = e.hasAttr("selected"); // To prevent the last option added be picked as default always
			timetableOptions.addOption(new Option(optionDisplayText, e.attr("value"), isDefault));
		}
	}

	public xyz.thastertyn.Types.Timetable getRozvrh()
	{
		return timetable;
	}

	@Override
	public String toString()
	{
		return (timetable == null) ?
			"Nothing downloaded yet"
			:
			"All up and ready";
	}

	@Override
	public Options[] getOptions() {
		return new Options[] {schoolYearOptions, timetableOptions};
	}
}
