package com.fp.person.rules.save;

import java.util.List;

import com.fp.dto.Response;
import com.fp.dto.save.SaveBean;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.pcustomer.balance.TcustPersonBalance;
import com.fp.persistence.pcustomer.company.TcustCompany;
import com.fp.persistence.pcustomer.company.TcustCompanyBranch;
import com.fp.persistence.pcustomer.company.TcustCompanyBranchKey;
import com.fp.persistence.pcustomer.company.TcustCompanyClient;
import com.fp.persistence.pcustomer.company.TcustCompanyClientKey;
import com.fp.persistence.pcustomer.company.TcustCompanyFinancial;
import com.fp.persistence.pcustomer.company.TcustCompanyFinancialKey;
import com.fp.persistence.pcustomer.company.TcustCompanyKey;
import com.fp.persistence.pcustomer.company.TcustCompanyManagers;
import com.fp.persistence.pcustomer.company.TcustCompanyManagersKey;
import com.fp.persistence.pcustomer.company.TcustCompanyProducts;
import com.fp.persistence.pcustomer.company.TcustCompanyProductsKey;
import com.fp.persistence.pcustomer.company.TcustCompanyProvider;
import com.fp.persistence.pcustomer.company.TcustCompanyProviderKey;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.people.TcustPeopleAssociation;
import com.fp.persistence.pcustomer.people.TcustPeopleAssociationKey;
import com.fp.persistence.pcustomer.people.TcustPeopleFixedIncome;
import com.fp.persistence.pcustomer.people.TcustPeopleFixedIncomeKey;
import com.fp.persistence.pcustomer.people.TcustPeopleLoan;
import com.fp.persistence.pcustomer.people.TcustPeopleLoanKey;
import com.fp.persistence.pcustomer.people.TcustPeopleOtherAssets;
import com.fp.persistence.pcustomer.people.TcustPeopleOtherAssetsKey;
import com.fp.persistence.pcustomer.people.TcustPeopleRealAssets;
import com.fp.persistence.pcustomer.people.TcustPeopleRealAssetsKey;
import com.fp.persistence.pcustomer.people.TcustPeopleVehicle;
import com.fp.persistence.pcustomer.people.TcustPeopleVehicleKey;

/**
 * 
 * Clase se encarga de completar el codigo para una nueva persona juridica.
 * 
 * @author Wilson Sigcho
 * @version 2.1
 */
public class CompleteCompanyCode extends CompletePersonAndCompany {

    /**
     * Metodo que se encarga de completar el codigo de compania para su creacion cuando un save es requerido.
     * 
     * @param pSaveRequest Objeto que posee el save requerido.
     * @return SaveRequest
     * @throws Exception
     */
    @Override
    public SaveRequest normalProcess(SaveRequest pSaveRequest) throws Exception {
        TcustPersonDetail tcustPersonDetail = (TcustPersonDetail) pSaveRequest.getSaveBeanModifiedObject("TCUSTPERSONDETAIL");
        TcustCompany tcustCompany = (TcustCompany) pSaveRequest.getSaveBeanModifiedObject("TCUSTCOMPANY");
        TcustCompanyFinancial tcustCompanyFinancial = (TcustCompanyFinancial) pSaveRequest.getSaveBeanModifiedObject("TCUSTCOMPANYFINANCIAL");
//        TcustPersonBalance tcustPersonBalance = (TcustPersonBalance) pSaveRequest.getSaveBeanModifiedObject("TCUSTPERSONBALANCE");
        SaveBean sbTcustPersonBalance = (SaveBean) pSaveRequest.get("com.fp.persistence.pcustomer.balance.TcustPersonBalance");
        TcustPersonBalance tcustPersonBalance=null;
        if(sbTcustPersonBalance!=null)
        tcustPersonBalance = (TcustPersonBalance) sbTcustPersonBalance.getModifiedObject();
        List<Object> tcustPersonAddress = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPERSONADDRESS");
        List<Object> tcustPersonPhone = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPERSONPHONE");
        List<Object> bankreference = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPERSONBANKREFERENCE");
        List<Object> personalreference = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPERSONPERSONALREFERENCE");
        List<Object> products = pSaveRequest.getSaveBeanModifiedRecords("TCUSTCOMPANYPRODUCTS");
        List<Object> managers = pSaveRequest.getSaveBeanModifiedRecords("TCUSTCOMPANYMANAGERS");
        List<Object> branch = pSaveRequest.getSaveBeanModifiedRecords("TCUSTCOMPANYBRANCH");
        List<Object> provider = pSaveRequest.getSaveBeanModifiedRecords("TCUSTCOMPANYPROVIDER");
        List<Object> client = pSaveRequest.getSaveBeanModifiedRecords("TCUSTCOMPANYCLIENT");
        List<Object> loan = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPEOPLELOAN");
        List<Object> vehicles = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPEOPLEVEHICLE");
        List<Object> otherassets = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPEOPLEOTHERASSETS");
        List<Object> realassets = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPEOPLEREALASSETS");
        List<Object> fixedincome = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPEOPLEFIXEDINCOME");
        List<Object> association = pSaveRequest.getSaveBeanModifiedRecords("TCUSTPEOPLEASSOCIATION");
        // List<Object> balancefinancial = pSaveRequest.getSaveBeanModifiedRecords("TCUSTBALANCEFINANCIAL");
        // List<FileHelper> fileItem = (List<FileHelper>) pSaveRequest.get("file");
        if ((tcustPersonDetail != null) && (tcustPersonDetail.getPk().getPersoncode() == null)) {
            Integer personcode = this.getPersonCode();
            this.createPerson(pSaveRequest, personcode);
            this.completePersonDetail(pSaveRequest, tcustPersonDetail, personcode);           
            this.completeCompany(tcustCompany, personcode,pSaveRequest);            
            this.completeFinancial(tcustCompanyFinancial, personcode);
            this.completeAddress(tcustPersonAddress, personcode);
            this.completePhone(tcustPersonPhone, personcode);
            this.completeBankReference(bankreference, personcode);
            this.completePersonalReference(personalreference, personcode);
            this.completeProducts(products, personcode);
            this.completeManagers(managers, personcode);
            this.completeBranch(branch, personcode);
            this.completeProvider(provider, personcode);
            this.completeClient(client, personcode);
            this.completeLoan(loan, personcode);
            this.completeVehicles(vehicles, personcode);
            this.completeOtherAssets(otherassets, personcode);
            this.completeRealAssets(realassets, personcode);
            this.completeFixedIncome(fixedincome, personcode);
            this.completeAssociation(association, personcode);
            this.completeBalance(tcustPersonBalance, personcode, pSaveRequest);
            Response response = pSaveRequest.getResponse();
            response.put("personcode", personcode);
            response.put("personname", tcustPersonDetail.getName());
        }
        this.completeAditionalInfo(pSaveRequest, tcustPersonDetail);
        this.balanceCode(tcustPersonBalance, pSaveRequest);
        return pSaveRequest;
    }

    /**
     * Metodo que me permmite revertir el proceso.
     * 
     * @param pSaveRequest Objeto que posee el save requerido.
     * @return pSaveRequest
     * @throws Exception
     */
    @Override
    public SaveRequest reverseProcess(SaveRequest pSaveRequest) throws Exception {
        return pSaveRequest;
    }

    /**
     * Completa el codigo de la nueva persona juridica en la tabla TcustCompany.
     * 
     * @param tcustCompany Objeto que posee el registro a guardar en la tabla TcustCompany.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeCompany(TcustCompany tcustCompany, Integer personcode,SaveRequest pSaverequest) throws Exception {
        TcustCompanyKey tcustCompanyKey = new TcustCompanyKey();
        tcustCompanyKey.setPersoncode(personcode);
        if(tcustCompany==null){
        	TcustCompany tcusCompanyPros = new TcustCompany();
        	tcusCompanyPros.setPk(tcustCompanyKey);
        	tcusCompanyPros.setIngressuser(pSaverequest.getUser());
        	pSaverequest.getResponse().remove("TCUSTCOMPANY");
            pSaverequest.putSaveBean("TCUSTCOMPANY", tcusCompanyPros, 2);
        	
        }else{
        	tcustCompany.setPk(tcustCompanyKey);
            tcustCompany.setIngressuser(pSaverequest.getUser());
        }
        
    }    

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustCompanyManagers
     * 
     * @param lDetail Objeto que posee una lista de los registros a guardar en la tabla TcustCompanyManagers.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeManagers(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustCompanyManagers tcustCompanyManagers = (TcustCompanyManagers) object;
            if ((tcustCompanyManagers != null) && (tcustCompanyManagers.getPk().getPersoncode() == null)) {
                TcustCompanyManagersKey tcustCompanyManagersKey = tcustCompanyManagers.getPk();
                tcustCompanyManagersKey.setPersoncode(personcode);
                tcustCompanyManagers.setPk(tcustCompanyManagersKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustCompanyBranch
     * 
     * @param lDetail Objeto que posee una lista de los registros a guardar en la tabla TcustCompanyBranch.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeBranch(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustCompanyBranch tcustCompanyBranch = (TcustCompanyBranch) object;
            if ((tcustCompanyBranch != null) && (tcustCompanyBranch.getPk().getPersoncode() == null)) {
                TcustCompanyBranchKey tcustCompanyBranchKey = new TcustCompanyBranchKey();
                tcustCompanyBranchKey.setPersoncode(personcode);
                tcustCompanyBranchKey.setBranchsequence(tcustCompanyBranch.getPk().getBranchsequence());
                tcustCompanyBranch.setPk(tcustCompanyBranchKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustCompanyProducts
     * 
     * @param lDetail Objeto que posee una lista de los registros a guardar en la tabla TcustCompanyProducts.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeProducts(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustCompanyProducts tcustCompanyProducts = (TcustCompanyProducts) object;
            if ((tcustCompanyProducts != null) && (tcustCompanyProducts.getPk().getPersoncode() == null)) {
                TcustCompanyProductsKey tcustCompanyProductsKey = new TcustCompanyProductsKey();
                tcustCompanyProductsKey.setPersoncode(personcode);
                tcustCompanyProductsKey.setProductsequence(tcustCompanyProducts.getPk().getProductsequence());
                tcustCompanyProducts.setPk(tcustCompanyProductsKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustCompanyProvider
     * 
     * @param lDetail Objeto que posee una lista de los registros a guardar en la tabla TcustCompanyProvider.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeProvider(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustCompanyProvider tcustCompanyProvider = (TcustCompanyProvider) object;
            if ((tcustCompanyProvider != null) && (tcustCompanyProvider.getPk().getPersoncode() == null)) {
                TcustCompanyProviderKey tcustCompanyProviderKey = new TcustCompanyProviderKey();
                tcustCompanyProviderKey.setPersoncode(personcode);
                tcustCompanyProviderKey.setProvidersequence(tcustCompanyProvider.getPk().getProvidersequence());
                tcustCompanyProvider.setPk(tcustCompanyProviderKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustCompanyClient
     * 
     * @param lDetail Objeto que posee una lista de los registros a guardar en la tabla TcustCompanyClient.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeClient(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustCompanyClient tcustCompanyClient = (TcustCompanyClient) object;
            if ((tcustCompanyClient != null) && (tcustCompanyClient.getPk().getPersoncode() == null)) {
                TcustCompanyClientKey tcustCompanyClientKey = new TcustCompanyClientKey();
                tcustCompanyClientKey.setPersoncode(personcode);
                tcustCompanyClientKey.setClientsequence(tcustCompanyClient.getPk().getClientsequence());
                tcustCompanyClient.setPk(tcustCompanyClientKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustCompanyFinancial
     * 
     * @param lDetail Objeto que posee una lista de los registros a guardar en la tabla TcustCompanyFinancial.
     * @param personcode Codigo de la nueva persona juridica a ser guardada.
     * @throws Exception
     */
    private void completeFinancial(TcustCompanyFinancial tcustCompanyFinancial, Integer personcode) throws Exception {
        if ((tcustCompanyFinancial != null) && (tcustCompanyFinancial.getPk().getPersoncode() == null)) {
            TcustCompanyFinancialKey tcustCompanyFinancialKey = new TcustCompanyFinancialKey();
            tcustCompanyFinancialKey.setPersoncode(personcode);
            tcustCompanyFinancial.setPk(tcustCompanyFinancialKey);
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustPeopleLoan
     * 
     * @param lDetail
     * @param personcode
     * @throws Exception
     */
    private void completeLoan(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleLoan tcustPeopleLoan = (TcustPeopleLoan) object;
            if ((tcustPeopleLoan != null) && (tcustPeopleLoan.getPk().getPersoncode() == null)) {
                TcustPeopleLoanKey tcustPeopleLoanKey = new TcustPeopleLoanKey();
                tcustPeopleLoanKey.setPersoncode(personcode);
                tcustPeopleLoanKey.setLoansequence(tcustPeopleLoan.getPk().getLoansequence());
                tcustPeopleLoan.setPk(tcustPeopleLoanKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustPeopleVehicle
     * 
     * @param lDetail
     * @param personcode
     * @throws Exception
     */
    private void completeVehicles(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleVehicle tcustPeopleVehicle = (TcustPeopleVehicle) object;
            if ((tcustPeopleVehicle != null) && (tcustPeopleVehicle.getPk().getPersoncode() == null)) {
                TcustPeopleVehicleKey tcustPeopleVehicleKey = new TcustPeopleVehicleKey();
                tcustPeopleVehicleKey.setPersoncode(personcode);
                tcustPeopleVehicleKey.setVehiclesequence(tcustPeopleVehicle.getPk().getVehiclesequence());
                tcustPeopleVehicle.setPk(tcustPeopleVehicleKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustPeopleOtherAssets
     * 
     * @param lDetail
     * @param personcode
     * @throws Exception
     */
    private void completeOtherAssets(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleOtherAssets tcustPeopleOtherAssets = (TcustPeopleOtherAssets) object;
            if ((tcustPeopleOtherAssets != null) && (tcustPeopleOtherAssets.getPk().getPersoncode() == null)) {
                TcustPeopleOtherAssetsKey tcustPeopleOtherAssetsKey = new TcustPeopleOtherAssetsKey();
                tcustPeopleOtherAssetsKey.setPersoncode(personcode);
                tcustPeopleOtherAssetsKey.setAssetssequence(tcustPeopleOtherAssets.getPk().getAssetssequence());
                tcustPeopleOtherAssets.setPk(tcustPeopleOtherAssetsKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustPeopleRealAssets
     * 
     * @param lDetail
     * @param personcode
     * @throws Exception
     */
    private void completeRealAssets(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleRealAssets tcustPeopleRealAssets = (TcustPeopleRealAssets) object;
            if ((tcustPeopleRealAssets != null) && (tcustPeopleRealAssets.getPk().getPersoncode() == null)) {
                TcustPeopleRealAssetsKey tcustPeopleRealAssetsKey = new TcustPeopleRealAssetsKey();
                tcustPeopleRealAssetsKey.setPersoncode(personcode);
                tcustPeopleRealAssetsKey.setAssetssequence(tcustPeopleRealAssets.getPk().getAssetssequence());
                tcustPeopleRealAssets.setPk(tcustPeopleRealAssetsKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustPeopleFixedIncome
     * 
     * @param lDetail
     * @param personcode
     * @throws Exception
     */
    private void completeFixedIncome(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleFixedIncome tcustPeopleFixedIncome = (TcustPeopleFixedIncome) object;
            if ((tcustPeopleFixedIncome != null) && (tcustPeopleFixedIncome.getPk().getPersoncode() == null)) {
                TcustPeopleFixedIncomeKey tcustPeopleFixedIncomeKey = new TcustPeopleFixedIncomeKey();
                tcustPeopleFixedIncomeKey.setPersoncode(personcode);
                tcustPeopleFixedIncomeKey.setFixedincomesequence(tcustPeopleFixedIncome.getPk().getFixedincomesequence());
                tcustPeopleFixedIncome.setPk(tcustPeopleFixedIncomeKey);
            }
        }
    }

    /**
     * Metodo que se encarga de completar el personcode en la tabla TcustPeopleAssociation
     * 
     * @param lDetail
     * @param personcode
     * @throws Exception
     */
    private void completeAssociation(List<Object> lDetail, Integer personcode) throws Exception {
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleAssociation tcustPeopleAssociation = (TcustPeopleAssociation) object;
            if ((tcustPeopleAssociation != null) && (tcustPeopleAssociation.getPk().getPersoncode() == null)) {
                TcustPeopleAssociationKey tcustPeopleAssociationKey = new TcustPeopleAssociationKey();
                tcustPeopleAssociationKey.setPersoncode(personcode);
                tcustPeopleAssociationKey.setAssociationsequence(tcustPeopleAssociation.getPk().getAssociationsequence());
                tcustPeopleAssociation.setPk(tcustPeopleAssociationKey);
            }
        }
    }
}
