package usda.weru.gis.data;

import org.apache.log4j.Logger;
import org.geotools.data.FeatureSource;
import org.geotools.util.factory.Hints;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.filter.text.cql2.CQLException;
import org.opengis.feature.Feature;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import usda.weru.weps.location.Site;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class SiteCountyLookup extends AbstractLookup<Site.Level2> {

    private static final Logger LOGGER = Logger.getLogger(SiteCountyLookup.class);

    /**
     *
     */
    public static final String COLUMN_COUNTY_D00_ = "CO99_D00_";

    /**
     *
     */
    public static final String COLUMN_COUNTY_D00_I = "CO99_D00_I";

    /**
     *
     */
    public static final String COLUMN_COUNTY_NAME = "NAME";

    /**
     *
     */
    public static final String COLUMN_COUNTY_LSAD = "LSAD_TRANS";

    /**
     *
     */
    public static final String COLUMN_COUNTY_STATE = "STATE";

    /**
     *
     */
    public static final String COLUMN_COUNTY_COUNTY = "COUNTY";

    /**
     *
     * @param source
     * @param feature
     * @return
     */
    @Override
    protected Site.Level2 create(FeatureSource<?, ?> source, Feature feature) {
        String stateFips = getValue(feature, COLUMN_COUNTY_STATE, String.class);
        String countyFips = getValue(feature, COLUMN_COUNTY_COUNTY, String.class);

        if (stateFips == null) {
            return null;
        }

        stateFips = stateFips.trim();
        countyFips = countyFips.trim();
/**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable specific assertions (feed the virtual machine 
                 * the -ea argument).
                 */
//        assert stateFips.length() == 2;

        String fips = "US" + stateFips;

        Site.Level1 state = (Site.Level1) Site.valueOfFIPS(fips);

        return state.getSubDivision(countyFips);

    }

    /**
     *
     * @param hints
     * @param factory
     * @param source
     * @param county
     * @return
     */
    @Override
    protected Filter createFilter(final Hints hints, final FilterFactory2 factory,
            final FeatureSource<?, ?> source, final Site.Level2 county) {
        try {
            //assume the parent is the state
            Site<?> state = county.getParent();
            String stateCode = state.getFIPS();

            String countyCode = county.getPrimaryKey();

            //returns features that match the state and county
            String cql = COLUMN_COUNTY_STATE + " = '" + stateCode + "' AND "
                    + COLUMN_COUNTY_COUNTY + " = '" + countyCode + "'";
            Filter filter = CQL.toFilter(cql);
            return filter;
        } catch (CQLException ce) {
            LOGGER.warn("Unable to create CQL query.", ce);
            return null;
        }
    }

    /**
     *
     * @return
     */
    @Override
    protected Class<Site.Level2> getSupportedClass() {
        return Site.Level2.class;
    }

    @Override
    protected boolean supports(FeatureSource<?,?> source) {
        return hasColumn(source, COLUMN_COUNTY_COUNTY, String.class)
                && hasColumn(source, COLUMN_COUNTY_D00_, Long.class)
                && hasColumn(source, COLUMN_COUNTY_D00_I, Long.class);
    }
}
