package org.oskari.service.mvt;

import java.math.BigDecimal;

import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.Name;

import com.wdtinc.mapbox_vector_tile.VectorTile.Tile.Feature.Builder;
import com.wdtinc.mapbox_vector_tile.adapt.jts.IUserDataConverter;
import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps;

import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;

public class SimpleFeatureConverter implements IUserDataConverter {

    private static final Logger LOG = LogFactory.getLogger(SimpleFeatureConverter.class);

    @Override
    public void addTags(Object userData, MvtLayerProps layerProps, Builder featureBuilder) {
        if (!(userData instanceof SimpleFeature)) {
            LOG.debug("userData not a SimpleFeature!");
            return;
        }

        SimpleFeature f = (SimpleFeature) userData;
        Name geomPropertyName = f.getDefaultGeometryProperty().getName();
        String id = f.getID();
        for (Property p : f.getProperties()) {
            Name name = p.getName();
            if (geomPropertyName.equals(name)) {
                // Skip geometry
                continue;
            }
            String prop = name.getLocalPart();
            Object value = p.getValue();
            if (value == null) {
                continue;
            }
            if (value instanceof BigDecimal) {
                value = ((BigDecimal) value).doubleValue();
            }

            int valueIndex = layerProps.addValue(value);
            if (valueIndex < 0) {
                // Value wasn't IN (Boolean,Integer,Long,Float,Double,String)
                // => Can't be encoded to MVT
                // TODO: Check how dates and datetimes are handled
                LOG.debug("Skipping", id + "." + prop,
                        "value type not valid for MVT encoding, class:", value.getClass());
                continue;
            }

            featureBuilder.addTags(layerProps.addKey(prop));
            featureBuilder.addTags(valueIndex);
        }
    }

}

@@ -608,16 +608,66 @@
 lass);%0A%0A
+    private static final String KEY_ID = %22_oid%22;%0A%0A
     @Ove
@@ -672,16 +672,16 @@
 verride%0A
-
     publ
@@ -895,33 +895,32 @@
 turn;%0A        %7D%0A
-%0A
         SimpleFe
@@ -955,16 +955,96 @@
 erData;%0A
+%0A        String id = f.getID();%0A        addId(layerProps, featureBuilder, id);%0A%0A
         
@@ -1113,39 +1113,8 @@
 ();%0A
-        String id = f.getID();%0A
     
@@ -2194,10 +2194,531 @@
 %0A    %7D%0A%0A
+    private void addId(MvtLayerProps layerProps, Builder featureBuilder, String id) %7B%0A        if (id == null %7C%7C id.isEmpty()) %7B%0A            return;%0A        %7D%0A        int valueIndex = layerProps.addKey(id);%0A        if (valueIndex %3C 0) %7B%0A            // This shouldn't happen as id is non-empty String%0A            LOG.warn(%22Could not add id: %22 + id);%0A            return;%0A        %7D%0A        layerProps.addValue(id);%0A        featureBuilder.addTags(layerProps.addKey(KEY_ID));%0A        featureBuilder.addTags(valueIndex);%0A    %7D%0A%0A
 %7D%0A

Add id property to MVT features as _oid property