import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class InteractiveDBTester {
    public static void main(String[] args) throws FileNotFoundException {

        // *** Add code for steps 1 - 3 of the main method ***
    	
    
        //check whether exactly one command line argument is given
    	Scanner standardIn = new Scanner(System.in);
        String fileName = standardIn.nextLine();
        if (fileName.contains(" "))
        {
        	System.out.println("Please provide input file as command-line argument");
        	System.exit(1);
        }
        
        
        //check whether input file exists and is readable
        File srcFile = new File(fileName);
        Scanner fileIn = new Scanner(srcFile);	
        
        if (!srcFile.exists() || !srcFile.canRead())
        {
        	System.out.println("Error: Cannot access input file");
        	System.exit(1);
        }
        
        //load data from input file and construct customer database
        CustomerDatabase CustDB = new CustomerDatabase();
        String delims = ",+";
        while (fileIn.hasNext())
        {
        	String inputLine = fileIn.next();
        	String[] tokens = inputLine.split(delims);
        	for (int i = 0; i < tokens.length; i++)
        	{
        		if (i == 0)
        		{
        			CustDB.addCustomer(tokens[0]);
        		}
        		else
        		{
        			CustDB.addProduct(tokens[0], tokens[i]);
        		}
        	}
        }
        fileIn.close();
        //standardIn.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) {
                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':
                    // *** Add code to implement this option ***
                	Boolean discontinued = false;
                	discontinued = CustDB.removeProduct(remainder);
                	if (discontinued)
                	{
                		System.out.println("product discontinued");
                	}             	
                	else
                	{
                		System.out.println("product not found");
                	}
                    break;

                case 'f':
                    // *** Add code to implement this option ***
                	Boolean foundCustomer = false;
                	foundCustomer = CustDB.containsCustomer(remainder);
                	
                	if (!foundCustomer)
                	{
                		System.out.println("customer not found");
                	}
                	else
                	{
                		List<String> products = CustDB.getProducts(remainder);
                		int i = 0;
                		Iterator<String> iterProducts = products.iterator();
                		System.out.print(remainder+":");
                		
                		while (iterProducts.hasNext())
                		{
                			i = i + 1;
                			if (i == 1)
                			{
                				System.out.print(iterProducts.next());
                			}
                			else
                			{
                				System.out.print("," + iterProducts.next());
                			}
                		}	
                		System.out.println();
                	}
                	
                    break;

                case 'h': 
                    printOptions();
                    break;

                case 'i':
                    // *** Add code to implement this option ***
                	int numCustomers = 0;
                	int numProducts = 0;
                	int mostProducts = 0;
                	int leastProducts = 0;
                	int avgProducts = 0;
                	int mostCustomers = 0;
                	int leastCustomers = 0;
                	int avgCustomers = 0;
                	
                	ArrayList<String> uniqueProducts = new ArrayList<String>();
                	Customer customerInfo;
                	List<String> tempWishList;
                   	Iterator<Customer> iterCust = CustDB.iterator();
                   	
                	while (iterCust.hasNext())
                	{
                		numCustomers = numCustomers + 1;
                		customerInfo = iterCust.next();
                		tempWishList = customerInfo.getWishlist();
                		Iterator<String> iterWishList = tempWishList.iterator();
                		
                		while (iterWishList.hasNext())
                		{
                			String product = iterWishList.next();
                			Boolean found = false;
                			Iterator<String> iterUnique = uniqueProducts.iterator();
                			                			                			
                			while (iterUnique.hasNext() && !found)
                			{
                				found = iterUnique.next().equals(product);
                			}
                			
                			if (!found)
                			{
                				uniqueProducts.add(product);
                				numProducts = numProducts + 1;
                			}
                		}
                		
                	}
                	
                	System.out.print("Customers: " + numCustomers);
                	System.out.print(" Products: " + numProducts);
                	
                	
                	System.out.println();
                	System.out.print("# of products/customer: most " + mostProducts);
                	System.out.print(", least " + leastProducts + ", average " + avgProducts);
                	System.out.println();
                	
                	System.out.print("# of customers/product: most " + mostCustomers);
                	System.out.print(", least " + leastCustomers + ", average " + avgCustomers);
                	System.out.println();
                	
                	
                    break;
                    
                case 's':
                    // *** Add code to implement this option ***
                	Boolean foundProduct = false;
                	foundProduct = CustDB.containsProduct(remainder);
                	
                	if (!foundProduct)
                	{
                		System.out.println("product not found");
                  	}
                	else
                	{
                		List<String> customers = CustDB.getCustomers(remainder);
                		int i = 0;
                		Iterator<String> iterCustomer = customers.iterator();
                		System.out.print(remainder + ":");
                		
                		while (iterCustomer.hasNext())
                		{
                			i = i + 1;
                			if (i == 1)
                			{
                				System.out.print(iterCustomer.next());
                			}
                			else
                			{
                				System.out.print("," + iterCustomer.next());
                			}
                		}
                		System.out.println();
                	}
                	
                    break;

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

                case 'r':
                    // *** Add code to implement this option ***
                	Boolean removedCustomer = false;
                	removedCustomer = CustDB.removeCustomer(remainder);
                	
                	if(!removedCustomer)
                	{
                		System.out.println("customer not found");
                	}
                	else
                	{
                		System.out.println("customer removed");
                	}
                	
                    break;

                default:  // ignore any unknown commands
                    break;
                }
            }
        }
        
        stdin.close();
        standardIn.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>");
    }
}
