import NetworkConnection;
import Mailbox;
import MailMessage;
import SetPrefsStub;
import SimpleOutput;
import SimpleInput;
import TextCompose;
/**
 *  
 * Takes line of input at a prompt and does stuff with it.
 *
 * parameters: reads a string from an input
 * preconditions: a string to read
 * Post:
 * problems/exceptions
 * produces:  
 * 
 * @author Paul Carlson
 *
 * @version 1.0 of November 1999
 *
 * 11/8:  Created
 * 11/18: Started integrating Mailbox class.
 * 11/21: It works!  Sort of.
 *  
 */

public class Prompter {
    // +--------+--------------------------------------------------
    // | Fields |
    // +--------+
    
    protected String command;

    
    // +--------------+--------------------------------------------
    // | Constructors |
    // +--------------+

    protected Prompter(Mailbox box) {

        SimpleInput in=new SimpleInput();
        SimpleOutput out=new SimpleOutput();
        try {
        out.print("This is a prompt: ");
        command=in.readString();
        if (command.equalsIgnoreCase("address")) {
            out.println("this is your address book.");
            new Prompter(box);
        } // if (address)

        else if (command.equalsIgnoreCase("list")) {
            out.print("Possible commands:  Address (Address book)\n"+
                      "                    new     (New Mail)\n"+
                      "                    new folder  (Create a new folder)\n"+ 
                      "                    delete folder   (Delete a folder)\n"+ 
                      "                    inbox   (All your mail)\n"+
                      "                    quit    (Exit TUI)\n"+
                      "                    create  (Make a new message)\n"+
                      "                    index (See all your folders)\n"+
                      "                    options (Change your options)\n"+
                      "                    list    (see these options again)"+ 
                      "+\n\n");
            new Prompter(box);
        } //if (list)

        else if (command.equalsIgnoreCase("options")) {
            out.println("this is where you change your options.");
            SetPrefsStub prefs = new SetPrefsStub("username");
            prefs.listOptions();
            new Prompter(box); 
        } // if (options)

        else if (command.equalsIgnoreCase("new folder")) {
            out.println("What would you like to name your folder? ");
            String foldername = in.readString();
            box.createFolder(foldername);
            new Prompter(box); 
        } // if (new folder)

        else if (command.equalsIgnoreCase("delete folder")) {
            out.println("Which folder would you like to delete? ");
            String foldername = in.readString();
            box.deleteFolder(foldername);
            new Prompter(box); 
        } // if (delete folder)

        else if (command.equalsIgnoreCase("inbox")) {
            box.setFolder("inbox");
            String[] messages;
            messages = box.getFolderContents();
            out.println("this is your inbox.");
            //list the messages
            for (int i=1; i<=(messages.length-1); i++) {
                out.println(messages[i]); 
            } // for
            new Prompter(box); 
        } // if (inbox)

        else if (command.equalsIgnoreCase("new")) {
            out.println("these are your new messages.");
            String[] messages;
            messages = box.getFolderContents();
            
            for (int i=1; i<=(messages.length-1); i++) {        
                //something like this....
                //                if (newFlag==true)
                //                    out.println(messages);
            } //for
            new Prompter(box); 
        } // if(new)
        
        else if (command.equalsIgnoreCase("index")) {
            out.println("these are all your folders.");
            out.println(box.listFolders());
            new Prompter(box); 
        } // if (index)
        

        else if (command.equalsIgnoreCase("create")) {
            out.println("this is you, creating a message.");
            TextCompose letter = new TextCompose();
            out.println("Is this letter ready to go? (y/n)");
            String answer = in.readString();
            if (answer.equalsIgnoreCase("y")) {
                //do something to send it
                //sendmessage(letter);
                out.println("Your message has been sent.  (Not really.)\n");
            } // if
            else {
                out.println("Message is cancelled.\n");
            } //else
            new Prompter(box); 
        } // if (create)

        else if ((command.equalsIgnoreCase("quit")) || 
                 (command.equalsIgnoreCase("exit")))  {
            out.println("this is you, quitting.");
            
            // save options before exiting.
            
            box.bye();
            System.exit(0);
        } // if (quit)
        
        
        else {
            out.println("incorrect command.  Try again.");
            out.println("\n\nPossible commands:  Address (Address book)\n                    new     (New Mail)\n                    new folder   (Create a new folder)\n                    delete folder   (Delete a folder)\n                    inbox   (All your mail)\n                    quit    (Exit TUI)\n                    create  (Make a new message)\n                    index (See all your folders)\n                    options (Change your options)\n                    list    (see these options again)\n\n");
            new Prompter(box); 
        } //else
        } //try
        catch (Exception e) {
            out.println("Something messed up in prompter");
        } // catch
    } //Prompter(Mailbox box)
    
} // Prompter class

