package tracker;

import java.util.Scanner;
import tracker.TrackerManagementTCP;
import tracker.TrackerManagementUDP;
import tools.Directories;
import tools.Logger;
import tools.LogLevel;
import tools.TrackerPortRange;
import tools.HostItem;

/** Tracker implementation
 * First argument of main method is port listened by the tracker, and is mandatory.
 * @author	Louis Royer
 * @author	Flavien Haas
 * @author	JS Auge
 * @version	1.0
 */
public class Tracker {
	private HostItem tracker;
	private Directories directories;
	private Logger logger;

	/** Constructor with portStr containing a port number.
	 * @param hostname	hostname to bind
	 * @param port	port to bind
	 * @param isInteractive	true if application is in interactive mode
	 */
	public Tracker(String hostname, int port, boolean isInteractive) {
		tracker = new HostItem(hostname, port);
		directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + port);
		logger = new Logger(directories.getDataHomeDirectory() + "tracker.log");
		System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory());
		Scanner scanner = new Scanner(System.in);
		if (isInteractive) {
			directories.askOpenDataHomeDirectory(null, scanner);
		}
		scanner.close();
	}

	/** Main program entry point
	 * first parameter is port number and is mandatory
	 * to test, run with: java serverP2P.ServerP2P
	 * @param args	parameters
	 */
	public static void main(String [] args) {
		final TrackerPortRange trackerPortRange = new TrackerPortRange();
		final String defaultHostname = "localhost";
		Scanner scanner = new Scanner(System.in);
		String hostname = "";
		int port = 0;
		Tracker t;
		boolean isInteractive = false;

		if ((args.length != 3) && (args.length != 0)) {
			System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- <hostname> <PORT> (" + trackerPortRange +")");
			System.exit(1);
		} else if (args.length == 3) {
			isInteractive = false;
			hostname = args[1];
			port = Integer.valueOf(Integer.parseInt(args[2]));
		} else {
			isInteractive = true;
			System.out.println("Tracker Server, enter hostname to bind (default = localhost): ");
			hostname = scanner.nextLine();
			if (hostname.equals("")) {
				hostname = defaultHostname;
				System.out.println("using default hostname : " + hostname);
			}
			System.out.println("enter port (default = " + trackerPortRange.getDefaultPort() +"): ");
			String portStr = scanner.nextLine();
			if (portStr.equals("")) {
				port = trackerPortRange.getDefaultPort();
				System.out.println("using default port : " + port);
			} else {
				port = Integer.valueOf(Integer.parseInt(portStr));
			}
		}

		System.out.println("using hostname : " + hostname);
		if (trackerPortRange.isPortInRange(port)) {
			System.out.println("using port : " + port);
			t = new Tracker(hostname, port, isInteractive);
			TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger);
			TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, t.logger);
			Thread tudp = new Thread(tmudp);
			tudp.setName("Tracker UDP P2P-JAVA-PROJECT");
			tudp.start();
			Thread ttcp = new Thread(tmtcp);
			ttcp.setName("Tracker TCP P2P-JAVA-PROJECT");
			ttcp.start();
		} else {
			System.out.println("Port not in range. " + trackerPortRange);
			System.exit(2);
		}
	}
}
