package managedbean.common;

import java.io.IOException;
import java.util.Properties;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import ejb.common.CommonFacadeRemote;
import ejb.medicalTest.MedicalTestFacadeRemote;
import ejb.profile.ProfileFacadeRemote;
import ejb.systemAdmin.SystemAdminFacadeRemote;
import ejb.visit.VisitFacadeRemote;

/***
 * 
 * @author Marcos García Núñez (mgarcianun@uoc.edu)
 *
 */
public class ManagedBeanBase {
	@EJB
	protected CommonFacadeRemote remoteManagerCommon;
	@EJB
	protected ProfileFacadeRemote remoteManagerProfile;
	@EJB
	protected SystemAdminFacadeRemote remoteManagerSystemAdmin;
	@EJB
	protected VisitFacadeRemote remoteManagerVisit;
	@EJB
	protected MedicalTestFacadeRemote remoteManagerMedicalTest;

	/**
	 * Inicializa la conexión con el EJB Remoto
	 * 
	 * @return
	 * 
	 * @throws Exception
	 */
	private <T> T getContext(String name, Class<T> type) {
		T retObj = null;

		try {
			Properties props = System.getProperties();
			Context ctx = new InitialContext(props);
			retObj = type.cast(ctx.lookup(name));
		} catch (NamingException e) {
			this.manageException(e);
		}
		return retObj;
	}

	protected CommonFacadeRemote getRemoteManagerCommon() {
		if (remoteManagerCommon == null) {
			remoteManagerCommon = this.getContext("java:app/MyHealth.jar/CommonFacadeBean!ejb.common.CommonFacadeRemote", CommonFacadeRemote.class);
		}

		return remoteManagerCommon;
	}

	protected ProfileFacadeRemote getRemoteManagerProfile() {
		if (remoteManagerProfile == null) {
			remoteManagerProfile = this.getContext("java:app/MyHealth.jar/ProfileFacadeBean!ejb.profile.ProfileFacadeRemote", ProfileFacadeRemote.class);
		}

		return remoteManagerProfile;
	}

	protected SystemAdminFacadeRemote getRemoteManagerSystemAdmin() {
		if (remoteManagerSystemAdmin == null) {
			remoteManagerSystemAdmin = this.getContext("java:app/MyHealth.jar/SystemAdminFacadeBean!ejb.systemAdmin.SystemAdminFacadeRemote", SystemAdminFacadeRemote.class);
		}

		return remoteManagerSystemAdmin;
	}

	protected VisitFacadeRemote getRemoteManagerVisit() {
		if (remoteManagerVisit == null) {
			remoteManagerVisit = this.getContext("java:app/MyHealth.jar/VisitFacadeBean!ejb.visit.VisitFacadeRemote", VisitFacadeRemote.class);
		}

		return remoteManagerVisit;
	}

	protected MedicalTestFacadeRemote getRemoteManagerMedicalTest() {
		if (remoteManagerMedicalTest == null) {
			remoteManagerMedicalTest = this.getContext("java:app/MyHealth.jar/MedicalTestFacadeBean!ejb.medicalTest.MedicalTestFacadeRemote", MedicalTestFacadeRemote.class);
		}

		return remoteManagerMedicalTest; 
	}

	protected void addFacesMessageKeep(FacesMessage.Severity severity, String summary, String detail) {
		FacesContext context = FacesContext.getCurrentInstance();

		this.addFacesMessage(FacesContext.getCurrentInstance(), null, severity, summary, detail);

		context.getExternalContext().getFlash().setKeepMessages(true);
	}

	protected void addFacesMessage(FacesMessage.Severity severity, String summary, String detail) {
		this.addFacesMessage(FacesContext.getCurrentInstance(), null, severity, summary, detail);
	}

	protected void addFacesMessage(String clientId, FacesMessage.Severity severity, String summary, String detail) {
		this.addFacesMessage(FacesContext.getCurrentInstance(), clientId, severity, summary, detail);
	}

	protected void addFacesMessage(FacesContext context, String clientId, FacesMessage.Severity severity, String summary, String detail) {
		this.addFacesMessage(context, clientId, new FacesMessage(severity, summary, detail));
	}

	protected void addFacesMessage(FacesMessage facesMsg, String clientId) {
		this.addFacesMessage(FacesContext.getCurrentInstance(), clientId, facesMsg);
	}

	protected void addFacesMessage(FacesContext context, String clientId, FacesMessage facesMsg) {
		context.addMessage(clientId, facesMsg);
	}

	protected void manageException(Exception ex) {
		String strType = "";

		if (ex.getClass().equals(ViewExpiredException.class)) {
			// Sessión expired
			strType = "expired";
			this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Su sesión ha caducado", "Su sesión ha caducado, vuelva a logarse en el sistema.");
		} else {
			this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Se ha producido un error inesperado", "Descripción del error: " + ex.getLocalizedMessage());
		}

		
		try {
			ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
			
			ctx.redirect(ctx.getApplicationContextPath() + "/error.xhtml?type=" + strType);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
