package org.oskari.control.mvt;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.*;
import fi.nls.oskari.control.feature.AbstractWFSFeaturesHandler;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.domain.map.wfs.WFSLayerConfiguration;
import fi.nls.oskari.service.ServiceRuntimeException;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.oskari.util.ResponseHelper;
import fi.nls.oskari.wfs.WFSLayerConfigurationService;
import org.json.JSONException;
import org.json.JSONObject;
import org.oskari.service.user.UserLayerService;
import org.oskari.service.util.ServiceFactory;

import java.util.*;

@OskariActionRoute("GetLocalizedPropertyNames")
public class GetLocalizedPropertyNamesHandler extends AbstractWFSFeaturesHandler {

    private WFSLayerConfigurationService wfsLayerService;

    @Override
    public void init() {
        super.init();
        this.wfsLayerService = ServiceFactory.getWfsLayerService();
    }

    @Override
    public void handleAction(ActionParameters params) throws ActionException {
        final String layerId = params.getRequiredParam(ActionConstants.PARAM_ID);
        Optional<UserLayerService> contentProcessor = getUserContentProsessor(layerId);
        OskariLayer layer = findLayer(layerId, params.getUser(), contentProcessor);
        WFSLayerConfiguration layerConf = wfsLayerService.findConfiguration(layer.getId());

        JSONObject response = new JSONObject();
        if (layerConf == null) {
            ResponseHelper.writeResponse(params, response);
            return;
        }

        String language = params.getHttpParam(ActionConstants.PARAM_LANGUAGE, PropertyUtil.getDefaultLanguage());
        List<String> propNames = layerConf.getSelectedFeatureParams(language);
        List<String> localizedPropNames = layerConf.getFeatureParamsLocales(language);
        if (localizedPropNames.isEmpty() && !propNames.isEmpty() && language != PropertyUtil.getDefaultLanguage()) {
            localizedPropNames = layerConf.getFeatureParamsLocales(PropertyUtil.getDefaultLanguage());
        }
        for (int i = 0; i < propNames.size() && i < localizedPropNames.size(); i++) {
            try {
                response.put(propNames.get(i), localizedPropNames.get(i));
            } catch (JSONException ex) {
                throw new ServiceRuntimeException("Unexpected JSONException occurred");
            }
        }
        ResponseHelper.writeResponse(params, response);
    }
}

@@ -528,16 +528,43 @@
 Object;%0A
+import org.json.JSONArray;%0A
 import o
@@ -596,32 +596,32 @@
 erLayerService;%0A
-
 import org.oskar
@@ -1439,17 +1439,16 @@
 tId());%0A
-%0A
         
@@ -1451,22 +1451,21 @@
     JSON
-Object
+Array
  respons
@@ -1476,22 +1476,21 @@
 new JSON
-Object
+Array
 ();%0A    
@@ -2243,64 +2243,192 @@
     
-response.put(propNames.get(i), localizedPropNames.get(i)
+JSONObject prop = new JSONObject();%0A                prop.put(%22name%22, propNames.get(i));%0A                prop.put(%22locale%22, localizedPropNames.get(i));%0A                response.put(prop
 );%0A 

return ordered names and locales in array