package tools;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.InputMismatchException;
import tools.HostItem;

/** Helper to get the server list from the user
* @author	Louis Royer
* @author	Flavien Haas
* @author	JS Auge
* @version	1.0
*/
public class HostList {
	/**
	* Let the user enter all server and puts it in a list
	* @return list of servers
	*/
	public static List<HostItem> getServList() {
		List<HostItem> serverList = new ArrayList<HostItem>();
		Scanner scanner = new Scanner(System.in);
		String servName;
		int port = 0;
		do {
			System.out.println("Enter hostname of next server: (or \"stop\" when finished, default: localhost).");
			servName = scanner.nextLine();
			if (servName.equals("")) {
				servName = "localhost";
			}
			if (!servName.equals("stop")) {
				boolean err = false;
				do {
					System.out.println("Enter port for this server");
					try {
						port = scanner.nextInt();
						scanner.nextLine();
						if (port > 65535 || port <= 0) {
							err = true;
							System.out.println("Port number must be in 1-65535 range. Try again.");
						} else {
							err = false;
						}
					} catch (InputMismatchException e) {
						System.out.println("Invalid number. Try again.");
						err = true;
					}
				} while (err);
				serverList.add(new HostItem(servName, port));
			}
			
		} while (!servName.equals("stop"));
		return serverList;
	}
}
