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

import agilex.commons.helper.PropertyHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.agilex.healthcare.schedulingmanager.dataservice.PatientPreferenceDataService;
import com.agilex.healthcare.schedulingmanager.domain.SMPatientPreference;
import com.agilex.messaging.core.MessageHandler;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: clarkee
 * Date: 2/22/13
 * Time: 10:53 AM
 * To change this template use File | Settings | File Templates.
 */
public class AppointmentRequestMessageCreatedMessageHandler implements MessageHandler<AppointmentRequestMessageCreatedMessage> {
    
    private final static Log logger = LogFactory.getLog(AppointmentRequestMessageCreatedMessageHandler.class);

    @Resource
    private PropertyHelper propertyHelper;

    private static String dailyEmailPreference = "Daily";
    private static String neverEmailPreference = "Never";
    private static String eachEmailPreference = "Each new message";
    private EmailDefaults emailDefaults;
    private static final String EMAIL_BODY_PROPERTY_STRING = "email.notifications.body";
    private static String emailBody = null;

    @Resource
    private PatientPreferenceDataService patientPreferenceDataService;

    public AppointmentRequestMessageCreatedMessageHandler(EmailDefaults emailDefaults) {
        this.emailDefaults = emailDefaults;
    }

    @Override
    public void handle(AppointmentRequestMessageCreatedMessage appointmentRequestMessageCreatedMessage) {
        //Retrieve patient preferences
        //create new messages for email and sms as appropriate
        logger.debug("Received " + appointmentRequestMessageCreatedMessage.getClass().getName());

//        PatientPreferenceDataService preferenceDataService = new PatientPreferenceDataService();
        final SMPatientPreference smPatientPreference = patientPreferenceDataService.fetchPatientPreference(appointmentRequestMessageCreatedMessage.getPatientIdentifier());

        try {
            createEmailMessage(smPatientPreference, appointmentRequestMessageCreatedMessage.getDetailCode());
        } catch (MessagePublishException e) {
            logger.error(e.getMessage());
        }

    }

    private void createEmailMessage(SMPatientPreference smPatientPreference, String detailCode) throws MessagePublishException {

        if (smPatientPreference.isEmailAllowed()){
            if(propertyHelper != null && detailCode != null && detailCode.trim().length() > 0){
                emailBody = propertyHelper.getProperty(EMAIL_BODY_PROPERTY_STRING + "." + detailCode);
            }
            assert(smPatientPreference.getEmailAddress() != null);

            String frequency = smPatientPreference.getNotificationFrequency();
            if (frequency == null || frequency.equals(neverEmailPreference)){
                logger.debug("Email Notification Message not sent due to patient preference");
            }
            else if (frequency.equals(dailyEmailPreference)){
                logger.debug("Creating Email Notification Message for Daily");
                BatchEmailNotificationMessage message = new BatchEmailNotificationMessage(smPatientPreference.getEmailAddress(), emailDefaults, emailBody);
                BatchEmailNotificationGateway gateway = MessagingFactory.getBatchEmailNotificationGateway();

                gateway.send(message);
                logger.debug("Email Notification Message sent");
            }
            else if (frequency.equals(eachEmailPreference)){
                logger.debug("Creating Email Notification Message");

                EmailNotificationMessage message = new EmailNotificationMessage(smPatientPreference.getEmailAddress(), emailDefaults, emailBody);
                EmailNotificationGateway gateway = MessagingFactory.getEmailNotificationGateway();

                gateway.send(message);
                logger.debug("Email Notification Message sent");
            }
        }
    }
}
