/*
 * ElectronicButler.java
 * Nadeem Abdul Hamid - Fall 2004 - CSC120 - Berry College
 *
 * This is the main program that provides an interface to the manor
 * database. 
 *
 * Based on a program developed in "Great Ideas in Computer Science 
 * with Java" by Biermann and Ramm.
 *
 */

import java.io.*;

public class ElectronicButler {

    /* possible commands to issue to the butler */
    static final int INVALIDCOMMAND = -1;
    static final int LOADCOMMAND = 1;
    static final int ADDCOMMAND = 2;
    static final int QUERYCOMMAND = 3;
    static final int SAVECOMMAND = 4;
    static final int PRINTCOMMAND = 5;
    static final int QUITCOMMAND = 9;



    /* the butler's electronic database of knowledge about the
     * going on's of the manor */
    ManorDatabase db;

    // default database file name
    String filename;



    /* Constructor:
     * Prepare the electronic butler
     */
    public ElectronicButler() {
        for (int i=0; i<75; i++) System.out.print("*");
        System.out.println("\n\nWelcome to the Electronic Butler program.");
        db = new ManorDatabase();
        filename = "butler.db";
    }



    /*
     * Put the electronic butler into action, ready to answer queries
     * or add new facts to his database
     */
    public void engage() {
        int cmd = INVALIDCOMMAND;
    
        do {
            printMenu();
            try { cmd = Keyboard.readInt(); }
            catch (NumberFormatException e) { 
            cmd = INVALIDCOMMAND; 
            }
    
            for (int i=0; i<75; i++) System.out.print("*");
            System.out.println("\n");
    
            switch (cmd) {
            case LOADCOMMAND: loadFile(); break;
            case ADDCOMMAND:  db.inputFact(); break;
            case QUERYCOMMAND: queryFacts(); break;
            case SAVECOMMAND: saveFile(); break;
            case PRINTCOMMAND: db.printFacts(); break;
            case QUITCOMMAND: break;
            default:
            System.out.println("\nKindly select a valid operation from the list of available choices.\n");
            }
        } while (cmd != QUITCOMMAND);
    }



    /*
     * Print menu of choices for commands to the butler
     */
    public void printMenu() {
        String[] commands = { "\t(" + LOADCOMMAND  + ") Load database",
                      "\t(" + ADDCOMMAND   + ") Add fact",
                      "\t(" + QUERYCOMMAND + ") Query fact",
                      "\t(" + SAVECOMMAND  + ") Save database",
                      "\t(" + PRINTCOMMAND + ") Print all facts",
                      "\t(" + QUITCOMMAND  + ") Exit the premises" };
        System.out.println();
        for (int i=0; i<commands.length; i++)
            System.out.println(commands[i]);
        System.out.print("\nYour desired operation? ");
    }



    /*
     * Load in a database file
     */
    public void loadFile() {
        String filename 
            = Keyboard.promptString("Name of database file [" 
                        + this.filename + "]: ");
        if (filename.equals("")) filename = this.filename;
        try {
            BufferedReader inFile 
            = new BufferedReader(new FileReader(filename));
            ManorDatabase newdb = new ManorDatabase(); // reading in new data
            newdb.readFacts(inFile);
            inFile.close();
            this.filename = filename;
            db = newdb;
            System.out.println("File loaded. " + db.numOfFacts() 
                       + " fact(s) currently in database.");
        } catch (FileNotFoundException e) {
            System.out.println("The file \"" + filename + "\" was not found. No facts were loaded.");
            // e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Some strange error occurred: " + e);
        }
    }



    /*
     * Save facts database to file
     */
    public void saveFile() {
        String filename 
            = Keyboard.promptString("Name of database file ["
                        + this.filename + "]: ");
        if (filename.equals("")) filename = this.filename;
        try {
            PrintWriter outFile = new PrintWriter(new FileWriter(filename));
            db.saveFacts(outFile);
            outFile.close();
            System.out.println(db.numOfFacts() + " fact(s) stored.");
            this.filename = filename;         // update for next
        } catch (IOException e) {
            System.out.println("The file \"" + filename + "\" could not be written to. No data was saved.");
            // e.printStackTrace();
        }
    }


    /*
     * Query facts from the database
     */
    public void queryFacts() {
        System.out.println("Entering query mode:");
        System.out.println("Enter a fact(s) to query (nounA/relation/nounB - one per line) using a");
        System.out.println("question mark (?) as necessary. Leave first line blank to abort.\n");
        try {
            BufferedReader kybd 
            = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
            Fact query = new Fact(kybd);
            System.out.println();
            db.searchFacts(query);
            System.out.println();
            }
        } catch (IOException e) {
            System.out.println("Query mode aborted.");
        }
    }



    /*
     *
     */
    public void retire() {
        System.out.println("\nThank you for your gracious visit.\n");
    }



    public static void main(String[] args) {
        ElectronicButler parsons = new ElectronicButler();
        parsons.engage();
        parsons.retire();
    }

}
