package com.fp.armas.mail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.parmas.fun.TarmRegistroArmas;
import com.fp.persistence.parmas.fun.TarmVentaArmaTem;
import com.fp.persistence.parmas.soli.TarmArmas;
import com.fp.persistence.pcustomer.gene.TcustPersonAddress;
import com.fp.persistence.pgeneral.gene.TgeneParameters;
import com.fp.persistence.pgeneral.gene.TgeneParametersKey;
import com.fp.simple.maia.MailRecipient;

/**
 * Clase que se encarga de objener el email de una persona.
 * 
 * @version 2.1
 */
public class PersonaDireccionVentaTemp extends MailRecipient {

    /*
     * (non-Javadoc)
     * 
     * @see com.fp.simple.maia.MailRecipient#getEmailAddress(java.lang.Object)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getEmailAddress(Object pParam) throws Exception {
        List<String> lemail = new ArrayList<String>();
        Map<String, Object> mresp = new HashMap<String, Object>();
        HashMap<String, Object> m = (HashMap<String, Object>) pParam;
        Map<String, Object> mrequest = (Map<String, Object>) m.get("request");
        System.out.println("addres---1. "+mrequest.get("csolicitud"));
        if (mrequest.get("csolicitud") == null) {
        	 mresp.put("lemail", lemail);
            return mresp;
        }
        try {
        	String solicitud = (String) mrequest.get("csolicitud");
        	System.out.println("addres---2. ");
            EntityManager em=PersistenceHelper.getEntityManager();
            System.out.println("addres---3. ");
            TarmArmas arma= TarmArmas.find( em, solicitud);
            System.out.println("addres---4. "+arma);
            List<TarmVentaArmaTem>listaventa= em.createQuery("select p from TarmVentaArmaTem p where p.carma=:carma and p.estado=:estado")
            		.setParameter("carma", arma.getPk())
            		.setParameter("estado", "PENDIENTE")
            		.getResultList();
            System.out.println("addres---5. "+listaventa);
            TarmRegistroArmas registro = TarmRegistroArmas.find(em, arma.getCregistro());
            System.out.println("addres---6. "+listaventa);
            TgeneParameters parameters= TgeneParameters.find(em, new TgeneParametersKey("CODIGO.MAIL", 1));
            System.out.println("addres---7. "+listaventa);
            if(listaventa!=null && listaventa.size()>0){
            	System.out.println("addres---8. "+listaventa.size());
            	// TcustPersonAddressKey
                List<TcustPersonAddress> lpersonadress= em.createQuery("select o from TcustPersonAddress o where o.pk.personcode=:personcode "
                											+ " and o.addresstypecatalog=:addresstypecatalog"
                											+ " and  o.pk.dateto=:dateto",TcustPersonAddress.class)
                											.setParameter("personcode", registro.getPersoncode())
                											.setParameter("dateto", FormatDates.getDefaultExpiryTimestamp())
                											.setParameter("addresstypecatalog", parameters.getTextvalue()).getResultList();
                System.out.println("addres---9. "+lpersonadress);
                // adiciona email.
                for(TcustPersonAddress addres:lpersonadress){
                	System.out.println("addres--10-"+addres);
                	if(addres!=null && addres.getAddress()!=null && addres.getAddress().contains("@")){
                		lemail.add(addres.getAddress());
                		System.out.println("addres--10.1-"+addres.getAddress());
                	}
                }
                System.out.println("addres--11-");
            }
		} catch (Exception e) {
			 System.out.println("erro-----------12-");
			e.printStackTrace();
		}
        System.out.println("sale-----------13-");
        mresp.put("lemail", lemail);
        return mresp; 
    }
}
