package ve.gob.cnti.receiver.ejb;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import ve.gob.cnti.receiver.mail.IMail;
import ve.gob.cnti.receiver.mail.Mailer;
import ve.gob.cnti.receiver.utils.IMapMessageConverter;
import ve.gob.cnti.receiver.utils.IPropertiesReader;
import ve.gob.cnti.receiver.utils.MapMessageConverter;
import ve.gob.cnti.receiver.utils.PropertiesReader;

/**
 * Message-Driven Bean implementation class for: ReceiverRespuesta
 */
@MessageDriven(
		activationConfig = { @ActivationConfigProperty(
				propertyName = "destination", propertyValue = "jms/queue/respuesta"), @ActivationConfigProperty(
				propertyName = "destinationType", propertyValue = "javax.jms.Queue")
		}, 
		mappedName = "jms/queue/respuesta")
public class ReceiverRespuesta implements MessageListener {

	private static final Logger log = Logger.getLogger(ReceiverRespuesta.class.getName());
	
    /**
     * Default constructor. 
     */
    public ReceiverRespuesta() {
        // TODO Auto-generated constructor stub
    }
	
	/**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
    	
    	MapMessage incoming = (MapMessage)message;
    	IMapMessageConverter mapConverter=new MapMessageConverter();
    	Map<String,String> map=mapConverter.converMapMessageToMap(incoming);		
		IPropertiesReader properties=new PropertiesReader(map.get("processName"));
		String TEMPLATE_SUBJECT_NOTIFICACION = null;
	    String TEMPLATE_BODY_NOTIFICACION = null;	
	    log.info("paso a la respuesta"+map.get("revision"));
	    
	    if(map.get("revision").equals("Aprobado")){
			if(map.get("f_cita")!=null&&!map.get("f_cita").equals("")&&!map.get("f_cita").equals("null")){
				TEMPLATE_SUBJECT_NOTIFICACION = properties.getProperty("TEMPLATE_SUBJECT_NOTIFICACION_CITA");
			    TEMPLATE_BODY_NOTIFICACION = properties.getProperty("TEMPLATE_BODY_NOTIFICACION_CITA");			
			}else{
				 TEMPLATE_SUBJECT_NOTIFICACION = properties.getProperty("TEMPLATE_SUBJECT_NOTIFICACION_DOC");
				 TEMPLATE_BODY_NOTIFICACION = properties.getProperty("TEMPLATE_BODY_NOTIFICACION_DOC");
				 log.info("la plantilla es la siguiente"+TEMPLATE_BODY_NOTIFICACION);
			}
	    }else{
	    	TEMPLATE_SUBJECT_NOTIFICACION = properties.getProperty("TEMPLATE_SUBJECT_NOTIFICACION_RECH");
		    TEMPLATE_BODY_NOTIFICACION = properties.getProperty("TEMPLATE_BODY_NOTIFICACION_RECH");	
		    TEMPLATE_BODY_NOTIFICACION = properties.getProperty("TEMPLATE_BODY_NOTIFICACION_RECH").replace("${causasDevolucion}", map.get("causasDevolucion").replace("[", "").replace("]", ""));	
		    
	    }
		
	 
		
			
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${tramite}",map.get("tramite"));		
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${cedula}",map.get("cedula"));
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${taskDescription}",map.get("taskDescription"));
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${processDescription}",map.get("processDescription"));		
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${nombre}",map.get("nombre")+" "+map.get("apellido"));		
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${f_solicitud}",map.get("f_solicitud"));
		TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${revision}",map.get("revision"));
		
		
		byte[] arr=null;
		try {
			arr = TEMPLATE_SUBJECT_NOTIFICACION.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		TEMPLATE_SUBJECT_NOTIFICACION= new String(arr);    	
	    try {
			arr = TEMPLATE_BODY_NOTIFICACION.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    TEMPLATE_BODY_NOTIFICACION= new String(arr); 
		
		
		IMail mail=new Mailer();
		
		
		if(map.get("f_cita")!=null&&!map.get("f_cita").equals("")){
			TEMPLATE_BODY_NOTIFICACION=TEMPLATE_BODY_NOTIFICACION.replace("${f_cita}", map.get("f_cita"));
		}else if (map.get("_FILE_decision")!=null&&!map.get("_FILE_decision").equals("")){
			try {
				mail.setReceiveFile(incoming.getBytes("Bytes_FILE_Planilla"));
				mail.setFileName(map.get("_FILE_Planilla_Nombre"));
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}		

		mail.setToAddress(map.get("email"));
		mail.setSubject(TEMPLATE_SUBJECT_NOTIFICACION.
				replace("${tramite}", map.get("tramite")).
				replace("${processDescription}", map.get("processDescription")));
		mail.setBody(TEMPLATE_BODY_NOTIFICACION);		
		mail.send();
    }

}
