package com.agilex.healthcare.schedulingmanager.notification.appointmentrequest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * Created with IntelliJ IDEA.
 * User: clarkee
 * Date: 2/25/13
 * Time: 1:31 PM
 * To change this template use File | Settings | File Templates.
 */

public class MessagingFactory implements ApplicationContextAware {
    private static final org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(MessagingFactory.class);
    private static BeanFactory beanFactory;
    private static ApplicationContext springContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        springContext = applicationContext;
    }

    private static BeanFactory getBeanFactory() {
        if (beanFactory == null) {
            synchronized (MessagingFactory.class) {
                if (beanFactory == null) {
                    LOGGER.debug("bean factory not initialized, get app context");
                    ApplicationContext appContext = springContext;
                    beanFactory = appContext;
                    LOGGER.debug("bean factory now set");
                }
            }
        }
        return beanFactory;
    }

    public static EmailNotificationGateway getEmailNotificationGateway(){
            return (EmailNotificationGateway)getBeanFactory().getBean("emailNotificationGateway", EmailNotificationGateway.class);
    }
    public static EmailNotificationWorker getEmailNotificationWorker(){
        return (EmailNotificationWorker)getBeanFactory().getBean("emailNotificationWorker", EmailNotificationWorker.class);
    }

    public static AppointmentRequestMessageNotificationGateway getAppointmentRequestMessageNotificationGateway(){
        return (AppointmentRequestMessageNotificationGateway)getBeanFactory().getBean("appointmentRequestMessageNotificationGateway", AppointmentRequestMessageNotificationGateway.class);
    }
    public static HaMessagingConfiguration getAppointmentRequestMessageNotificationConfiguration(){
        return (HaMessagingConfiguration)getBeanFactory().getBean("appointmentRequestMessageNotificationConfiguration", HaMessagingConfiguration.class);
    }

    public static JavaMailSender getJavaMailSender(){
        return (JavaMailSender)getBeanFactory().getBean("mailSender", JavaMailSender.class);
    }

    public static BatchEmailNotificationGateway getBatchEmailNotificationGateway() {
        return (BatchEmailNotificationGateway)getBeanFactory().getBean("batchEmailNotificationGateway", BatchEmailNotificationGateway.class);
    }

    public static HaMessagingConfiguration getBatchEmailNotificationConfiguration() {
        return (HaMessagingConfiguration)getBeanFactory().getBean("batchEmailNotificationConfiguration", HaMessagingConfiguration.class);
    }

    public static BatchEmailNotificationMessageHandler getBatchEmailNotificationMessageHandler() {
        return (BatchEmailNotificationMessageHandler)getBeanFactory().getBean("batchEmailNotificationHandler", BatchEmailNotificationMessageHandler.class);
    }

    public static BatchEmailNotificationWorker getBatchEmailNotificationWorker(){
        return (BatchEmailNotificationWorker)getBeanFactory().getBean("batchEmailNotificationWorker", BatchEmailNotificationWorker.class);
    }
}
