package program1;

import java.util.*;
import java.io.*;
/**
 * The InteractiveDBTester class is used to load a (specially formatted .txt 
 * file and build out a CustomerDatabase object.
 * 
 * It also has various command line options, that allow interaction and 
 * manipulation of the created CustomerDatabase object.
 * 
 * @author Stefen Showers
 */

public class InteractiveDBTester {
    public static void main(String[] args) throws FileNotFoundException {
    	
    	//Load the data from the input file and use it to construct a customer database.
    	//use the .split() string method to read line break and comma delimiters
    	
    	//CustomerDatabase db = new CustomerDatabase();
    	//String line;  //for the lines in the input file

    	
    	//UNFINISHED: Check whether the input file exists and is readable; if not, display "Error: Cannot access input file" and quit.
    	
    	File srcFile = new File("sampleinput.txt");
    	Scanner fileIn = new Scanner(srcFile);
    	CustomerDatabase db = new CustomerDatabase();
    	while(fileIn.hasNext()){
        	String line = fileIn.nextLine();
            String c = line.split(",",2)[0];
            db.addCustomer(c);
            String pline = line.split(",",2)[1];	
    		String[] tokens = pline.split(",");
    		for (int i = 0; i < tokens.length; i++){
    			String p = tokens[i];
    			db.addProduct(c, p);
    		}
    	}
    	
    	
    	fileIn.close();
    	
        Scanner stdin = new Scanner(System.in);  // for reading console input
        printOptions();
        boolean done = false;
        while (!done) {
            System.out.print("Enter option ( dfhisqr ): ");
            String input = stdin.nextLine();
            input = input.toLowerCase();  // convert input to lower case

            // only do something if the user enters at least one character
            if (input.length() > 0) {
            	if (input.length() > 1) {
            		System.out.print("Please provide input file as command-line argument.");
            		done = true;
                    break;
            	}
                char choice = input.charAt(0);  // strip off option character
                String remainder = "";  // used to hold the remainder of input
                if (input.length() > 1) {
                    // trim off any leading or trailing spaces
                    remainder = input.substring(1).trim(); 
                }

                switch (choice) {
                
                /**
                 * If product is not in the database, 
                 * display "product not found". Otherwise, discontinue product 
                 * (i.e., remove the product from all the wish lists in which it 
                 * appears) and display "product discontinued".
                 */
                case 'd':
                	Scanner ind = new Scanner(System.in);
                	String d = ind.nextLine();
                    d = d.toLowerCase();
                	if (db.containsProduct(d)){
                		db.removeProduct(d);
                		System.out.println("Product discontinued.");
                	}
                	else{
                		System.out.println("Product not found.");
                	}
                    break;

                /**
                 * If customer is not in the database, display "customer not found". 
                 * Otherwise, find customer and display the customer (on one line) 
                 * in the format: customer:product1,product2,product3
                 */
                case 'f':
                	Scanner inf = new Scanner(System.in);
                	String f = inf.nextLine();
                    f = f.toLowerCase();
                    if (db.containsCustomer(f)){
                    	System.out.println(f + ":" + db.getProducts(f));
                    }
                    else{
                    	System.out.println("Customer not found.");
                    }
                    break;
                
                /**
                 * Provide help by displaying the list of command options.
                 */
                case 'h': 
                    printOptions();
                    break;

                /**
                 * Display information about this database
                 */
                case 'i':
                	double pcaverage = (double)db.size()/db.allMetrics()[0]; 
                	double cpaverage = (double)db.allMetrics()[0]/db.size(); 
                	System.out.println("Customers: " + db.size() + ", Products: " + db.allMetrics()[0]);
                    System.out.println("# of products/customer: most " + db.allMetrics()[1] + ", least " + db.allMetrics()[2] + ", average " + pcaverage);
                    System.out.println("# of customers/product: most " + db.allMetrics()[3] + ", least " + db.allMetrics()[4] + ", average " + cpaverage);
                    System.out.println("Most popular product(s): " + db.popProducts(db.allMetrics()[3]) + " [" + db.allMetrics()[3] + "]");
                    break;
                    
                /**
                 * If product is not in the database, display "product not found". 
                 * Otherwise, search for product and display the product along 
                 * with the customers who have that product in their wish list 
                 * (on one line) in the format: product:customer1,customer2,customer3
                 */
                case 's':
                	Scanner ins = new Scanner(System.in);
                	String s = ins.nextLine();
                    s = s.toLowerCase();
                    if (db.containsProduct(s)){
                    	System.out.println(s + ":" + db.getCustomers(s));
                    }
                    else{
                    	System.out.println("Product not found.");
                    }
                    
                    break;

                /**
                 * Display "quit" and quit the program.
                 */
                case 'q':
                    done = true;
                    System.out.println("quit");
                    break;

                /**
                 * If customer is not in the database, display "customer not found". 
                 * Otherwise, remove customer and display "customer removed".
                 */
                case 'r':
                	Scanner inr = new Scanner(System.in);
                	String r = inr.nextLine();
                    r = r.toLowerCase();
                    if (db.containsCustomer(r)){
                    	db.removeCustomer(r);
                    	System.out.println("Customer removed.");
                    }
                    else{
                    	System.out.println("Customer not found.");
                    }
                    break;

                default:  // ignore any unknown commands
                    break;
                }
            }
        }
        
        stdin.close();
    }

    /**
     * Prints the list of command options along with a short description of
     * one.  This method should not be modified.
     */
    private static void printOptions() {
        System.out.println("d <product> - discontinue the given <product>");
        System.out.println("f <customer> - find the given <customer>");
        System.out.println("h - display this help menu");
        System.out.println("i - display information about this customer database");
        System.out.println("s <product> - search for the given <product>");
        System.out.println("q - quit");
        System.out.println("r <customer> - remove the given <customer>");
    }
}