// Really simple login plus menu-driven interface.

import java.io.*;
import java.util.*;

class Login {
    public static void main(String[] args) 
    throws  java.io.IOException {
        setup();
	d = new BufferedReader(new InputStreamReader(System.in));
	int type = 0;
	while (type != 3) {
	    String menu = "Enter:\n"+
		"0 for administrator menu;\n" +
		"1 for student menu;\n" +
		"2 for lecturer menu;\n" +
		"3 to quit the application:\n";
	    System.out.print(menu);
	    type = Integer.parseInt(d.readLine());
	    switch (type) {
	    case 0:
		administratorLogin();
		break;
	    case 1: 
		studentLogin();
		break;
	    case 2:
		lecturerLogin();
		break;
	    default:
		System.out.println ("Unrecognised option");
	    }
	}
    }

    public static void setup() {
	// set some things up that supposedly change rarely enough that it's
	// OK to have them in the code (maybe!)
	Registry.registerCourse(new HonoursCourse 
				("Computer Science", new HashSet())); 
	Registry.registerCourse(new HonoursCourse 
				("Artificial Intelligence", new HashSet())); 
	// and set up some other things just for fun
        Lecturer l = new Lecturer("Perdita Stevens", "123");
	Registry.registerLecturer(l);
	
	Registry.registerModule
	    (new Module 
	     ("Software Engineering with Objects and Components 2",
	      "A must for all intending software engineers.",
	      l));

    }


static void administratorLogin () 
throws java.io.IOException {
    System.out.println
	("Enter the secret administrators' code.");
    String id = d.readLine();
    if (id.equals("sesame")) {
	String menu = "Enter:\n"+
	    "0 to add a student\n" +
	    "1 to add a lecturer;\n" +
	    "2 to add a module;\n" +
	    "3 to update lecturer of a module;\n" +
	    "4 to quit:\n";
	int entry = 0;
	while (entry != 4) {
	    System.out.print(menu);
	    entry = Integer.parseInt(d.readLine());
	    switch (entry) {
	    case 0:
		addStudent();
		break;
	    case 1: 
		addLecturer();
		break;
	    case 2:
		addModule();
		break;
	    case 3:
		changeLecturer();
		break;
	    case 4:
		break;
	    default:
		System.out.println ("Unrecognised option");
	    }
	}
    } else {
	System.out.println
	    ("Validation failed.");
    }
}

    static void addStudent() 
	throws java.io.IOException {
	System.out.println ("Name:");
	String name = d.readLine();
	System.out.println ("Director of Studies:");
	String dosname = d.readLine();
	System.out.println ("Id:");
	String id = d.readLine();
	System.out.println ("Graduating? (y/n)");
	String grad = d.readLine();
	DirectorOfStudies dos = Registry.getDos(dosname);
	if (null == dos) {
	    System.out.println ("Dos not found");
	} else {
	    Student s;
	    if (grad.equals("y")) {
		System.out.println ("Degree course:");
		String courseName = d.readLine();
		HonoursCourse course = Registry.getCourse(courseName);
		if (null == course) {
		    System.out.println ("Unrecognised course");
		} else {
		    s = new GraduatingStudent (name, dos, course);
		    Registry.registerStudent(s);
		}
	    } else {
		s = new NonGraduatingStudent (name, dos);
		Registry.registerStudent(s);
	    }
	}
    }

    static void addLecturer() 
	throws java.io.IOException {
	System.out.println ("Name:");
	String name = d.readLine();
	System.out.println ("Id:");
	String id = d.readLine();
	System.out.println ("Dos? (y/n)");
	String dos = d.readLine();
	Lecturer s;
	if (dos.equals("y")) {
	    s = new DirectorOfStudies (name, id);
	} else {
	    s = new Lecturer (name, id);
	}
	Registry.registerLecturer(s);
    }

    static void addModule() 
	throws java.io.IOException {
	System.out.println ("Name:");
	String name = d.readLine();
	System.out.println ("Description:");
	String desc = d.readLine();
	System.out.println ("Lecturer:");
	String lecturer = d.readLine();
	Lecturer lec = Registry.getLecturer(lecturer);
	if (null == lec) {
	    System.out.println ("Unknown lecturer");
	} else {
	    Registry.registerModule(new Module(name, desc, lec));
	}
	// permitted for which honours courses?
    }

    static void changeLecturer() 
	throws java.io.IOException {
	System.out.println ("Module name:");
	String name = d.readLine();
	Module m = Registry.getModule(name);
	if (null == m) {
	    System.out.println ("Unknown module");
	} else {
	    System.out.println ("New lecturer's name:");
	    String lecName = d.readLine();
	    Lecturer lec = Registry.getLecturer(lecName);
	    if (null == lec) {
		System.out.println ("Unknown lecturer");
	    } else {
		m.lecturer = lec;
		lec.teachModule(m);
	    }
	}
    }

static void studentLogin () 
throws java.io.IOException {
    System.out.println ("Enter name (e.g. Firstname Lastname)");
    String name = d.readLine();
    Student thisStudent = Registry.getStudent(name);
    if (null==thisStudent) {
	System.out.println ("Sorry, name not recognised");
    } else {
	thisStudent.interact(d);
    }
}



static void lecturerLogin () 
throws java.io.IOException {
    System.out.println ("Enter name (e.g. Firstname Lastname)");
    String name = d.readLine();
    Lecturer thisLecturer = Registry.getLecturer(name);
    if (null==thisLecturer) {
	System.out.println ("Sorry, name not recognised");
    } else {
	System.out.println
	    ("Enter your secret personal identifier for validation.");
	String id = d.readLine();
	if (thisLecturer.validate(id)) {
	    thisLecturer.interact(d);
	} else {
	    System.out.println
	    ("Validation failed.");
	}
    }
}



    public static BufferedReader d;

}
