package cpsc471.ftp.client.control;

import org.apache.log4j.Logger;

import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Interactive shell for ftp client
 */
public class Shell {

    private static final Logger logger = Logger.getLogger(Shell.class);

    // region Static Methods: main, usage

    public static void usage() {

        System.out.println(
                "Usage:\n" +
                        "cli <domain name> <port>"
        );
    }

    public static void main(String args[]) {

        if(args.length != 2) {
            usage();
        }
        else {
            try {
                String domainName = args[0];
                short port = Short.parseShort(args[1]);
                ControlClient client = new ControlClientImpl(domainName, port);

                Shell shell = new Shell(client);
                shell.run();
            }
            catch (NumberFormatException e) {
                // thrown if port isn't a number or a short
                System.err.println("<port> must be a 16-bit integer");
            }
            catch (Exception e) {
                System.err.println("An unexpected error occurred: "
                        + e.getMessage());
                logger.error(e.getClass().getName() + " " + e.getMessage());
            }
        }
    }

    // endregion

    // region Shell instance methods

    private ControlClient client;

    private static final String HELP_GET =  "get    : get <remote file>\n" +
                                            "           download a file\n";
    private static final String HELP_LS =   "ls     : ls\n" +
                                            "           list files on remote system\n";
    private static final String HELP_PUT =  "put    : put <local file>\n" +
                                            "           upload a file\n";
    private static final String HELP_QUIT = "quit   : quit\n" +
                                            "           exits the program\n";

    /**
     * Create an interactive ftp shell
     * @param client a client with an open connection to a server
     */
    public Shell(ControlClient client) {

        this.client = client;
    }

    /**
     * Runs the interactive ftp loop
     */
    public void run() {

        Scanner inputReader = new Scanner(System.in);
        while(true) {
            System.out.print("ftp> "); // prompt

            // get user input
            String[] input = inputReader.nextLine().split(" ");

            if(invokeCmd(input) == false) {
                break;
            }
        }
    }

    /**
     * Attempt to invoke the command entered by the user
     * @param args args to pass to command, including the command itself
     * @return true if the run loop should continue
     */
    public boolean invokeCmd(String[] args) {

        String cmd = args[0];

        boolean continueExec = true;

        switch(cmd) {
            case "ls":
                client.ls();
                break;
            case "get":
                handleGet(args);
                break;
            case "put":
                handlePut(args);
                break;
            case "quit":
                // close connection to server
                client.quit();
                // exit process
                continueExec = false;
                break;
            default: // unknown command; print help
                help();
                break;
        }
        return continueExec;
    }

    /**
     * Handles the "get" function
     * @param args arguments to pass to get
     */
    public void handleGet(String[] args) {

        if(args.length == 2) {
            client.get(args[1]);
        }
        else {
            System.out.println(HELP_GET);
        }
    }

    /**
     * Handles the put function
     * @param args arguments to pass to put
     */
    public void handlePut(String[] args) {

        if(args.length == 2) {
            try {
                client.put(args[1]);
            }
            catch (FileNotFoundException e) {
                System.err.println(
                        "Unable to upload \"" + args[1] + "\"; " +
                        "unable to find file."
                );
            }
        }
        else {
            System.out.println(HELP_PUT);
        }
    }

    /**
     * Print a list of commands and options
     */
    public void help() {

        System.out.printf(
                "Available commands:\n" +
                        HELP_GET +
                        HELP_LS +
                        HELP_PUT +
                        HELP_QUIT
        );
    }

    // endregion
}

@@ -101,16 +101,54 @@
 eption;%0A
+import java.net.UnknownHostException;%0A
 import j
@@ -631,38 +631,16 @@
  else %7B%0A
-            try %7B%0A    
         
@@ -672,16 +672,34 @@
 rgs%5B0%5D;%0A
+            try %7B%0A
         
@@ -1129,16 +1129,27 @@
  catch (
+UnknownHost
 Exceptio
@@ -1147,32 +1147,33 @@
 tException e) %7B%0A
+%0A
                 
@@ -1196,157 +1196,46 @@
 ln(%22
-An unexpected error occurred: %22%0A                        + e.getMessage());%0A                logger.error(e.getClass().getName() + %22 %22 + e.getMessage()
+Unable to resolve host: %22 + domainName
 );%0A 

Handle host not found in main