package com.ferienclub.beachclub;

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

import org.apache.log4j.Logger;
import org.mule.impl.UMODescriptorAware;
import org.mule.umo.UMODescriptor;
import org.mule.umo.UMOMessage;

import com.ferienclub.beachclub.esb.BeachClubService;
import com.ferienclub.beachclub.esb.messages.MenuItem;
import com.ferienclub.beachclub.esb.messages.MenuRequest;
import com.ferienclub.beachclub.esb.messages.OrderRequest;

public class BeachClub implements UMODescriptorAware {

	Logger log = Logger.getLogger(BeachClub.class);
	
	private List theMenu; 	
	
	public MenuRequest getMenu(MenuRequest request){
		System.out.println("\n\nBeachClub\n\n");
		request.setMenu(theMenu);
		return request;
	}
	
	public void makeOrder(OrderRequest request){
		log.info("Entering method makeOrder(OrderRequest request)");
		if (checkRequest(request.getOrder())){
			log.info("OK, the drink is ordered for User " + request.getUser().getUserId() + " and will be delivered to position: " + request.getPos());
		} else {
			log.info("\nSorry, this item is not on the menu!\n");
		}
	}
	
	private boolean checkRequest(MenuItem item){
		for (Iterator itor = theMenu.iterator(); itor.hasNext();){
			MenuItem i = (MenuItem)itor.next();
			if (i.equals(item)) return true;
		}
		return false;
	}
	
	public void setDescriptor(UMODescriptor descriptor) {
		Map props = descriptor.getProperties();
		theMenu = new ArrayList();
		for (Iterator itor = props.entrySet().iterator(); itor.hasNext();){
			Map.Entry entry = (Map.Entry)itor.next();
			String key = (String)entry.getKey();
			double value = Double.parseDouble((String)entry.getValue());
			theMenu.add(new MenuItem(key, value));
		}
		log.info("Successfully parsed " + theMenu.size() + " menu items.");
	}
	
}
