package com.ferienclub.leisure.esb;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import com.ferienclub.custadmin.beans.CustomerPreference;
import com.ferienclub.custadmin.beans.CustomerPreferences;
import com.ferienclub.custadmin.beans.HistoryEntry;
import com.ferienclub.leisure.esb.messages.BookingRequest;
import com.ferienclub.leisure.esb.messages.HistoryEntryRequest;
import com.ferienclub.leisure.esb.messages.LeisureQuery;
import com.ferienclub.leisure.esb.messages.PrefQuery;

public class CAGateway {
	
	Logger log = Logger.getLogger(CAGateway.class);
	
	public void process(LeisureQuery query){
		log.info("Entering method process(LeisureQuery query)");
		try {
			UMOEventContext context = RequestContext.getEventContext();
			UMOMessage result = context.sendEvent(new PrefQuery());
			Object o = result.getPayload();
			String userId = query.getUserId();
			CustomerPreferences[] prefs = (CustomerPreferences[])o;
			boolean found = false;
			for (int i = 0; i < prefs.length; i++)
			{
				CustomerPreferences cp = prefs[i];
				if (cp.getUserId().equals(userId))
				{	
					query.setPrefs(prefs[i]);
					found = true;
				}
			}
			if (!found) {
				// Generating some dummy Preferences
				CustomerPreference cp = new CustomerPreference("dummy",1);
				List l = new ArrayList();
				l.add(cp);
				CustomerPreferences cps = new CustomerPreferences(l, query.getUserId());
				query.setPrefs(cps);
			}
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public void process(HistoryEntryRequest req) {
		log.info("Entering method process(HistoryEntryRequest req)");
		UMOEventContext context = RequestContext.getEventContext();
		HistoryEntry entry = new HistoryEntry();
		entry.setEntryDate(new Date());
		entry.setUserId(req.getUserId());
		entry.setEntryText(req.getText());
		entry.setEntryTypeDesc(req.getType());
		try {
			log.info("Sending History Entry.");
			context.sendEvent(entry);
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		context.setStopFurtherProcessing(true);
	}
	
	public void process(BookingRequest request){
		log.info("Entering method process(BookingRequest request)");
		if (!request.isSuccessful()){
			log.info("\nSorry, the booking failed!\n");
			return;
		}
		try {
			log.info("Sending HistoryEntry for leisure booking.");
			UMOEventContext context = RequestContext.getEventContext();
			HistoryEntry entry = new HistoryEntry();
			entry.setEntryDate(new Date());
			entry.setUserId(request.getUserId());
			entry.setEntryText("Booked " + request.getOffer().getName());
			entry.setEntryTypeDesc("LEISURE");
			UMOMessage result = context.sendEvent(entry);
			if (request.isCarSuccessful())
			{
				log.info("Sending HistoryEntry for car rental.");
				entry.setEntryText("Hired a car");
				entry.setEntryTypeDesc("CAR_RENT");
				result = context.sendEvent(entry);
			}
			log.info("Stopping MULE from further processing.");
			context.setStopFurtherProcessing(true);
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
