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);

    private static final String KEY_ID = "_oid";

    @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;

        String id = f.getID();
        addId(layerProps, featureBuilder, id);

        Name geomPropertyName = f.getDefaultGeometryProperty().getName();
        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);
        }
    }

    private void addId(MvtLayerProps layerProps, Builder featureBuilder, String id) {
        if (id == null || id.isEmpty()) {
            return;
        }
        int valueIndex = layerProps.addKey(id);
        if (valueIndex < 0) {
            // This shouldn't happen as id is non-empty String
            LOG.warn("Could not add id: " + id);
            return;
        }
        layerProps.addValue(id);
        featureBuilder.addTags(layerProps.addKey(KEY_ID));
        featureBuilder.addTags(valueIndex);
    }

}

@@ -2084,54 +2084,91 @@
     
-featureBuilder.addTags(layerProps.addKey(prop)
+int keyIndex = layerProps.addKey(prop);%0A            featureBuilder.addTags(keyIndex
 );%0A 
@@ -2428,19 +2428,21 @@
 rops.add
-Key
+Value
 (id);%0A  
@@ -2452,178 +2452,21 @@
    i
-f (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       
+nt keyIndex =
  lay
@@ -2472,32 +2472,34 @@
 yerProps.add
-Value(id
+Key(KEY_ID
 );%0A        f
@@ -2524,33 +2524,16 @@
 ags(
-layerProps.addKey(KEY_ID)
+keyIndex
 );%0A 

Fix addId function