package ve.gob.cnti.receiver.ejb;

import java.nio.charset.Charset;
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.utils.ConverDate;
import ve.gob.cnti.receiver.utils.IConvertDate;
import ve.gob.cnti.receiver.utils.IMapMessageConverter;
import ve.gob.cnti.receiver.utils.MapMessageConverter;
import ve.gob.cnti.receiver.utils.PropertiesReader;
import ve.gob.cnti.receiver.jms.ISenderJMS;
import ve.gob.cnti.receiver.jms.SenderJMS;
import ve.gob.cnti.receiver.mail.IMail;
import ve.gob.cnti.receiver.mail.Mailer;

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

    /**
     * Default constructor. 
     */
	
	private static final Logger log = Logger.getLogger(ReciverActivity.class.getName());
	
    public ReciverActivity() {
        // TODO Auto-generated constructor stub
    }
	
	/**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
    	
    	log.info("usando el ejb para enviar el mensaje al portal ciudadano de las actividades");
		MapMessage incoming = (MapMessage)message;
		
		
		try {
			log.info("paso y deja el id de la actividad"+incoming.getString("taskId"));
		} catch (JMSException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {			
			
			ISenderJMS senderJMS=new SenderJMS();
			senderJMS.sendActivitiesRegistry(incoming);	
			IMapMessageConverter  converter=new MapMessageConverter();
			IConvertDate converterDate=new ConverDate();
			Map<String,String> map=converter.converMapMessageToMap(incoming);
			PropertiesReader properties = new PropertiesReader(map.get("processName"));		
			String TEMPLATE_SUBJECT_CIUDADANO = properties.getProperty("TEMPLATE_SUBJECT_CIUDADANO");
			
			String TEMPLATE_BODY_CIUDADANO  = properties.getProperty("TEMPLATE_BODY_CIUDADANO");		
			String subject=TEMPLATE_SUBJECT_CIUDADANO.replace("${tramite}", map.get("id")).
						   replace("${processDescription}",map.get("processDescription"));
			
			byte[] arr=  subject.getBytes("UTF-8");
			subject= new String(arr);			
			
            String text=TEMPLATE_BODY_CIUDADANO.replace("${numero}", map.get("id"));;
			text=text.replace("${processName}",map.get("processName"));
			text=text.replace("${ciudadano}",(map.get("nombre"))+" "+map.get("apellido"));
			text=text.replace("${processDescription}",map.get("processDescription"));
			text=text.replace("${taskName}",map.get("taskName"));
			text=text.replace("${taskDescription}",map.get("taskDescription"));			
			text=text.replace("${f_final}",converterDate.getFormattedLongDateDate("dd-MM-yyyy",map.get("lastupdate")));
			
			arr = text.getBytes("UTF-8");
			text= new String(arr);
			
			IMail mailSender=new Mailer();
			mailSender.setToAddress(map.get("email"));			
			mailSender.setSubject(subject);
	    	mailSender.setBody(text);	    	 
	    	mailSender.send();	    	
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	
    	
        
    }

}
