package fi.nls.oskari.control.admin;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.*;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.OskariLayerService;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.ServiceRuntimeException;
import fi.nls.oskari.util.ResponseHelper;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

@OskariActionRoute("LayerAdminUsageCheck")
public class LayerAdminUsageCheckHandler extends RestActionHandler {
    private static final Logger LOG = LogFactory.getLogger(LayerAdminUsageCheckHandler.class);

    private static final String PARAM_LAYER_ID = "id";
    private OskariLayerService mapLayerService;

    @Override
    public void init() {
        try {
            mapLayerService = OskariComponentManager.getComponentOfType(OskariLayerService.class);
        } catch (Exception e) {
            throw new ServiceRuntimeException("Exception occured while initializing map layer service", e);
        }
    }

    @Override
    public void handleGet(ActionParameters params) throws ActionException {
        params.requireAdminUser();
        final int layerId = params.getRequiredParamInt(PARAM_LAYER_ID);
        LOG.info("Checking layer usage in other layers for layerId: ", layerId);
        Map<String, Set<Integer>> layerUsages = new HashMap<>();
        Set<Integer> timeseriesLayerIds = getTimeseriesLayerIds(layerId);
        layerUsages.put("timeseries", timeseriesLayerIds);
        ResponseHelper.writeResponse(params, layerUsages);
    }

    private Set<Integer> getTimeseriesLayerIds(int layerId) throws ActionException {
        Set<Integer> timeseriesLayerIds = new HashSet<>();
        for (OskariLayer layer : mapLayerService.findAll()) {
            JSONObject options = layer.getOptions();
            try {
                if (options != null && options.has("timeseries")) {
                    JSONObject timeseriesOptions = options.getJSONObject("timeseries");
                    Integer metadataLayerId = timeseriesOptions.getJSONObject("metadata").getInt("layer");
                    if (metadataLayerId == layerId) {
                        timeseriesLayerIds.add(layer.getId());
                    }
                }
            } catch (JSONException e) {
                throw new ActionException("Cannot parse layer metadata options for layer: " +
                        layer.getName() + ", id: " + layer.getId());
            }
        }
        return timeseriesLayerIds;
    }

}

@@ -1652,16 +1652,31 @@
 params, 
+new JSONObject(
 layerUsa
@@ -1679,16 +1679,17 @@
 rUsages)
+)
 ;%0A    %7D%0A
@@ -2146,31 +2146,37 @@
     
-Integer metadataLayerId
+JSONObject timeseriesMetadata
  = t
@@ -2188,26 +2188,26 @@
 riesOptions.
-ge
+op
 tJSONObject(
@@ -2217,16 +2217,192 @@
 tadata%22)
+;%0A                    if (timeseriesMetadata == null) %7B%0A                        continue;%0A                    %7D%0A                    Integer metadataLayerId = timeseriesMetadata
 .getInt(
@@ -2830,17 +2830,16 @@
 yerIds;%0A
+
     %7D%0A
-%0A
 %7D%0A

Use JSONObject in response. Better JSON handling