package usda.weru.gis.data;

import java.util.HashMap;
import java.util.Map;
import org.geotools.data.FeatureSource;
import org.opengis.feature.Feature;
import org.opengis.feature.type.Name;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class TerritoryLookup extends AbstractLookup<Territory> {
    //countries

    /**
     *
     */
    public static final String COLUMN_CNTRY_NAME = "CNTRY_NAME";

    /**
     *
     */
    public static final String COLUMN_FIPS_CNTRY = "FIPS_CNTRY";

    //states
    /**
     *
     */
    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";

    //counties
    /**
     *
     */
    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";

    private final Map<Name, TerritoryType> c_typeIndex;

    /**
     *
     */
    public TerritoryLookup() {
        c_typeIndex = new HashMap<Name, TerritoryType>();
    }

    /**
     *
     * @param source
     * @param feature
     * @return
     */
    @Override
    protected Territory create(FeatureSource<?, ?> source, Feature feature) {
        switch (getType(source.getName())) {
            case Country:
                String countryName = getValue(feature, COLUMN_CNTRY_NAME, String.class);
                String countryFips = getValue(feature, COLUMN_FIPS_CNTRY, String.class);
                Territory country = new Territory(TerritoryType.Country, countryName);
                return country;
            case State:
                String stateName = getValue(feature, COLUMN_STATE_NAME, String.class);
                Territory state = new Territory(TerritoryType.State, stateName);
                return state;
            case County:
                String countyName = getValue(feature, COLUMN_COUNTY_NAME, String.class);
                String countyDescription = getValue(feature, COLUMN_COUNTY_LSAD, String.class);
                Territory county = new Territory(TerritoryType.County, countyName, countyDescription);
                return county;
        }

        return null;
    }

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

    @Override
    protected boolean supports(FeatureSource<?, ?> source) {
        if (hasColumn(source, COLUMN_CNTRY_NAME, String.class) && hasColumn(source, COLUMN_FIPS_CNTRY, String.class)) {
            //this this is a country source
            c_typeIndex.put(source.getName(), TerritoryType.Country);
            return true;
        } else if (hasColumn(source, COLUMN_STATE_D00_, Long.class) && hasColumn(source, COLUMN_STATE_D00_I, Long.class)) {
            c_typeIndex.put(source.getName(), TerritoryType.State);
            return true;
        } else if (hasColumn(source, COLUMN_COUNTY_D00_, Long.class) && hasColumn(source, COLUMN_COUNTY_D00_I, Long.class)) {
            c_typeIndex.put(source.getName(), TerritoryType.County);
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @param name
     * @return
     */
    protected TerritoryType getType(Name name) {
        return c_typeIndex.get(name);
    }

}
