package fi.nls.oskari.control.layer;

import fi.nls.oskari.analysis.AnalysisHelper;
import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionHandler;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.domain.map.analysis.Analysis;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.analysis.service.AnalysisDataService;
import fi.nls.oskari.map.analysis.service.AnalysisDbService;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.ResponseHelper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.oskari.permissions.PermissionService;
import org.oskari.permissions.model.PermissionType;
import org.oskari.permissions.model.ResourceType;

import java.util.List;
import java.util.Set;

/**
 * Returns users own analysis layers as JSON.
 */
@OskariActionRoute("GetAnalysisLayers")
public class GetAnalysisLayersHandler extends ActionHandler {

    private static final Logger LOG = LogFactory.getLogger(GetAnalysisLayersHandler.class);
    private AnalysisDbService analysisService;
    private PermissionService permissionsService;

    private static final String JSKEY_ANALYSISLAYERS = "analysislayers";

    @Override
    public void init() {
        super.init();
        permissionsService = OskariComponentManager.getComponentOfType(PermissionService.class);
        analysisService = OskariComponentManager.getComponentOfType(AnalysisDbService.class);
    }

    @Override
    public void handleAction(ActionParameters params) throws ActionException {
        params.requireLoggedInUser();

        final JSONObject response = new JSONObject();
        final JSONArray layers = new JSONArray();
        JSONHelper.putValue(response, JSKEY_ANALYSISLAYERS, layers);

        final User user = params.getUser();
        final List<Analysis> list = analysisService.getAnalysisByUid(user.getUuid());
        Set<String> publishPermission = permissionsService.getResourcesWithGrantedPermissions(ResourceType.analysislayer, user, PermissionType.PUBLISH);
        Set<String> downloadPermission = permissionsService.getResourcesWithGrantedPermissions(ResourceType.analysislayer, user, PermissionType.DOWNLOAD);
        JSONObject baseOptions = AnalysisDataService.getBaseLayer().getOptions();
        for(Analysis a: list) {
            // Parse analyse layer json out analysis
            final JSONObject analysisLayer = AnalysisHelper.getlayerJSON(a, baseOptions);
            final String permissionKey = "analysis+" + a.getId();

            JSONHelper.putValue(analysisLayer, "permissions",
                    AnalysisHelper.getAnalysisPermissions(
                            publishPermission.contains(permissionKey),
                            downloadPermission.contains(permissionKey)));
            layers.put(analysisLayer);
        }

        ResponseHelper.writeResponse(params, response);
    }
}

@@ -35,54 +35,8 @@
 r;%0A%0A
-import fi.nls.oskari.analysis.AnalysisHelper;%0A
 impo
@@ -490,32 +490,102 @@
 lysisDbService;%0A
+import fi.nls.oskari.map.layer.formatters.LayerJSONFormatterANALYSIS;%0A
 import fi.nls.os
@@ -759,159 +759,8 @@
 ect;
-%0Aimport org.oskari.permissions.PermissionService;%0Aimport org.oskari.permissions.model.PermissionType;%0Aimport org.oskari.permissions.model.ResourceType;
 %0A%0Aim
@@ -783,30 +783,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0A/*
@@ -1080,58 +1080,8 @@
 ice;
-%0A    private PermissionService permissionsService;
 %0A%0A  
@@ -1217,105 +1217,8 @@
 ();%0A
-        permissionsService = OskariComponentManager.getComponentOfType(PermissionService.class);%0A
     
@@ -1762,680 +1762,227 @@
     
-Set%3CString%3E publishPermission = permissionsService.getResourcesWithGrantedPermissions(ResourceType.analysislayer, user, PermissionType.PUBLISH);%0A        Set%3CString%3E downloadPermission = permissionsService.getResourcesWithGrantedPermissions(ResourceType.analysislayer, user, PermissionType.DOWNLOAD);%0A        JSONObject baseOptions = AnalysisDataService.getBaseLayer().getOptions();%0A        for(Analysis a: list) %7B%0A            // Parse analyse layer json out analysis%0A            final JSONObject analysisLayer = AnalysisHelper.getlayerJSON(a, baseOptions);%0A            final String permissionKey = %22analysis+%22 + a.getId();%0A%0A            JSONHelper.putValue(analysisLayer, %22
+for(Analysis a: list) %7B%0A            // Parse analyse layer json out analysis%0A            final JSONObject analysisLayer = AnalysisDataService.parseAnalysis2JSON(a, null);%0A            // user's own analysis, add default 
 perm
@@ -1992,12 +1992,9 @@
 ions
-%22,%0A 
+%0A
     
@@ -2005,196 +2005,70 @@
     
-       AnalysisHelper.getAnalysisPermissions(%0A                            publishPermission.contains(permissionKey),%0A                            downloadPermission.contains(permissionKey))
+LayerJSONFormatterANALYSIS.setDefaultPermissions(analysisLayer
 );%0A 

use layerjson formatter for getanalysislayers