package clientP2P;

import java.util.Scanner;
import java.util.List;
import clientP2P.ClientManagementUDP;
import clientP2P.ClientManagementTCP;
import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP;
import tools.Directories;
import tools.Logger;
import tools.LogLevel;
import tools.Directories;
import tools.HostItem;
import tools.HostList;

/** Client + Server implementation.
 * @author	Louis Royer
 * @author	Flavien Haas
 * @author	JS Auge
 * @version	1.0
 */

public class ClientP2P {
	static private final String subdir = "seeded/";
	static private String parts = ".parts";
	private Logger loggerServer;
	private Logger loggerClient;
	private String host;
	private int port;
	private Directories directories;
	private List<HostItem> hostList;
	private static final int defaultPort = 20000;


	/** Initialize loggers if directories and logger are null,
	 * else fail silently.
	 */
	public void initLogger() {
		if (directories == null && loggerServer == null && loggerClient == null) {
			directories = new Directories("P2P_JAVA_PROJECT_" + port);
			loggerServer = new Logger(directories.getDataHomeDirectory() + "server.log");
			loggerClient = new Logger(directories.getDataHomeDirectory() + "client.log");
		}
	}

	/** Constructor with portStr as parameter.
	 * @param portStr	String containing port for server listenning.
	 */
	public ClientP2P(String portStr) {
		try{
			port = Integer.valueOf(Integer.parseInt(portStr));
		} catch (NumberFormatException e){
			int oldPort = port;
			port = defaultPort;
			initLogger();
			System.err.println("Error incorrect port " + oldPort + " using default port " + defaultPort);
			loggerServer.write("incorrect port " + oldPort + " using default port " + defaultPort, LogLevel.Info);
		}
		initLogger();
		directories.createSubdir(subdir);
		directories.createSubdir(parts);
		host = "localhost";
		System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
		directories.askOpenDataHomeDirectory(subdir);
		System.out.println("Please enter list of servers to use; first one will be used to ask list of files");
	}

	/** Main program entry point.
	 * 1rst parameter is optionnal, and is used to
	 * define port used by the server module to listen. If not provided, default to another port.
	 * @param args	server listenning port
	 */
	public static void main(String [] args) {

		if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){
			System.out.println("usage : java -ea clientP2P.ClientP2P -- <PORT> ");
		}
		else{
			ClientP2P c;
			try {
				c = new ClientP2P(args[1]);
			} catch (IndexOutOfBoundsException e){
				c = new ClientP2P("" + defaultPort);
			}

			// Server threads
			ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer);
			ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer);
			Thread tudp = new Thread(smudp);
			tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")");
			tudp.start();
			Thread ttcp = new Thread(smtcp);
			ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")");
			ttcp.start();

			// Wait a bit before printing client interface
			// This is not required, but allow to have a cleaner interface
			try {
				Thread.sleep(100);
			} catch(InterruptedException e) {
				Thread.currentThread().interrupt();
			}

			// initialize Host lists
			c.hostList = HostList.getServList();
			System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
			Scanner sc = new Scanner(System.in);
			String transportchoosen = sc.nextLine();
			Thread t;
			switch(transportchoosen){
				case "UDP":
				case "udp":
				case "upd": // alias typo
				case "2" :
					System.out.println("Starting with UDP");
					ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/", c.loggerClient);
					t = new Thread(cmudp);
					break;
				case "TCP":
				case "tcp":
				case "1":
				default:
					System.out.println("Starting with TCP");
					ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/", c.loggerClient);
					t = new Thread(cmtcp);
					break;
				}

			t.setName("client P2P-JAVA-PROJECT");
			t.start();
		}
	}
}
