///////////////////////////////////////////////////////////////////////////////
//                   ALL STUDENTS COMPLETE THESE SECTIONS
// Title:            CustomerWIshListDatabase
// Files:            InteractiveDBTester.java
//					 CustomerDatabase.java
//					 Customer.java					 
// Semester:         CS367 Summer
//
// Author:           Dan Willmer
// Email:            dan.willmer@gmail.com
//
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class InteractiveDBTester {
    public static void main(String[] args) throws FileNotFoundException {
    	CustomerDatabase db = null;
    	if (args != null){
    		db = buildDB(args[0]);
    	}
    	else{
    		System.out.println("Please provide input file as command-line argument");
    		//quit
    	}
    		

        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) {
                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) {
                
                case 'd':
                    // 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".
                    if (db.removeProduct(remainder))
                    	System.out.println("Product discontinued");
                    else
                    	System.out.println("Product not found.");
                	break;


                case 'f':
                    // 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
                    if (db.containsCustomer(remainder)){
                    	//display customer
                    	String s = remainder + ":";
                    	List<String> wishlist = db.getProducts(remainder);
                    	for(int i = 0; i < wishlist.size(); i++){
                    		s = s + "," + wishlist.get(i);
                    	}
                    	System.out.println(s);
                    	
                    }
                    else
                    	System.out.println("Customer not found.");
                	break;

                case 'h': 
                    printOptions();
                    break;

                case 'i':
                    // *** Add code to implement this option ***
                	System.out.println(db.BasicCount());
                	System.out.println(db.CustomersPerProductStats());
                	System.out.println(db.ProductsPerCustomerStats());
                	System.out.println(db.MostPopularProduct());
                    break;
                    
                case 's':
                    // 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
                	if (db.containsProduct(remainder)){
                		String s = remainder + ":";
                		List<String> customers = db.getCustomers(remainder);
                    	for(int i = 0; i < customers.size(); i++){
                    		s = s + "," + customers.get(i);
                    	}
                    	System.out.println(s);
                	}
                	else
                		System.out.print("Product not found");
                    break;

                case 'q':
                    done = true;
                    System.out.println("quit");
                    break;

                case 'r':
                    // If customer is not in the database, display "customer not found". 
                	//Otherwise, remove customer and display "customer removed".
                	if (db.removeCustomer(remainder))
                		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>");
    }
    private static CustomerDatabase buildDB(String fileName) throws FileNotFoundException{
    	// Takes the file provided as a command line argument and populates a CustomerDatabase with the data
    	CustomerDatabase db = new CustomerDatabase();
    	File srcFile = new File(fileName);
    	Scanner fileIn = new Scanner(srcFile);
    	String[] items;
    	String delims = "[,]+";
    	while (fileIn.hasNext()){
    		items = fileIn.nextLine().split(delims);
    		db.addCustomer(items[0]);
    		System.out.println(items[0]);
    		System.out.println(items.length);
    		for(int x = 1; x < items.length; x++){
    			db.addProduct(items[0], items[x]);
    		}
    	}
    	return db;
    }

}