package com.ferienclub.pda.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOException;
import org.mule.umo.UMOMessage;

import com.ferienclub.beachclub.esb.messages.MenuItem;
import com.ferienclub.custadmin.beans.User;
import com.ferienclub.pda.forms.OrderForm;

public class OrderAction extends Action {

	Logger log = Logger.getLogger(OrderAction.class);
	
	public ActionForward perform(ActionMapping mapping,
			 ActionForm form,
			 HttpServletRequest request,
			 HttpServletResponse response){
		
		log.info("Entering OrderAction");
		OrderForm oForm = (OrderForm)form;
		ActionErrors errors = new ActionErrors();
		int orderIndex = Integer.parseInt(oForm.getOrderIndex());
		User currentUser = (User)request.getSession().getAttribute("user");
		List menu = (List)request.getSession().getAttribute("menu"); 
		MenuItem selectedItem = (MenuItem)menu.get(orderIndex);
		System.out.println("\n\n" + selectedItem.getName() + " --> " + orderIndex + "\n\n");
		Object[] args = new Object[]{currentUser, selectedItem, new Integer(1)};
		
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/BeachClubManagement?method=makeOrder",args,null);
			request.setAttribute("message","Your drink has been ordered!");
		} catch (UMOException e) {
			errors.add("errors",new ActionError("technical.error.mule"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		return mapping.findForward("success");
		
	}
	
}
