package com.ferienclub.custadmin.beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * This class is just a container for CustomerPreference objects with some 
 * specialized gettermethods.
 * 
 * @author Sven Stegelmeier
 */
public class CustomerPreferences {

	private List items;
	private String userId;
	
	public CustomerPreferences(List items, String userId){
		this.items = items;
		this.userId = userId;
	}
	
	public CustomerPreferences(List items){
		this.items = items;
	}
	
	public CustomerPreferences(){
		this.items = new ArrayList();
	}
	
	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public CustomerPreference getHighestPreference(){
		CustomerPreference highest = null, current = null;
		for (Iterator itor = items.iterator(); itor.hasNext();){
			current = (CustomerPreference)itor.next();
			if (highest == null) highest = current;
			else {
				if (highest.getValue() < current.getValue()) highest = current;
			}
		}
		return highest;
	}
	
	public CustomerPreference getLowestPreference(){
		CustomerPreference lowest = null, current = null;
		for (Iterator itor = items.iterator(); itor.hasNext();){
			current = (CustomerPreference)itor.next();
			if (lowest == null) lowest = current;
			else {
				if (lowest.getValue() > current.getValue()) lowest = current;
			}
		}
		return lowest;
	}
	
	public List getPreferencesByValue(int value){
		List prefs = new ArrayList();
		for (Iterator itor = items.iterator(); itor.hasNext();){
			CustomerPreference pref = (CustomerPreference)itor.next();
			if (pref.getValue() == value) prefs.add(pref);
		}
		return prefs;
	}
	
	public CustomerPreference getPreferenceByName(String name){
		for (Iterator itor = items.iterator(); itor.hasNext();){
			CustomerPreference pref = (CustomerPreference)itor.next();
			if (pref.getName().equals(name)) return pref;
		}
		return null;
	}
	
	public void addPreference(CustomerPreference pref){
		this.items.add(pref);
	}
	
	public void removePreference(CustomerPreference pref){
		this.items.remove(pref);
	}
	
	public Iterator iterator(){
		return items.iterator();
	}
	
	/*
	 * For testing purposes.
	 */
	public static void main(String[] args){
		CustomerPreference pref1 = new CustomerPreference("Wintersport",1);
		CustomerPreference pref2 = new CustomerPreference("Ballsport",2);
		CustomerPreference pref3 = new CustomerPreference("Essengehen",3);
		CustomerPreference pref4 = new CustomerPreference("Wandertouren",4);
		CustomerPreference pref5 = new CustomerPreference("Sightseeing",4);
		CustomerPreferences prefs = new CustomerPreferences();
		prefs.addPreference(pref1);
		prefs.addPreference(pref2);
		prefs.addPreference(pref3);
		prefs.addPreference(pref4);
		prefs.addPreference(pref5);
		
		System.out.println("Lowest Preference: " + prefs.getLowestPreference().getName() + " --> " + prefs.getLowestPreference().getValue());
		System.out.println("HighestPreference: " + prefs.getHighestPreference().getName() + " --> " + prefs.getHighestPreference().getValue());
		System.out.println("Preferences by Name \"Essengehen\" : " + prefs.getPreferenceByName("Essengehen").getValue());
		System.out.println("Preferences by value 4 : " + prefs.getPreferencesByValue(4).size());
	}
	
}
