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 com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pcustomer.company.TcustFamiliarlLink;
import com.fp.persistence.pcustomer.company.TcustLegalLink;
import com.fp.persistence.pcustomer.gene.TcustPersonAddress;
import com.fp.persistence.pcustomer.gene.TcustPersonAddressKey;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.people.TcustPeopleJob;
import com.fp.persistence.pcustomer.people.TcustPeopleOtherAssets;
import com.fp.persistence.pcustomer.people.TcustPeopleTradereferences;
import com.fp.persistence.pcustomer.people.TcustPeopleVehicle;
import com.fp.persistence.pgeneral.gene.TgeneAccountRelationship;
import com.fp.persistence.pgeneral.gene.TgeneCompany;
import com.fp.persistence.pgeneral.gene.TgeneSolicitudeRelationship;
import com.fp.person.db.DataHelper;
import java.util.List;

/**
 * Clase que se encarga de completar el nombre de una persona.
 *
 * @author Wilson Sigcho
 * @version 2.1
 */
public class PersonName extends QueryRule {

    private String name = "name";
    private String expiratedesignation = "expiratedesignation";

    /**
     * Metodo que agrega el nombre de una persona, cuando una consulta es requerida.
     *
     * @param pQueryRequest Objeto que posee la consulta requerida.
     * @return pQueryRequest
     * @throws Exception
     */
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        Query qry = new Query();
        qry.process(pQueryRequest);
        List<Object> legallink = (List<Object>) response.get("TCUSTLEGALLINK");
        List<Object> familiarlink = (List<Object>) response.get("TCUSTFAMILIARLLINK");
        List<Object> genecompany = (List<Object>) response.get("TGENECOMPANYLOV");
        List<Object> jobs = (List<Object>) response.get("TCUSTPEOPLEJOB");
        List<Object> vehicles = (List<Object>) response.get("TCUSTPEOPLEVEHICLE");
        List<Object> otherassets = (List<Object>) response.get("TCUSTPEOPLEOTHERASSETS");
        List<Object> tradereferences = (List<Object>) response.get("TCUSTPEOPLETRADEREFERENCES");
        List<Object> relationship = (List<Object>) response.get("TGENESOLICITUDERELATIONSHIP");
        List<Object> accountrelationship = (List<Object>) response.get("TGENEACCOUNTRELATIONSHIP");
        this.completeLegalLink(legallink);
        this.completeFamiliarlLink(familiarlink);
        this.completeGeneCompany(genecompany);
        this.completeJob(jobs);
        this.completeVehicles(vehicles);
        this.completeOtherAssets(otherassets);
        this.completeTradeReferences(tradereferences);
        this.completeSolicitudeRealtionShip(relationship);
        this.completeAccountRelationShip(accountrelationship);
        return pQueryRequest;
    }

    /**
     * Metodo que se encraga de completar el nombre de una persona, en la tabla TcustLegalLink.
     *
     * @param lDetail Objeto que posee una lista de los registros consultados en la tabla TcustLegalLink.
     * @throws Exception
     */
    private void completeLegalLink(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TcustLegalLink tcustLegalLink = (TcustLegalLink) obj;
            if (tcustLegalLink.getPk().getLinkpersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustLegalLink.getPk().getLinkpersoncode());
                tcustLegalLink.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tcustLegalLink.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que se encraga de completar el nombre de una persona, en la tabla TcustFamiliarlLink.
     *
     * @param lDetail Objeto que posee una lista de los registros consultados en la tabla TcustFamiliarlLink.
     * @throws Exception
     */
    private void completeFamiliarlLink(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TcustFamiliarlLink tcustFamiliarlLink = (TcustFamiliarlLink) obj;
            if (tcustFamiliarlLink.getPk().getLinkpersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustFamiliarlLink.getPk().getLinkpersoncode());
                tcustFamiliarlLink.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tcustFamiliarlLink.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que se encraga de completar el nombre de una persona, en la tabla TgeneCompany.
     *
     * @param lDetail Objeto que posee una lista de los registros consultados en la tabla TgeneCompany.
     * @throws Exception
     */
    private void completeGeneCompany(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TgeneCompany tgeneCompany = (TgeneCompany) obj;
            if (tgeneCompany.getPersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tgeneCompany.getPersoncode());
                tgeneCompany.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tgeneCompany.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el nombre del deunio de la empresa
     *
     * @param lDetail
     * @throws Exception
     */
    private void completeJob(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TcustPeopleJob tcustPeopleJob = (TcustPeopleJob) obj;
            if (tcustPeopleJob != null) {
                if (tcustPeopleJob.getPk().getPersoncode() != null) {
                    TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustPeopleJob.getPk().getPersoncode());
                    tcustPeopleJob.addAddtionalInfo(name, tcustPersonDetail.getName());
                } else {
                    tcustPeopleJob.addAddtionalInfo(name, null);
                }
                if (tcustPeopleJob.getPk().getPersoncode() != null && tcustPeopleJob.getAddress() != null) {
                    TcustPersonAddressKey tcustPersonAddressKey = new TcustPersonAddressKey(tcustPeopleJob.getPk().getPersoncode(), tcustPeopleJob.getAddress(), FormatDates.getDefaultExpiryTimestamp());
                    TcustPersonAddress tcustPersonAddress = TcustPersonAddress.find(PersistenceHelper.getEntityManager(), tcustPersonAddressKey);
                    tcustPeopleJob.addAddtionalInfo("addressaux", DirectionFull.getFullAddress(tcustPersonAddress));
                } else {
                    tcustPeopleJob.addAddtionalInfo("addressaux", null);
                }
            }
        }
    }

    /**
     * Metodo que se encarga de completar el nombre del beneficiario de la prenda del vehiculo
     *
     * @param lDetail
     * @throws Exception
     */
    private void completeVehicles(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TcustPeopleVehicle tcustPeopleVehicle = (TcustPeopleVehicle) obj;
            if (tcustPeopleVehicle.getPledgedpersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustPeopleVehicle.getPledgedpersoncode());
                tcustPeopleVehicle.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tcustPeopleVehicle.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el nombre del beneficiario de la prenda del vehiculo
     *
     * @param lDetail
     * @throws Exception
     */
    private void completeOtherAssets(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TcustPeopleOtherAssets tcustPeopleOtherAssets = (TcustPeopleOtherAssets) obj;
            if (tcustPeopleOtherAssets.getMortgagepersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustPeopleOtherAssets.getMortgagepersoncode());
                tcustPeopleOtherAssets.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tcustPeopleOtherAssets.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el nombre del vendedor en referencias comerciales
     *
     * @param lDetail
     * @throws Exception
     */
    private void completeTradeReferences(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TcustPeopleTradereferences tcustPeopleTradereferences = (TcustPeopleTradereferences) obj;
            if (tcustPeopleTradereferences.getTradepersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustPeopleTradereferences.getTradepersoncode());
                tcustPeopleTradereferences.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tcustPeopleTradereferences.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que completa el nombre de la tabla tgenesolicituderelationship
     *
     * @param lDetail
     * @throws Exception
     */
    private void completeSolicitudeRealtionShip(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TgeneSolicitudeRelationship tgeneSolicitudeRelationship = (TgeneSolicitudeRelationship) obj;
            if (tgeneSolicitudeRelationship.getPk().getPersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tgeneSolicitudeRelationship.getPk().getPersoncode());
                tgeneSolicitudeRelationship.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tgeneSolicitudeRelationship.addAddtionalInfo(name, null);
            }
        }
    }

    /**
     * Metodo que completa el nombre de la tabla tgeneaccountrelationship
     *
     * @param lDetail
     * @throws Exception
     */
    private void completeAccountRelationShip(List<Object> lDetail) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object obj : lDetail) {
            TgeneAccountRelationship tgeneAccountRelationship = (TgeneAccountRelationship) obj;
            if (tgeneAccountRelationship.getPk().getPersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tgeneAccountRelationship.getPk().getPersoncode());
                tgeneAccountRelationship.addAddtionalInfo(name, tcustPersonDetail.getName());
            } else {
                tgeneAccountRelationship.addAddtionalInfo(name, null);
            }
        }
    }
}
