package xyz.thastertyn.JecnaParse;

import java.io.IOException;
import java.net.UnknownHostException;

import javax.security.auth.login.CredentialException;

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

public class Login {

	private String Jsessionid = null;

	// Check for session expiration
	private long start;
	private long lastCheck;

	public void loginJecna(String user, String pass) throws UnknownHostException, IOException, CredentialException
	{
			//#region JSESSIONID
			Connection.Response response = Jsoup.connect("https://www.spsejecna.cz")
			.header("Connection", "keep-alive")
			.method(Method.HEAD)
			.execute();

			Jsessionid = response.cookie("JSESSIONID");
			Downloader.setJSessionId(Jsessionid);
			//#endregion

			//#region Token3
			String token3 = Jsoup.connect("https://www.spsejecna.cz/user/role?role=student")
			.header("Connection", "keep-alive")
			.cookie("JSESSIONID", Jsessionid)
			.get()
			.select("input[name=token3]")
			.attr("value");
			//#endregion

			//#region Login
			Jsoup.connect("https://www.spsejecna.cz/user/login")
			.method(Connection.Method.POST)
			.header("Content-Type", "application/x-www-form-urlencoded")
			.header("Origin", "https://www.spsejecna.cz")
			.header("Connection", "keep-alive")
			.cookie("JSESSIONID", Jsessionid)
			.cookie("role", "student")
			.data("token3", token3)
			.data("user", user)
			.data("pass", pass)
			.data("submit", "P%C5%99ihl%C3%A1sit+se")
			.followRedirects(true)
			.execute();
			//#endregion

			Document test = Jsoup.connect("https://www.spsejecna.cz/score/student")
			.header("Connection", "keep-alive")
			.cookie("JSESSIONID", Jsessionid)
			.cookie("role", "student")
			.get();

			if(test.toString().contains("Pro pokračování se přihlaste do systému"))
			{
				throw new CredentialException("Incorrect username or password");
			}

			start = System.currentTimeMillis() / 1000L;
			lastCheck = start;
	}

	/*public void loginJidelna() throws UnknownHostException, IOException
	{
            //#region JSESSIONID
            Connection.Response jidelna = Jsoup.connect("https://objednavky.jidelnasokolska.cz/")
            .header("Connection", "keep-alive")
            .method(Method.HEAD)
            .execute();

            String jidelnaJSESSIONID = jidelna.cookie("JSESSIONID");
            String XSRF_TOKEN = jidelna.cookie("XSRF-TOKEN");
            //#endregion

            //#region CSRF
            String csrf = Jsoup.connect("https://objednavky.jidelnasokolska.cz/faces/login.jsp")
            .header("Connection", "keep-alive")
            .cookie("XSRF-TOKEN", XSRF_TOKEN)
            .cookie("JSESSIONID", jidelnaJSESSIONID)
            .get()
            .select("input[name=_csrf]")
            .attr("value");


            if(!XSRF_TOKEN.equals(csrf))
            {
                throw new SecurityException("CSRF tokens do not match, something is up");
            }
            //#endregion

            //#region Login
            Connection.Response jidelnaLogin = Jsoup.connect("https://objednavky.jidelnasokolska.cz/j_spring_security_check")
            .header("Connection", "keep-alive")
            .header("Content-Type", "application/x-www-form-urlencoded")
            .cookie("XSRF-TOKEN", XSRF_TOKEN)
            .cookie("JSESSIONID", jidelnaJSESSIONID)
            .data("j_username", Credentials.user)
            .data("j_password", Credentials.pass)
            .data("terminal", "false")
            .data("type", "web")
            .data("_csrf", XSRF_TOKEN)
            .data("targetUrl", "/faces/secured/main.jsp?terminal=false&status=true&printer=false&keyboard=false")
            .method(Method.POST)
            .execute();
            //#endregion
	}*/
}
