///////////////////////////////////////////////////////////////////////////////
// Title:            Program 1
// Files:            (list of source files)
// Semester:         CS367 Summer 2014
//
// Author:           Wayne Reeder
// Email:            wreeder@g.clemson.edu
// CS Login:         wreeder2
// Lecturer's Name:  Beck Hasti
//////////////////////////// 80 columns wide //////////////////////////////////

import java.util.*;

/**
 * 
 * 
 * <p>Bugs:
 * 
 * @author Wayne Reeder, CS 367, Copyright 2014
 *
 */
public class CustomerDatabase 
{
	
	private ArrayList<Customer> CustDB;
	private java.lang.IllegalArgumentException IllegalArgumentException;
	
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
	}
	
	/**
	 * Constructs an empty customer database
	 */
	public CustomerDatabase() 
	{
		CustDB = new ArrayList<Customer>();
	}
	
	/**
	 * Adds a customer to the customer database if the 
	 * username, c, is not already taken.
	 * 
	 * @param c the username of the customer
	 */
	public void addCustomer (String c) 
	{
		int len = c.length();	
		c = c.toLowerCase();
		
		if (len < 1) 
		{
			throw IllegalArgumentException;
		}
		
		if (!containsCustomer(c)) 
		{
			Customer tempCustomer;
			tempCustomer = new Customer(c);
			CustDB.add(tempCustomer);
		}
		return;		
	}
	
	/**
	 * adds the product, p, to the customer, c.
	 * 
	 * @param c the username of the customer
	 * @param p the product to add
	 */
	public void addProduct (String c, String p) 
	{
		Customer customerInfo;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		Boolean found = false;
		c = c.toLowerCase();
		p = p.toLowerCase();
				
		
		if (!containsCustomer(c))
		{
			throw IllegalArgumentException;
		}
		
		while (iterCustomers.hasNext() && !found)
		{
			customerInfo = iterCustomers.next();
			if (customerInfo.getUsername().equals(c))
			{
				List<String> wishList;
				wishList = customerInfo.getWishlist();
				wishList.add(p);
				found = true;
			}
		}
		
		
		return;
	}
	
	/**
	 * 
	 * @param c the customer username
	 * @return true if Customer Database has a customer with username, c
	 * 	contained in String c, false if it does not.
	 */
	public boolean containsCustomer (String c) 
	{
		Customer customerInfo;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		Boolean found = false;
		c = c.toLowerCase();
		
		while (iterCustomers.hasNext() && !found)
		{
			customerInfo = iterCustomers.next();
			found = customerInfo.getUsername().equals(c);
		}
		
		return found;
	}
	
	/**
	 * 
	 * @param p the product that is contained
	 * @return true if CustomerDatabase already contains 
	 */
	public boolean containsProduct (String p) 
	{
		Customer customerInfo;
		List<String> wishlist;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		Boolean found = false;
		p = p.toLowerCase();
		
		while (iterCustomers.hasNext() && !found)
		{
			customerInfo = iterCustomers.next();
			wishlist = customerInfo.getWishlist();
			found = wishlist.contains(p);
		}
		
		return found;
	}
	
	/**
	 * 
	 * @param c the customer username
	 * @param p the product that we are interested in
	 * @return true if customer, c, has product, p.
	 */
	public boolean hasProduct(String c, String p)
	{
		Customer customerInfo;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		Boolean found = false;
		c = c.toLowerCase();
		p = p.toLowerCase();
		
		while (iterCustomers.hasNext() && !found)
		{
			customerInfo = iterCustomers.next();
			if (customerInfo.getUsername().equals(c))
			{
				found = customerInfo.getWishlist().contains(p);
			}
			
		}
		
		return found;
	}
	
	/**
	 * return a list of customers that all have a given product
	 * 
	 * @param p the product we are interested in
	 * @return a list of customers with a given product, p.
	 */
	public List<String> getCustomers(String p) 
	{
		Customer customerInfo;
		List<String> customers = new ArrayList<String>();
		Iterator<Customer> iterCustomers = CustDB.iterator();
		p = p.toLowerCase();
		
		while (iterCustomers.hasNext())
		{
			customerInfo = iterCustomers.next();
			Iterator<String> iterWishList = customerInfo.getWishlist().iterator();
			while(iterWishList.hasNext())
			{
				if(iterWishList.next().equals(p))
				{
					customers.add(customerInfo.getUsername());
				}
			}
			
		}
		
		return customers;
	}
	
	/**
	 * return a list of products on a given customers wishlist
	 * 
	 * @param c the customer username
	 * @return a list of products that customer c has
	 */
	public List<String> getProducts(String c) 
	{
		Customer customerInfo;
		List<String> products = null;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		c = c.toLowerCase();
		
		while (iterCustomers.hasNext())
		{
			customerInfo = iterCustomers.next();
			
			if (customerInfo.getUsername().equals(c))
			{
				products = customerInfo.getWishlist();
			}
		}
		
		return products;
	}
	
	/**
	 * returns an iterator of the customer type that is used
	 * to iterate over the customer database
	 * 
	 * @return iterator of customer type
	 */
	public Iterator<Customer> iterator() 
	{
		return CustDB.iterator();
	}
	
	/**
	 * removes a customer from the customer database
	 * 
	 * @param c customer username to remove
	 * @return true if the customer was removed successfully
	 */
	public boolean removeCustomer(String c) 
	{
		boolean found = false;
		Customer customerInfo;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		c = c.toLowerCase();
		
		while(iterCustomers.hasNext() && !found)
		{
			customerInfo = iterCustomers.next();
			
			if (customerInfo.getUsername().equals(c))
			{
				iterCustomers.remove();
				found = true;
			}
		}
		
		return found;
	}
	
	/**
	 * method removes a given product from all customer's wishlists
	 * 
	 * @param p the product to remove
	 * @return true if the product was removed successfully
	 */
	public boolean removeProduct(String p) 
	{
		boolean found = false;
		Customer customerInfo;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		p = p.toLowerCase();
		
		while(iterCustomers.hasNext() && !found)
		{
			customerInfo = iterCustomers.next();
			if (customerInfo.getWishlist().contains(p))
			{		
				List<String> wishList = customerInfo.getWishlist();				
				Iterator<String> iterWishList;
				iterWishList = wishList.iterator();
				boolean removed = false;
						
				while (iterWishList.hasNext() && !removed)
				{
					String tempProduct = iterWishList.next();
					
					if (tempProduct.equals(p))
					{
						iterWishList.remove();
						removed = true;
					}
				}
			}
		}	
		
		return found;
	}
	
	/**
	 * returns the number of customers in the customer database
	 * 
	 * @return the number of customers in the database
	 */
	public int size() 
	{
		Integer size = 0;
		Iterator<Customer> iterCustomers = CustDB.iterator();
		
		while(iterCustomers.hasNext())
		{
			size = size + 1;
			iterCustomers.next();
		}
		
		return size;
	}
	
}

 