package fi.nls.oskari.map.layer.formatters;

import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.domain.map.UserDataLayer;
import fi.nls.oskari.domain.map.wfs.WFSLayerOptions;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.PropertyUtil;
import org.json.JSONObject;

import java.util.Map;

public abstract class LayerJSONFormatterUSERDATA extends LayerJSONFormatterWFS {

    private static final boolean IS_SECURE = true;

    public JSONObject getJSON(OskariLayer baseLayer, UserDataLayer layer, String srs) {
        return this.getJSON(baseLayer, layer, srs, PropertyUtil.getDefaultLanguage());
    }

    public JSONObject getJSON(OskariLayer baseLayer, UserDataLayer layer, String srs, String lang) {
        JSONObject layerJson = getJSON(baseLayer, lang, IS_SECURE, srs);

        // Override base layer values
        JSONHelper.putValue(layerJson, KEY_TYPE, layer.getType());
        JSONHelper.putValue(layerJson, KEY_ID, layer.getPrefixedId());

        // override default name only if userdatalayer has name
        // add all localized names to allow user to edit them
        String name = layer.getName();
        Map<String, String> localized = layer.getNames();
        if (!localized.isEmpty()) {
            JSONObject names = new JSONObject();
            localized.entrySet().forEach(entry -> JSONHelper.putValue(names, entry.getKey(), entry.getValue()));
            JSONHelper.putValue(layerJson, KEY_LOCALIZED_NAME, names);
        } else if (name != null && !name.isEmpty()) {
            JSONHelper.putValue(layerJson, KEY_LOCALIZED_NAME, name);
        }
        // FIXME: base layer should have correct data provider and title.
        layerJson.remove(KEY_SUBTITLE);
        layerJson.remove(KEY_DATA_PROVIDER);

        WFSLayerOptions wfsOpts = layer.getWFSLayerOptions();
        wfsOpts.injectBaseLayerOptions(baseLayer.getOptions());
        JSONHelper.putValue(layerJson, KEY_OPTIONS, wfsOpts.getOptions());

        //init permissionz here??

        return layerJson;
    }
}

@@ -38,16 +38,68 @@
 tters;%0A%0A
+import fi.mml.map.mapwindow.util.OskariLayerWorker;%0A
 import f
@@ -131,24 +131,24 @@
 skariLayer;%0A
-
 import fi.nl
@@ -340,16 +340,68 @@
 NObject;
+%0Aimport org.oskari.permissions.model.PermissionType;
 %0A%0Aimport
@@ -499,16 +499,16 @@
 rWFS %7B%0A%0A
-
     priv
@@ -549,16 +549,81 @@
  = true;
+%0A    private static final String KEY_PERMISSIONS = %22permissions%22;
 %0A%0A    pu
@@ -2150,59 +2150,466 @@
     
-//init permissionz here??%0A%0A        return layerJson
+return layerJson;%0A    %7D%0A    public static void setDefaultPermissions(JSONObject layerJson) %7B%0A        JSONObject permissions = new JSONObject();%0A        JSONHelper.putValue(permissions, PermissionType.PUBLISH.getJsonKey(), OskariLayerWorker.PUBLICATION_PERMISSION_OK);%0A        JSONHelper.putValue(permissions, PermissionType.DOWNLOAD.getJsonKey(), OskariLayerWorker.DOWNLOAD_PERMISSION_OK);%0A        JSONHelper.putValue(layerJson, KEY_PERMISSIONS, permissions)
 ;%0A  

add method for userdata formatter to set default permissions