///////////////////////////////////////////////////////////////////////////////
//                   ALL STUDENTS COMPLETE THESE SECTIONS
// Main Class File:  (InteractiveDBTester.java
// File:             CustomerDatabase.java
// Semester:         CS367-002 Summer 2014
//
// Author:           James Maciejewski - james.m.maciejewski@gmail.com
// CS Login:         maciejewski2
// Lecturer's Name:  Beck Hasti
//
//////////////////////////// 80 columns wide //////////////////////////////////

import java.util.*;

/**
 * The CustomerDatabase class sotres a collection of customers and includes 
 * methods for managing the collection
 *
 * @author James Maciejewski
 */public class CustomerDatabase {

	private List<Customer> myDatabase;
	
	/**
	 * Constructs a new customer database
	 */
	public CustomerDatabase() {
		myDatabase = new ArrayList<Customer>();
	}
	
	/**
	 * add a customer with the specified name to the database
	 * 
	 * @param c the customer's name
	 */
	public void addCustomer(String c) {
		
		if (c == null) {
			throw new IllegalArgumentException();
		}
		
		c = c.toLowerCase();
		
		if (containsCustomer(c)) {
			return;
		}
		
		Customer newCustomer = new Customer(c);
		myDatabase.add(newCustomer);
	}

	/**
	 * Add the specified product to the customer's wishlist
	 * 
	 * @param c the customer's name
	 * @param p the product to add to the customer's wishlist
	 */	
	public void addProduct(String c, String p) {

		if ((c == null) || (p == null)){
			throw new IllegalArgumentException();
		}
	
		c = c.toLowerCase();
		p = p.toLowerCase();
			
		
		Customer customer = findCustomer(c);
		if (customer == null) {
			throw new IllegalArgumentException();
		}
		
		if (! customer.getWishlist().contains(p)) {
			customer.getWishlist().add(p);
		}
						
	}
		
	/**
	 * Check if the database contains the a customer with
	 * the specified name
	 * 
	 * @param c the customer to search for in the database
	 * @return true iff the customer is in the database
	 */
	public boolean containsCustomer(String c) {
		
		if (c == null){
			throw new IllegalArgumentException();
		}
	
		c = c.toLowerCase();
		
		Customer customer = findCustomer(c);
		return (customer != null);
	}
	
	/**
	 * Check if any customer int he database has the specified priduct in 
	 * their wishlist
	 * 
	 * @param p the product to search for
	 * @return true iff some customer has the product in their wishlist
	 */
	public boolean containsProduct(String p) {
		
		if (p == null) {
			throw new IllegalArgumentException();
		}
		
		p = p.toLowerCase();
		
		Iterator<Customer> customerIter = myDatabase.iterator();
		Customer currCustomer;
		
		while (customerIter.hasNext()) {
			currCustomer = customerIter.next();
			if (currCustomer.getWishlist().contains(p)) {
				return true;
			}
		}
				
		return false;
	}
	
	/**
	 * Check if the customer with name c ha product p in their wishlist
	 * 
	 * @param c the customer to check
	 * @param p the product to check for
	 * @return true iff the customer has the product
	 */
	public boolean hasProduct(String c, String p) {
		
		if ((c == null) || (p == null)) {
			throw new IllegalArgumentException();
		}

		c = c.toLowerCase();
		p = p.toLowerCase();

		Customer customer = findCustomer(c);
		if (customer == null) {
			return false;
		}
		
		if (customer.getWishlist().contains(p)) {
			return true;
		}
		
		return false;
		
	}

	/**
	 * Return a list of customer that have the specified product in 
	 * their wishlists
	 * 
	 * @param p the product to search for
	 * @return A list of customer that have the product in their wishlists
	 */
	public List<String> getCustomers(String p) {
		
		if (p == null) {
			throw new IllegalArgumentException();
		}
		
		p = p.toLowerCase();

		List<String> customerList = new ArrayList<String>();
		Iterator<Customer> customerIter = myDatabase.iterator();
		
		while (customerIter.hasNext()) {
			Customer currCustomer=customerIter.next();
			if (currCustomer.getWishlist().contains(p)) {
				customerList.add(currCustomer.getUsername());
			}
		}
		
		if (customerList.size() == 0) {
			return null;
		}
		
		return customerList;
		
	}

	public List<String> getProducts(String c) {
		
		if (c == null) {
			throw new IllegalArgumentException();
		}
		
		c = c.toLowerCase();

		Customer currCustomer = findCustomer(c);
		
		if (currCustomer == null) {
			return null;
		}
			
		return currCustomer.getWishlist();
	}

	public Iterator<Customer> iterator() {
		Iterator<Customer> iter = myDatabase.iterator();
		return iter;
	}

	public boolean removeCustomer(String c){
		
		if (c == null) {
			throw new IllegalArgumentException();
		}

		c = c.toLowerCase();

		Customer currCustomer = findCustomer(c);
		
		if (currCustomer == null) {
			return false;
		}
		
		return myDatabase.remove(currCustomer);
		
	}
	
	public boolean removeProduct(String p) {
		
		if (p == null) {
			throw new IllegalArgumentException();
		}
		
		p = p.toLowerCase();

		Iterator<Customer> customerIter = myDatabase.iterator();
		boolean isRemoved = false;
		
		while (customerIter.hasNext()) {
			Customer currCustomer = customerIter.next();
			isRemoved = (currCustomer.getWishlist().remove(p) || isRemoved);
		}
		
		return isRemoved;
	}
	
	public int size() {
		return myDatabase.size();
	}
	
 	private Customer findCustomer(String c) {
		Iterator<Customer> iter = myDatabase.iterator();
		Customer customer = null;
		boolean isFound = false;
	
		while (iter.hasNext()) {
			customer = iter.next();
			if (customer.getUsername().equals(c)) {
				isFound = true;
				break;
			}
		}
		
		if (! isFound) {
			customer = null;
		}
		
		return customer;
	}
	
	
}
