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;
import usda.weru.weps.location.Site.Level1;

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

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

    /**
     *
     */
    public static final String COLUMN_STATE_D00_ = "ST99_D00_";

    /**
     *
     */
    public static final String COLUMN_STATE_D00_I = "ST99_D00_I";

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

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

    /**
     *
     * @param source
     * @param feature
     * @return
     */
    @Override
    protected Level1 create(FeatureSource<?,?> source, Feature feature) {
        String stateCode = getValue(feature, COLUMN_STATE_STATE, String.class);

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

        stateCode = stateCode.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 stateCode.length() == 2;

        String fips = "US" + stateCode;

        return (Site.Level1) Site.valueOfFIPS(fips);
    }

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

    /**
     *
     * @param hints
     * @param factory
     * @param source
     * @param state
     * @return
     */
    @Override
    protected Filter createFilter(final Hints hints, final FilterFactory2 factory,
            final FeatureSource<?,?> source, final Site.Level1 state) {
        try {
            if (!Site.UNITED_STATES.equals(state.getParent())) {
                return null;
            }
            String stateCode = state.getFIPS();

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

    @Override
    protected boolean supports(FeatureSource<?,?> source) {
        return hasColumn(source, COLUMN_STATE_STATE, String.class)
                && hasColumn(source, COLUMN_STATE_D00_, Long.class)
                && hasColumn(source, COLUMN_STATE_D00_I, Long.class);
    }
}
