/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.gis.data;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import systems.uom.common.USCustomary;
import org.geotools.data.FeatureSource;
import org.geotools.util.factory.Hints;

import org.opengis.feature.Feature;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.identity.FeatureId;

import usda.weru.gis.latlong.LatLong;
import usda.weru.weps.location.WindgenStation;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class WingenSelectAreaLookup extends AbstractLookup<WindgenSelectArea> {

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

    private static final String COLUMN_WMOID = "WMOID";
    private static final String COLUMN_WBAN = "WBAN";
    private static final String COLUMN_CNTRY = "CNTRY";
    private static final String COLUMN_ST = "ST";
    private static final String COLUMN_NAME = "NAME";
    private static final String COLUMN_LAT = "LAT_DD";
    private static final String COLUMN_LONG = "LONG_DD";

    /**
     *
     * @param source
     * @param feature
     * @return
     */
    @Override
    protected WindgenSelectArea create(FeatureSource<?, ?> source, Feature feature) {
        String name = getValue(feature, COLUMN_NAME, String.class);
        name = name != null ? name.trim() : null;

        String fid = feature.getIdentifier().getID();
        switch (name.toLowerCase()) {
            case "out":
                //no station for this polygon
                return new WindgenSelectArea(fid, SelectArea.Type.Out);
            case "int":
                //polygon says to interpolate
                return new WindgenSelectArea(fid, SelectArea.Type.Interpolated);
            default:
                String country = getValue(feature, COLUMN_CNTRY, String.class);
                String state = getValue(feature, COLUMN_ST, String.class);
                Integer wban = getValue(feature, COLUMN_WBAN, Integer.class);
                Double lat = getValue(feature, COLUMN_LAT, Double.class);
                Double lon = getValue(feature, COLUMN_LONG, Double.class);
                LatLong latlong = LatLong.valueOf(lat, lon, USCustomary.DEGREE_ANGLE);
                WindgenStation station = new WindgenStation(latlong, wban, country, state, name);
                return new WindgenSelectArea(fid, station);
        }
    }

    /**
     *
     * @param hints
     * @param ff
     * @param source
     * @param area
     * @return
     */
    @Override
    protected Filter createFilter(Hints hints, FilterFactory2 ff, FeatureSource<?, ?> source, WindgenSelectArea area) {

        try {

            FeatureId fid = ff.featureId(area.fid);
            Set<FeatureId> fids = new HashSet<FeatureId>();
            fids.add(fid);
            Filter filter = ff.id(fids);

            return filter;
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Unable to create feature id filter.", e);
            return null;
        }
    }

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

    @Override
    protected boolean supports(FeatureSource<?, ?> source) {
        if (hasColumn(source, COLUMN_WBAN, Integer.class) && hasColumn(source, COLUMN_WMOID, Integer.class)) {
            return true;
        }

        return false;
    }

}
