package com.fp.person.rules.query;

import com.fp.bpm.query.Query;
import com.fp.dto.Response;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import java.util.List;
import com.fp.persistence.pcustomer.gene.TcustPersonAddress;

/**
 *
 * @author hinga
 */
public class DirectionFull extends QueryRule {

    private String addressaux = "addressaux";

    /**
     * Metodo que entrega la direccion completa (concatenado)
     *
     * @param pQueryRequest Objeto que posee la consulta requerida.
     * @return
     * @throws Exception
     */
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        Query qry = new Query();
        qry.process(pQueryRequest);
        List<TcustPersonAddress> lovDirect = (List<TcustPersonAddress>) response.get("TCUSTPERSONADDRESSLOVFULL");
        this.completeAddressJob(lovDirect);
        if (lovDirect == null || (lovDirect != null && lovDirect.isEmpty())) {
            return pQueryRequest;
        }
        return pQueryRequest;
    }

    /**
     * Metodo q se encarga de concatenar la direccion en un solo campo
     *
     * @param lovDirection objeto que posee una lista de registros consultados
     * de tcustPersonAddress
     * @throws Exception
     */
    protected void completeAddressJob(List<TcustPersonAddress> lovDirection) throws Exception {

        for (Object addre : lovDirection) {
            TcustPersonAddress tcustPersonAddress = (TcustPersonAddress) addre;
            tcustPersonAddress.addAddtionalInfo(addressaux, DirectionFull.getFullAddress(tcustPersonAddress));
        }
    }

    /**
     * Metodo que se encarga de armar la direccion dada la direccion
     *
     * @param tcustPersonAddress
     * @return
     */
    public static String getFullAddress(TcustPersonAddress tcustPersonAddress) {
        if (tcustPersonAddress == null) {
            return null;
        } else if (tcustPersonAddress.getAddresstypecatalog() != null
                && tcustPersonAddress.getAddresstypecatalog().compareTo("3") == 0) {
            return tcustPersonAddress.getAddress();
        } else {
            String newaddress = "";
            if (tcustPersonAddress.getAddress() != null) {
                newaddress += tcustPersonAddress.getAddress();
            }
            if (tcustPersonAddress.getStreetnumber() != null) {
                newaddress += " " + tcustPersonAddress.getStreetnumber();
            }
            if(tcustPersonAddress.getDepartment()!=null)
            {
                newaddress +=" ("+tcustPersonAddress.getDepartment()+")";
            }
            if (tcustPersonAddress.getStreet() != null) {
                newaddress += " " + tcustPersonAddress.getStreet();
            }
            if (tcustPersonAddress.getUrbanization() != null) {
                newaddress += " /" + tcustPersonAddress.getUrbanization();
            }
            if (tcustPersonAddress.getRemark() != null) {
                newaddress += ", " + tcustPersonAddress.getRemark() ;
            }
            if (newaddress.compareTo("") == 0) {
                newaddress = null;
            }
            return newaddress;
        }
    }
}
