/*
 * main menu for the database
 */

import java.util.Scanner;
import util.InputHelper;

/**
 * @author anwar
 */
public class Menu {
    /** Shared keyboard for user input.
    */
    private static Scanner keyboard = new Scanner(System.in);

    /** Main method handles the UI. Add code to implement each operation.
        @param args the command-line arguments (not used)
    */
    public static void main(String[] args) {
 
        int choice;
        String name, type, number;
        DBase myDb = new DbaseImpl();

        System.out.println("Welcome to Student Database");
        do {
            String num;
            System.out.println();
            System.out.println("0) Quit the program");
            System.out.println("1) Create a new database");
            System.out.println("2) Open an existing database");
            System.out.println("3) List all contacts");
            System.out.println("4) Add a new contact");
            System.out.println("5) Find a contact");
            System.out.println("6) Remove a contact by ID");
            System.out.println("7.Close the Database");
            System.out.print("-> Enter choice -> ");
            num = keyboard.next();
            keyboard.nextLine();
            choice = num.charAt(0);
            processMenu(choice,myDb);
        } while (choice != 0);
    }

    public static void processMenu(int choice,DBase db)
    {
        String dbName;
        switch (choice) {
                case '0': 
                    db.close();
                    System.exit(0);
                case '1': 
                    dbName = InputHelper.getStringInput("Enter Database name:");
                    if(db.create(dbName)){
                        System.out.println("Database " + dbName + " is created.");
                    }else{
                        System.out.println("Cannot create the database " + dbName);
                    }
                    break;
                case '2':
                    dbName = InputHelper.getStringInput("Enter Database name:");
                    if(db.open(dbName)){
                        System.out.println("Database " + dbName + " is opened.");
                    }else{
                        System.out.println("Cannot open the database " + dbName);
                    }
                    break;
                case '3': 
                    displayAll(db);   //displays all students in the database;
                    break;
                    
                case '4': 
                    addNewStudent(db);
                    break;
                case '5':
                    findStudents(db);
                    break;
                case '6': 
                    removeStudent(db);
                    break;
                case '7': 
                    System.out.println("\nClosing the database\n");
                    db.close();
                    break;
                default:
                    System.out.println("Sorry - invalid choice - try again");
            }   
    }
    private static void displayAll(DBase db)
    {
        if(db.isOpen()){
            for(Student s: db){
                System.out.println(s);
            }
        }else{
            System.out.println("There is no open database.");
        }
    }
    private static void addNewStudent(DBase db)
    {
        if(db.isOpen()){
            //TO DO
			// Add a new student to Database
			//User provides student info
			System.out.println("TO DO");
        }else{
            System.out.println("There is no open database.");
        }
    }
    private static void findStudents(DBase db)
    {
        if(db.isOpen()){  
            String name = InputHelper.getStringInput("name:");
            //find and display all students whose first name or last name is  equal to user entered name.
        }else{
            System.out.println("There is no open database.");
        }
    }
    private static void removeStudent(DBase db)
    {
        if(db.isOpen()){
            String id = InputHelper.getStringInput("Enter the ID:");
            db.remove(id);
        }else{
            System.out.println("There is no open database.");
        }
    }
}
