package com.ferienclub.leisure.esb;

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

import org.apache.log4j.Logger;
import org.mule.extras.client.MuleClient;
import org.mule.impl.MuleMessage;
import org.mule.impl.RequestContext;
import org.mule.umo.UMOEventContext;
import org.mule.umo.UMOException;
import org.mule.umo.UMOMessage;

import com.ferienclub.custadmin.beans.CustomerPreference;
import com.ferienclub.leisure.LeisureOffer;
import com.ferienclub.leisure.LeisureOffers;
import com.ferienclub.leisure.esb.messages.BookingQuery;
import com.ferienclub.leisure.esb.messages.LeisureQuery;
import com.ferienclub.vacancymgm.beans.Booking;

public class VacancyMgmGateway {

	Logger log = Logger.getLogger(VacancyMgmGateway.class);
	
	public void process(LeisureQuery query){
		log.info("Entering method process(LeisureQuery query)");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			UMOMessage result = context.sendEvent(new BookingQuery());
			Object src = result.getPayload();
			Booking[] bookings = (Booking[])src;
			Booking relevantBooking = null;
			for (int i = 0; i < bookings.length; i++){
				String id1 = bookings[i].getBookingUser().getUserId();
				String id2 = query.getUserId();
				if (bookings[i].getBookingUser().getUserId().equals(query.getUserId()))
					relevantBooking = bookings[i];
			}
			if (relevantBooking != null)
			{
				int filter = relevantBooking.getUsers().size();
				List filteredOffers = new ArrayList();
				for (Iterator itor = query.getLeisureOffers().iterator(); itor.hasNext();){
					LeisureOffer o = (LeisureOffer)itor.next();
					if (o.getNumberOfPersons() >= filter) filteredOffers.add(o);
				}
				query.setLeisureOffers(filteredOffers);
			}
			context.setStopFurtherProcessing(true);
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
