package com.ferienclub.leisure;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

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

import com.ferienclub.custadmin.beans.CustomerPreference;
import com.ferienclub.custadmin.beans.CustomerPreferences;
import com.ferienclub.leisure.esb.messages.LeisureQuery;

public class Facility implements LeisureFacility, UMODescriptorAware{

	Logger log = Logger.getLogger(Facility.class);
	
	private List leisureOffers;
	private String facilityName;
	
	public Facility(){
		this.leisureOffers = new ArrayList();
	}
	
	public LeisureOffers getLeisureOffers(CustomerPreferences prefs) {
		HashMap offersCache = new HashMap();
	    for (Iterator itor = prefs.iterator(); itor.hasNext();){
	    	CustomerPreference p = (CustomerPreference)itor.next();
	    	for (Iterator itor2 = leisureOffers.iterator(); itor2.hasNext();)
	    	{
	    		LeisureOffer offer = (LeisureOffer)itor2.next();
	    		if (offer.getPreferenceName().equals(p.getName())){
	    			if (offer.getPreferenceValue() <= p.getValue()){
	    				offersCache.put(offer.getName(),offer);
	    			}
	    		}
	    	}
	    }
	    
	    // LeisureOffer[] offers = new LeisureOffer[offersCache.values().size()];
	    List offers = new ArrayList();
	    int index = 0;
	    for (Iterator itor = offersCache.values().iterator(); itor.hasNext();)
	    {
	    	LeisureOffer lo = (LeisureOffer)itor.next();
	    	offers.add(lo);
	    	//offers[index] = (LeisureOffer)itor.next();
	    	//index++;
	    }
	    LeisureOffers o = new LeisureOffers(offers);
		return o;
	}

	public void setDescriptor(UMODescriptor descriptor) {
		this.facilityName = descriptor.getName();
		Map props = descriptor.getProperties();
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
		for (Iterator itor = props.values().iterator(); itor.hasNext();)
		{
			StringTokenizer tok = new StringTokenizer((String)itor.next(),",");
			while (tok.hasMoreTokens())
			{
				try {
					leisureOffers.add(new LeisureOffer(tok.nextToken(), 
													   tok.nextToken(), 
													   Integer.parseInt(tok.nextToken()),
													   Integer.parseInt(tok.nextToken()), 
													   Double.parseDouble(tok.nextToken()), 
													   sdf.parse(tok.nextToken()),
													   sdf.parse(tok.nextToken()),
													   tok.nextToken()));
				} catch (ParseException e){
					log.info(this.getClass() + " : Unable to parse DateFormat in configuration file!");
					System.exit(0);
				}
			}
		}
		log.info(this.getClass() + " : " + facilityName + " --> successfully parsed " + leisureOffers.size() + " leisure offers.");
	}

	public int makeBooking(LeisureOffer offer, Integer persons) {
		log.info("entering makeBooking(" + offer.getName() + ", " + persons + " )");
		LeisureOffer offerToBook = null;
		for (Iterator itor = leisureOffers.iterator(); itor.hasNext();){
			LeisureOffer o = (LeisureOffer)itor.next();
			if (o.equals(offer)){
				offerToBook = o;
			}
		}
		
		if (offerToBook == null) {
			log.info(this.getClass() + " : Sorry, the offer " + offer.getName() + " is not supported by this facility!");
			return -1;
		}
		if (offerToBook.getNumberOfPersons() - persons.intValue() < 0){
			log.info(this.getClass() + " : Sorry, this offer is full!");
			return -1;
		}
		offerToBook.setNumberOfPersons(offerToBook.getNumberOfPersons() - persons.intValue());
		return 1;
	}
}


















