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

import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.domain.map.userlayer.UserLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.PropertyUtil;
import org.json.JSONObject;

/**
 * User layer to oskari layer json
 */
public class LayerJSONFormatterUSERLAYER extends LayerJSONFormatter {

    private static final String USERLAYER_RENDERING_URL = "userlayer.rendering.url";
    private static final String USERLAYER_RENDERING_ELEMENT = "userlayer.rendering.element";

    private static final String PROPERTY_RENDERING_URL = PropertyUtil.getOptional(USERLAYER_RENDERING_URL);
    final String userlayerRenderingElement = PropertyUtil.get(USERLAYER_RENDERING_ELEMENT);

    private static Logger log = LogFactory.getLogger(LayerJSONFormatterUSERLAYER.class);

    /**
     *
     * @param layer
     * @param lang
     * @param isSecure
     * @param ulayer     data in user_layer table
     * @return
     */
    public JSONObject getJSON(OskariLayer layer,
                                     final String lang,
                                     final boolean isSecure,
                                     UserLayer ulayer) {

        final JSONObject layerJson = getBaseJSON(layer, lang, isSecure);

        JSONHelper.putValue(layerJson, "isQueryable", true);
        JSONHelper.putValue(layerJson, "name",ulayer.getLayer_name());
        JSONHelper.putValue(layerJson, "description",ulayer.getLayer_desc());
        JSONHelper.putValue(layerJson, "source",ulayer.getLayer_source());
        JSONHelper.putValue(layerJson, "fields",JSONHelper.createJSONArrayJsonKeys(JSONHelper.createJSONObject(ulayer.getFields())));
        // user layer rendering url - override DB url if property is defined
        JSONHelper.putValue(layerJson, "url", getUserLayerTileUrl());
        JSONHelper.putValue(layerJson, "renderingElement", userlayerRenderingElement);
        return layerJson;
    }

    private static String getUserLayerTileUrl() {
        if (PROPERTY_RENDERING_URL == null) {
            // action_route name points to fi.nls.oskari.control.layer.UserLayerTileHandler
            return PropertyUtil.get("oskari.ajax.url.prefix") + "action_route=UserLayerTile&id=";
        }
        return PROPERTY_RENDERING_URL + "&id=";
    }
}

@@ -283,16 +283,44 @@
 rtyUtil;
+%0A%0Aimport org.json.JSONArray;
 %0Aimport 
@@ -341,16 +341,17 @@
 bject;%0A%0A
+%0A
 /**%0A * U
@@ -1655,24 +1655,41 @@
 _source());%0A
+        try%7B%0A    
         JSON
@@ -1806,24 +1806,212 @@
 ields())));%0A
+        %7Dcatch (IllegalArgumentException e)%7B%0A            JSONHelper.putValue(layerJson, %22fields%22, createJSONArrayJSONObjectKeys(JSONHelper.createJSONArray(ulayer.getFields())));%0A        %7D%0A
         // u
@@ -2232,16 +2232,17 @@
 ement);%0A
+%0A
         
@@ -2554,32 +2554,32 @@
 id=%22;%0A        %7D%0A
-
         return P
@@ -2615,11 +2615,604 @@
 %22;%0A    %7D
+%0A%0A    // creates JSONArray from JSONArray's JSONObject keys %5B%7Bkey,value%7D,%7Bkey2,value2%7D,..%5D -%3E %5Bkey1, key2,..%5D%0A    private static JSONArray createJSONArrayJSONObjectKeys(final JSONArray json) %7B%0A        try %7B%0A            JSONArray jsarray =  new JSONArray();%0A            for(int i = 0; i %3C json.length(); ++i)%7B%0A                JSONObject obj = json.getJSONObject(i);%0A                jsarray.put(obj.names().get(0));%0A            %7D%0A            return jsarray;%0A        %7D catch (Exception e) %7B%0A            throw new IllegalArgumentException(%22Couldn't create JSONArray of Json keys%22);%0A        %7D%0A    %7D
 %0A%7D%0A

add fields value from JSONArray to keep field ordering