package ve.gob.cnti.receiver.ejb;

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

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

import ve.gob.cnti.receiver.jdbc.ConnectionJDBC;
import ve.gob.cnti.receiver.jdbc.FuncionarioGI;
import ve.gob.cnti.receiver.jdbc.IConnectionJDBC;
import ve.gob.cnti.receiver.jdbc.IFuncionarioGI;
import ve.gob.cnti.receiver.mail.IMail;
import ve.gob.cnti.receiver.mail.Mailer;
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;


/**
 * Message-Driven Bean implementation class for: ReceiberEmail
 */
@MessageDriven(
		activationConfig = { @ActivationConfigProperty(
				propertyName = "destination", propertyValue = "jms/queue/email"), @ActivationConfigProperty(
				propertyName = "destinationType", propertyValue = "javax.jms.Queue")
		}, 
		mappedName = "jms/queue/email")
public class ReciverEmail implements MessageListener {
	private static final Logger log = Logger.getLogger(ReciverEmail.class.getName());
    /**
     * Default constructor. 
     */
    public ReciverEmail() {
        // 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 correo al funcionario");
    	MapMessage incoming = (MapMessage)message;
    	String subject=null;
    	String text=null; 
    	IMail mailSender=new Mailer();
    	try {   		 
					 
    		 PropertiesReader properties=new PropertiesReader("templatesistema");
			 log.info("esto es una prueba"+properties.getProperty("TEMPLATE_SUBJECT_GI"));
	    	 IConnectionJDBC connection=new ConnectionJDBC();
	    	 IMapMessageConverter converterMessage=new MapMessageConverter();
	    	 Map<String,String> map=converterMessage.converMapMessageToMap(incoming);
	    	 IFuncionarioGI funcionarioGI=new FuncionarioGI();
	    	 if(!(map.get("asignado")!=null))
	    	 	map.put("emailSGI", funcionarioGI.getEmail(map.get("tipo_tramite")));
	    	 if(!connection.verifyUser(map.get("hash")))
	    	 	connection.insertRecordCiudadanoArchivos(map);  
	    	 IConvertDate convertDate=new ConverDate();
	    	 String pattern="dd-MM-yyyy";
	    	 connection.closeConnection();	    	 
	    	 subject = properties.getProperty("TEMPLATE_SUBJECT_GI").replace("${cedula}",map.get("cedula"));
	    	 if(map.get("asignado")==null)
	    	 	subject = subject.replace("${f_solicitud}",convertDate.getFormattedLongDateDate(pattern,map.get("f_solicitud")));
	    	 else 
	    		 subject = subject.replace("${f_solicitud}",convertDate.getFormattedShortDateDate(pattern,map.get("f_solicitud")));
	    	 subject = subject.replace("${nombre}",map.get("nombre")+" "+map.get("apellido"));
	    	 subject = subject.replace("${tramite}",map.get("id_tramite"));
	    	 
	    	 byte[] arr=  subject.getBytes("UTF-8");
			 subject= new String(arr);
	    	 
	    	 text = properties.getProperty("TEMPLATE_BODY_GI").replace("${login}",map.get("email"));
	    	 text = text.replace("${password}",map.get("hash"));
	    	 text = text.replace("${usuario}",map.get("emailSGI"));
	    	 PropertiesReader propertiesConfiguracion=new PropertiesReader("receiverEJB");
	    	 text = text.replace("${url}",propertiesConfiguracion.getProperty("URL_SISTEMA"));
	    	 log.info("correo"+map.get("emailSGI"));	
	    	 
	    	 arr = text.getBytes("UTF-8");
	    	 text= new String(arr);
	    	 
	    	 mailSender.setToAddress(map.get("emailSGI"));			
			 mailSender.setSubject(subject);
	    	 mailSender.setBody(text);	    	 
	    	 mailSender.send();	    	 
	    	
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }

}
