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.pcustomer.gene.TcustPersonAddress;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.people.TcustPeople;
import com.fp.persistence.pcustomer.people.TcustPeopleRealAssets;
import com.fp.persistence.pgeneral.gene.TgeneCanton;
import com.fp.persistence.pgeneral.gene.TgeneCity;
import com.fp.persistence.pgeneral.gene.TgeneCountry;
import com.fp.persistence.pgeneral.gene.TgeneParroquia;
import com.fp.persistence.pgeneral.gene.TgeneProvince;
import com.fp.person.db.DataHelper;
import java.util.List;

/**
 * Clase que se encarga de completar el nombre de pais, provincia y ciudad asociados a una persona.
 * @author Wilson Sigcho
 * @version 2.1
 */
public class CountryProvinceCity extends QueryRule { // NOPMD by jorge on 12/10/10 22:28

    private String country = "countrydesc";
    private String province = "provincedesc";
    private String canton = "cantondesc";
    private String city = "citydesc";
    private String parroquia = "parroquiadesc";
    private String nacionality ="nacionalitydesc";

    /**
     * Metodo que agrega el nombre de pais, provincia y cuidad asociadas a 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);
        Object people = (Object) response.get("TCUSTPEOPLE");
        List<Object> address = (List<Object>) response.get("TCUSTPERSONADDRESS");
        List<Object> realassets = (List<Object>) response.get("TCUSTPEOPLEREALASSETS");
        this.completePeople(people);
        this.completeAddress(address);
        this.completeRealAssets(realassets);
        return pQueryRequest;
    }

    /**
     * Metodo que se encraga de completar el nombre de pais, provincia, cuidad y parroquia de nacimiento de una persona.
     * @param detail Objeto que posee un registro consultado en la tabla TcustPeople.
     * @throws Exception
     */
    private void completePeople(Object detail) throws Exception { // NOPMD by jorge on 12/10/10 22:27
        if (detail == null) {
            return;
        }
        TcustPeople tcustPeople = (TcustPeople) detail;
        if (tcustPeople.getBirthcountrycode() != null) {
            TgeneCountry tgeneCountry = DataHelper.getInstance().getTgeneCountry(tcustPeople.getBirthcountrycode());
            tcustPeople.addAddtionalInfo(country, tgeneCountry.getDescription());
        } else {
            tcustPeople.addAddtionalInfo(country, null);
        }
        if (tcustPeople.getBirthcountrycode() != null && tcustPeople.getBirthprovincecode() != null) {
            TgeneProvince tgeneProvince = DataHelper.getInstance().getTgeneProvince(tcustPeople.getBirthcountrycode(), tcustPeople.getBirthprovincecode());
            tcustPeople.addAddtionalInfo(province, tgeneProvince.getDescription());
        } else {
            tcustPeople.addAddtionalInfo(province, null);
        }
        if (tcustPeople.getBirthcountrycode() != null && tcustPeople.getBirthprovincecode() != null && tcustPeople.getBirthcantoncode() != null) {
            TgeneCanton tgeneCanton = DataHelper.getInstance().getTgeneCanton(tcustPeople.getBirthcountrycode(),
                    tcustPeople.getBirthprovincecode(), tcustPeople.getBirthcantoncode());
            tcustPeople.addAddtionalInfo(canton, tgeneCanton.getDescription());
        } else {
            tcustPeople.addAddtionalInfo(canton, null);
        }
        if (tcustPeople.getBirthcountrycode() != null && tcustPeople.getBirthprovincecode() != null
                && tcustPeople.getBirthcantoncode() != null && tcustPeople.getBirthcitycode() != null) {
            TgeneCity tgeneCity = DataHelper.getInstance().getTgeneCity(tcustPeople.getBirthcountrycode(),
                    tcustPeople.getBirthprovincecode(), tcustPeople.getBirthcantoncode(), tcustPeople.getBirthcitycode());
            tcustPeople.addAddtionalInfo(city, tgeneCity.getDescription());
        } else {
            tcustPeople.addAddtionalInfo(city, null);
        }
        if (tcustPeople.getBirthcountrycode() != null && tcustPeople.getBirthprovincecode() != null
                && tcustPeople.getBirthcantoncode() != null && tcustPeople.getBirthparroquiacode() != null) {
            TgeneParroquia tgeneParroquia = DataHelper.getInstance().getTgeneParroquia(tcustPeople.getBirthcountrycode(),
                    tcustPeople.getBirthprovincecode(), tcustPeople.getBirthcantoncode(), tcustPeople.getBirthparroquiacode());
            tcustPeople.addAddtionalInfo(parroquia, tgeneParroquia.getDescription());
        } else {
            tcustPeople.addAddtionalInfo(parroquia, null);
        }
        if (tcustPeople.getNacionality() != null) {
            TgeneCountry tgeneCountry = DataHelper.getInstance().getTgeneCountry(tcustPeople.getNacionality());
            tcustPeople.addAddtionalInfo(nacionality, tgeneCountry.getCitizenship());
        } else {
            tcustPeople.addAddtionalInfo(nacionality, null);
        }
    }

    /**
     * Metodo que se encraga de completar el nombre de pais, provincia y cuidad asociados a direcciones de una persona.
     * @param lDetail Objeto que posee una lista de los registros consultados en la tabla TcustPersonAddress.
     * @throws Exception
     */
    private void completeAddress(List<Object> lDetail) throws Exception { // NOPMD by jorge on 12/10/10 22:27
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPersonAddress tcustPersonAddress = (TcustPersonAddress) object;
            if (tcustPersonAddress.getCountrycode() != null) {
                TgeneCountry tgeneCountry = DataHelper.getInstance().getTgeneCountry(tcustPersonAddress.getCountrycode());
                tcustPersonAddress.addAddtionalInfo(country, tgeneCountry.getDescription());
            } else {
                tcustPersonAddress.addAddtionalInfo(country, null);
            }
            if (tcustPersonAddress.getCountrycode() != null && tcustPersonAddress.getProvincecode() != null) {
                TgeneProvince tgeneProvince = DataHelper.getInstance().getTgeneProvince(tcustPersonAddress.getCountrycode(),
                        tcustPersonAddress.getProvincecode());
                tcustPersonAddress.addAddtionalInfo(province, tgeneProvince.getDescription());
            } else {
                tcustPersonAddress.addAddtionalInfo(province, null);
            }
            if (tcustPersonAddress.getCountrycode() != null && tcustPersonAddress.getProvincecode() != null
                    && tcustPersonAddress.getCantoncode() != null) {
                TgeneCanton tgeneCanton = DataHelper.getInstance().getTgeneCanton(tcustPersonAddress.getCountrycode(),
                        tcustPersonAddress.getProvincecode(), tcustPersonAddress.getCantoncode());
                tcustPersonAddress.addAddtionalInfo(canton, tgeneCanton.getDescription());
            } else {
                tcustPersonAddress.addAddtionalInfo(canton, null);
            }
            if (tcustPersonAddress.getCountrycode() != null && tcustPersonAddress.getProvincecode() != null
                    && tcustPersonAddress.getCantoncode() != null && tcustPersonAddress.getCitycode() != null) {
                TgeneCity tgeneCity = DataHelper.getInstance().getTgeneCity(tcustPersonAddress.getCountrycode(), tcustPersonAddress.getProvincecode(), tcustPersonAddress.getCantoncode(), tcustPersonAddress.getCitycode());
                tcustPersonAddress.addAddtionalInfo(city, tgeneCity.getDescription());
            } else {
                tcustPersonAddress.addAddtionalInfo(city, null);
            }
        }
    }

    /**
     * Metodo que se encraga de completar el nombre de pais, provincia, parroquia y cuidad de inmuebles asociados a una persona.
     * @param lDetail Objeto que posee una lista de los registros consultados en la tabla TcustPeopleRealAssets.
     * @throws Exception
     */
    private void completeRealAssets(List<Object> lDetail) throws Exception { // NOPMD by jorge on 12/10/10 22:27
        if (lDetail == null) {
            return;
        }
        for (Object object : lDetail) {
            TcustPeopleRealAssets tcustPeopleRealAssets = (TcustPeopleRealAssets) object;
            if (tcustPeopleRealAssets.getCountrycode() != null) {
                TgeneCountry tgeneCountry = DataHelper.getInstance().getTgeneCountry(tcustPeopleRealAssets.getCountrycode());
                tcustPeopleRealAssets.addAddtionalInfo(country, tgeneCountry.getDescription());
            } else {
                tcustPeopleRealAssets.addAddtionalInfo(country, null);
            }
            if (tcustPeopleRealAssets.getCountrycode() != null && tcustPeopleRealAssets.getProvincecode() != null) {
                TgeneProvince tgeneProvince = DataHelper.getInstance().getTgeneProvince(tcustPeopleRealAssets.getCountrycode(), tcustPeopleRealAssets.getProvincecode());
                tcustPeopleRealAssets.addAddtionalInfo(province, tgeneProvince.getDescription());
            } else {
                tcustPeopleRealAssets.addAddtionalInfo(province, null);
            }
            if (tcustPeopleRealAssets.getCountrycode() != null && tcustPeopleRealAssets.getProvincecode() != null
                    && tcustPeopleRealAssets.getCantoncode() != null) {
                TgeneCanton tgeneCanton = DataHelper.getInstance().getTgeneCanton(tcustPeopleRealAssets.getCountrycode(),
                        tcustPeopleRealAssets.getProvincecode(), tcustPeopleRealAssets.getCantoncode());
                tcustPeopleRealAssets.addAddtionalInfo(canton, tgeneCanton.getDescription());
            } else {
                tcustPeopleRealAssets.addAddtionalInfo(canton, null);
            }
            if (tcustPeopleRealAssets.getCountrycode() != null && tcustPeopleRealAssets.getProvincecode() != null
                    && tcustPeopleRealAssets.getCantoncode() != null && tcustPeopleRealAssets.getCitycode() != null) {
                TgeneCity tgeneCity = DataHelper.getInstance().getTgeneCity(tcustPeopleRealAssets.getCountrycode(),
                        tcustPeopleRealAssets.getProvincecode(), tcustPeopleRealAssets.getCantoncode(), tcustPeopleRealAssets.getCitycode());
                tcustPeopleRealAssets.addAddtionalInfo(city, tgeneCity.getDescription());
            } else {
                tcustPeopleRealAssets.addAddtionalInfo(city, null);
            }
            if (tcustPeopleRealAssets.getCountrycode() != null && tcustPeopleRealAssets.getProvincecode() != null
                    && tcustPeopleRealAssets.getCantoncode() != null && tcustPeopleRealAssets.getParroquiacode() != null) {
                TgeneParroquia tgeneParroquia = DataHelper.getInstance().getTgeneParroquia(tcustPeopleRealAssets.getCountrycode(),
                        tcustPeopleRealAssets.getProvincecode(), tcustPeopleRealAssets.getCantoncode(), tcustPeopleRealAssets.getParroquiacode());
                tcustPeopleRealAssets.addAddtionalInfo(parroquia, tgeneParroquia.getDescription());
            } else {
                tcustPeopleRealAssets.addAddtionalInfo(parroquia, null);
            }
            if (tcustPeopleRealAssets.getMortgagepersoncode() != null) {
                TcustPersonDetail tcustPersonDetail = DataHelper.getInstance().getTcustPersonDetail(tcustPeopleRealAssets.getMortgagepersoncode());
                tcustPeopleRealAssets.addAddtionalInfo("name", tcustPersonDetail.getName());
            } else {
                tcustPeopleRealAssets.addAddtionalInfo("name", null);
            }
        }
    }
}
