package com.ferienclub.mb;

import org.apache.log4j.Logger;
import org.mule.extras.client.MuleClient;
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.Appointment;
import com.ferienclub.custadmin.beans.CustomerPreferences;
import com.ferienclub.custadmin.beans.Place;
import com.ferienclub.custadmin.beans.User;
import com.ferienclub.mb.interfaces.IUserManagement;

public class UserManagementPM implements IUserManagement{

	Logger log = Logger.getLogger(UserManagementPM.class);
	
	public String[] getPreferenceNames(String dummy){
		log.info("Entering getPreferenceNames");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			// The good way
			//UMOMessage result = context.send("");
			// The bad way
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=getPreferenceNames","",null);
			if (result == null) return null;
			String[] prefNames = (String[])result.getPayload();
			log.info("Successfully received PreferenceNames");
			context.setStopFurtherProcessing(true);
			return prefNames;
		} catch (UMOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public CustomerPreferences getUserPreferences(String userId){
		log.info("Entering getUserPreferences");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			// The good way
			//UMOMessage result = context.send("");
			// The bad way
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=getCustomerPreferences",userId,null);
			CustomerPreferences cprefs = (CustomerPreferences)result.getPayload();
			log.info("Successfully received CustomerPreferences for user: " + userId);
			context.setStopFurtherProcessing(true);
			return cprefs;
		} catch (UMOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public User login(String firstName, String lastName, String password){
		log.info("Entering login()");
		User u = null;
		User[] users = getUsers("");
		for (int i = 0; i < users.length; i++)
		{
			u = users[i];
			if (u.getFirstName().equals(firstName) && u.getName().equals(lastName) && u.getPassword().equals(password))
			   return u;
		} 
		log.info("Login failed, could not find user " + firstName + " " + lastName + " with password " + password);
		return null;
	}
	
	public User[] getUsers(String dummy) {
		log.info(this.getClass() + " : Entering method getUsers()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			// The good way
			//UMOMessage result = context.send("");
			// The bad way
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=getUsers","",null);
			User[] users = (User[])result.getPayload();
			log.info(this.getClass() + " : Received " + users.length + " Users from the database.");
			context.setStopFurtherProcessing(true);
			return users;
		} catch (UMOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Place[] getPlaces(String dummy){
		log.info(this.getClass() + " : Entering method getPlaces()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=getPlaces","",null);
			Place[] places = (Place[])result.getPayload();
			log.info(this.getClass() + " : Received " + places.length + " Places");
			context.setStopFurtherProcessing(true);
			return places;
		} catch (UMOException e){
			e.printStackTrace();
		}
		return null;
	}
	
	public int enterNewUser(User u) {
		log.info(this.getClass() + " : Entering method enterNewUser()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			/* Good practice */
			//UMOMessage result = context.sendEvent(u);
			/* Very bad! Don't do this at home!*/
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=enterNewUser",u,null);
			Integer ok = (Integer)result.getPayload();
			if (ok.intValue() > 0) log.info(this.getClass() + " : Successfully entered a new User");
			  else log.info(this.getClass() + " : Error entering new User!"); 
			context.setStopFurtherProcessing(true);
			return ok.intValue();
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public int checkIn(User u) {
		log.info(this.getClass() + " : Entering method checkIn()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/VacancyManagement?method=checkIn",u,null);
			Integer ok = (Integer)result.getPayload();
			if (ok.intValue() > 0) log.info(this.getClass() + " : Successfully checked in");
			  else log.info(this.getClass() + " : Error checking in!"); 
			context.setStopFurtherProcessing(true);
			return ok.intValue();
		} catch (UMOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int checkOut(User u) {
		log.info(this.getClass() + " : Entering method checkOut()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/VacancyManagement?method=checkOut",u,null);
			Integer ok = (Integer)result.getPayload();
			if (ok.intValue() > 0) log.info(this.getClass() + " : Successfully checked out");
			  else log.info(this.getClass() + " : Error checking out!"); 
			context.setStopFurtherProcessing(true);
			return ok.intValue();
		} catch (UMOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int addPlace(Place p) {
		log.info(this.getClass() + " : Entering method getPlaces()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=addPlace",p,null);
			int rv = ((Integer)result.getPayload()).intValue();
			if (rv < 0) log.info(this.getClass() + " : Error entering place");
			else log.info(this.getClass() + " : Successfully entered place");
			context.setStopFurtherProcessing(true);
			return rv;
		} catch (UMOException e){
			e.printStackTrace();
		}
		return 0;
	}

	public int deletePlace(String placeId) {
		return 0;
	}

	public int insertAppointment(Appointment app) {
		log.info(this.getClass() + " : Entering method insertAppointment()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			/* Good practice */
			//UMOMessage result = context.sendEvent(u);
			/* Very bad! Don't do this at home!*/
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=insertAppointment",app,null);
			Integer ok = (Integer)result.getPayload();
			if (ok.intValue() > 0) log.info(this.getClass() + " : Successfully inserted appointment");
			  else log.info(this.getClass() + " : Error inserting appointment!"); 
			context.setStopFurtherProcessing(true);
			return ok.intValue();
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}

	public int deleteAppointment(String appId) {
		log.info(this.getClass() + " : Entering method deletePlace(" +  appId + ")");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			/* Good practice */
			//UMOMessage result = context.sendEvent(u);
			/* Very bad! Don't do this at home!*/
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=deleteAppointment",appId,null);
			Integer ok = (Integer)result.getPayload();
			if (ok.intValue() > 0) log.info(this.getClass() + " : Successfully deleted appointment");
			  else log.info(this.getClass() + " : Error deleting appointment!"); 
			context.setStopFurtherProcessing(true);
			return ok.intValue();
		} catch (UMOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}

	public Appointment[] getAppointments(String dummy) {
		log.info(this.getClass() + " : Entering method getAppointments()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/CustomerAdministration?method=getAppointments","",null);
			Appointment[] apps = (Appointment[])result.getPayload();
			log.info(this.getClass() + " : Received " + apps.length + " Places");
			context.setStopFurtherProcessing(true);
			return apps;
		} catch (UMOException e){
			e.printStackTrace();
		}
		return null;
	}
	
	public int updateAppointment(Appointment app){
		log.info(this.getClass() + " : Entering method updateAppointment()");
		UMOEventContext context = RequestContext.getEventContext();
		// First delete the old appointment
		int ok = deleteAppointment(app.getAppointmentId());
		if (ok < 0) return -1;
		// Then insert the new appointment
		ok = insertAppointment(app);
		if (ok < 0) return -1;
		return 1;
	}
	
	public int checkPassword(String name, String password){
		User[] users = getUsers("");
		boolean ok = false;
		for (int i = 0; i < users.length; i++){
			User u = users[i];
			if ((u.getFirstName() + " " + u.getName()).equals(name) && u.getPassword().equals(password)) {
				ok = true;
				break;
			}
		}
		if (ok) return 1;
		  else return -1;
	}
	
}


















