package xyz.thastertyn.Scrape;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Downloader {

	private static String JsessionId;

	private static boolean isInitialized = false;

	/**
	 * Provide a general Jsoup Connection for simplicity with some predefined values and JSessionId already set
	 * @param url
	 * @return HTML Document downloaded. If it fails, null
	 */
	public static Document download(String url) throws IOException
	{
		return Jsoup.connect(url)
		.header("Connection", "keep-alive")
		.cookie("role", "student")
		.cookie("JSESSIONID", JsessionId)
		.timeout(10000)
		.get();
	}

	/**
	 * Returns a basic {@link Connection} with some predefined headers
	 * @param url
	 * @return {@link Connection} with timeout for 10s, and headers needed for downloading from spsejecna.cz
	 */
	public static Connection getConnection(String url)
	{
		return Jsoup.connect(url)
			.header("Connection", "keep-alive")
			.cookie("role", "student")
			.cookie("JSESSIONID", JsessionId)
			.timeout(10000);
	}

	/**
	 * Sets JsessionId
	 * @param newJsessionId JsessionId
	 */
	public static void setJSessionId(String newJsessionId)
	{
		JsessionId = newJsessionId;
		isInitialized = true;
	}

	public static boolean isInitialized()
	{
		return isInitialized;
	}
}
