package com.ferienclub.admin.actions;

import java.util.ArrayList;
import java.util.Iterator;
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.UMOMessage;

import com.ferienclub.admin.forms.AppointmentForm;
import com.ferienclub.custadmin.beans.Appointment;
import com.ferienclub.custadmin.beans.User;

public class UpdateAppointmentAction extends Action {

	Logger log = Logger.getLogger(CreateAppointmentAction.class);
	
	public ActionForward perform(ActionMapping mapping,
			 ActionForm form,
			 HttpServletRequest request,
			 HttpServletResponse response){
		
		AppointmentForm af = (AppointmentForm)form;
		String userId = af.getUserId();
		String appointmentId = af.getAppointmentId();
		ActionErrors errors = new ActionErrors();
		
		log.info(this.getClass()  + " : Entering UpdateAppointmentAction");
		
		User newUser = null;
		// Get the Appointment and the User for the ids
		for (Iterator itor = ((List)request.getSession().getAttribute("users")).iterator(); itor.hasNext();){
			User u = (User)itor.next();
			if (u.getUserId().equals(userId)) newUser = u;
		}
		
		Appointment appointment = null;
		for (Iterator itor = ((List)request.getSession().getAttribute("appointments")).iterator(); itor.hasNext();){
			Appointment a = (Appointment)itor.next();
			if (a.getAppointmentId().equals(appointmentId)) appointment = a;
		}
		
		if (appointment == null){
			log.info(this.getClass() + " : No appointment selected!");
			errors.add("noappsel",new ActionError("no.appointment.selected"));
			super.saveErrors(request, errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		// Add the user to the appointment and contact MULE to update it
		boolean isUserNew = true;
		// Check wheter the user is not participating in the appointment already
		if (appointment.getUsers() != null){
			for (Iterator itor = appointment.getUsers().iterator(); itor.hasNext();){
				if (((User)itor.next()).getUserId().equals(userId)) isUserNew = false;
			}
		} else {
			appointment.setUsers(new ArrayList());
		}
		
		if (appointment.getLeadingUser().getUserId().equals(userId)) isUserNew = false;
		
		if (isUserNew) {
			appointment.getUsers().add(newUser);
			try {
				MuleClient client = new MuleClient();
				UMOMessage result = client.send("axis:http://localhost:8081/axis/services/UserManagementPM?method=updateAppointment",appointment,null);
				int ok = ((Integer)result.getPayload()).intValue();
				if (ok < 0){
					log.info(this.getClass() + " : Could not update Appointment");
					errors.add("error updating appointment",new ActionError("could.not.update.appointment"));
					super.saveErrors(request,errors);
					return (new ActionForward(mapping.getInput()));
				} 
			} catch (Exception e){
				errors.add("technical Error",new ActionError("technical.error.mule"));
				super.saveErrors(request,errors);
				return (new ActionForward(mapping.getInput()));
			}
		} else {
			return mapping.findForward("success");
		}
		
		return mapping.findForward("success");		
	}
}
