package com.ferienclub.pda.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

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.custadmin.beans.CustomerPreference;
import com.ferienclub.custadmin.beans.CustomerPreferences;
import com.ferienclub.custadmin.beans.User;
import com.ferienclub.pda.forms.ProfileForm;

public class SubmitProfileAction extends Action {

Logger log = Logger.getLogger(SubmitProfileAction.class); 
	
	public ActionForward perform(ActionMapping mapping,
			 ActionForm form,
			 HttpServletRequest request,
			 HttpServletResponse response){
		log.info("Entering SubmitProfileAction");
		
		ProfileForm pForm = (ProfileForm)form;
		ActionErrors errors = new ActionErrors();
		String profileString = pForm.getProfileString();
		String[] names = (String[])request.getSession().getAttribute("prefs");
		User user = (User)request.getSession().getAttribute("user");
		List prefs = new ArrayList();
		List values = new ArrayList();
		// Parse the value String from the jsp
		StringTokenizer tok = new StringTokenizer(profileString,",");
		while (tok.hasMoreTokens())
			values.add(tok.nextToken());
		
		// Check wheter there are enough values
		if (names.length != values.size())
		{
			errors.add("errors",new ActionError("must.fill.out.complete.profile"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		// Create new CustomerPreferences
		try 
		{
			for (int i = 0; i < names.length; i ++)
			{
				String name = names[i];
				String value = (String)values.get(i);
				int v = Integer.parseInt(value);
				prefs.add(new CustomerPreference(name,v));
			}
		} 
		catch (NumberFormatException e)
		{
			errors.add("errors",new ActionError("only.numbers.in.profile"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		CustomerPreferences cPrefs = new CustomerPreferences(prefs,user.getUserId());
		
		// Save the new Preferences with Mule
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=setCustomerPreferences",cPrefs,null);
			Integer i = (Integer)result.getPayload();
			if (i.intValue() < 0) {
				log.info("Could not enter profile!");
				errors.add("errors",new ActionError("technical.error.mule"));
				super.saveErrors(request,errors);
				return (new ActionForward(mapping.getInput()));
			}
		} catch (UMOException e) {
			errors.add("errors",new ActionError("technical.error.mule"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		} catch (Exception e){
			errors.add("errors",new ActionError("technical.error"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		return mapping.findForward("success");
	}
	
}
