/*
 * This source file was generated by the Gradle 'init' task
 */
package utb.fai;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashSet;
import java.util.LinkedList;

import javax.swing.text.html.parser.ParserDelegator;

public class App {

	public static void main(String[] args) {
		LinkedList<URIinfo> foundURIs = new LinkedList<URIinfo>();
		HashSet<URI> visitedURIs = new HashSet<URI>();
		URI uri;
		try {
			uri = new URI(args[0] + "/");
			foundURIs.add(new URIinfo(uri, 0));
			visitedURIs.add(uri);

			if (args.length < 1) {
				System.err.println("Missing parameter - start URL");
				return;
			}
			/**
			 * Zde zpracujte dalí parametry - maxDepth a debugLevel
			 */

			ParserCallback callBack = new ParserCallback(visitedURIs, foundURIs);
			ParserDelegator parser = new ParserDelegator();

			while (!foundURIs.isEmpty()) {
				URIinfo URIinfo = foundURIs.removeFirst();
				callBack.depth = URIinfo.depth;
				callBack.pageURI = uri = URIinfo.uri;
				System.err.println("Analyzing " + uri);
				try {
					BufferedReader reader = new BufferedReader(new InputStreamReader(uri.toURL().openStream()));
					parser.parse(reader, callBack, true);
					reader.close();
				} catch (FileNotFoundException e) {
					System.err.println("Error loading page - does it exist?");
				}
			}
		} catch (Exception e) {
			System.err.println("Zachycena neoetøená výjimka, konèíme...");
			e.printStackTrace();
		}
	}

}
