package fi.nls.oskari.control.layer;

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.map.wfs.WFSLayerConfiguration;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.service.ServiceException;
import fi.nls.oskari.util.ConversionHelper;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.oskari.util.ResponseHelper;
import fi.nls.oskari.wfs.WFSLayerConfigurationService;
import fi.nls.oskari.wfs.WFSLayerConfigurationServiceIbatisImpl;
import fi.nls.oskari.wfs.util.WFSDescribeFeatureHelper;
import org.json.JSONObject;

/**
 * Get WMS capabilites and return JSON
 */
@OskariActionRoute("GetWFSDescribeFeature")
public class GetWFSDescribeFeatureHandler extends ActionHandler {

    private static final Logger log = LogFactory.getLogger(GetWFSDescribeFeatureHandler.class);
    private final WFSLayerConfigurationService layerConfigurationService = new WFSLayerConfigurationServiceIbatisImpl();

    private static final String PARM_LAYER_ID = "layer_id";

    private static final String WPS_PARAMS = "wps_params";
    public static final String ANALYSIS_PREFIX = "analysis_";
    public static final String MYPLACES_PREFIX = "myplaces_";
    private static final String MYPLACES_BASELAYER_ID = "myplaces.baselayer.id";
    public static final String USERLAYER_PREFIX = "userlayer_";
    private static final String USERLAYER_BASELAYER_ID = "userlayer.baselayer.id";

    final String myplacesBaseLayerId = PropertyUtil.get(MYPLACES_BASELAYER_ID);
    final String userlayerBaseLayerId = PropertyUtil.get(USERLAYER_BASELAYER_ID);

    @Override
    public void init() {
        super.init();
    }

    public void handleAction(ActionParameters params) throws ActionException {
        final String layer_id = params.getHttpParam(PARM_LAYER_ID, "");
        int id = getLayerId(layer_id);
        JSONObject response = new JSONObject();

        if (id != -1) {
            // Get wfs layer configuration ala Oskari
            WFSLayerConfiguration lc = layerConfigurationService.findConfiguration(id);
            if (lc != null) {
                // Get wfs feature property names  (gml properties excluded)
                response = getFeatureTypesTextOrNumeric(lc, layer_id);
                // Add WPS params
                JSONHelper.putValue(response, WPS_PARAMS, JSONHelper.createJSONObject(lc.getWps_params()));
            }

        } else if (layer_id.indexOf(ANALYSIS_PREFIX) > -1) {
            // Set analysis layer field types
            response = WFSDescribeFeatureHelper.getAnalysisFeaturePropertyTypes(layer_id);
        }

        ResponseHelper.writeResponse(params, response);

    }

    private int getLayerId(final String layer_id) {

        // WFS layer, myplaces or analysis layer
        if (layer_id.indexOf(MYPLACES_PREFIX) > -1) {
            return ConversionHelper.getInt(myplacesBaseLayerId, -1);
        } else if (layer_id.indexOf(USERLAYER_PREFIX) > -1) {
            return ConversionHelper.getInt(userlayerBaseLayerId, -1);
        }
        // Wfs layer id
        return ConversionHelper.getInt(layer_id, -1);
    }

    /**
     * Request WFS DescribeFeatureType response (xml)
     * @param lc  WFSlayerconfiguration ala Oskari
     *             e.g. http://tampere.navici.com/tampere_wfs_geoserver/ows?SERVICE=WFS&VERSION=1.1.0&REQUEST=DescribeFeatureType&TYPENAME=tampere_ora:KIINTEISTOT_ALUE
     * @param layer_id for keying response
     * @return  JSON object  (feature property names and text/numeric typing of property)
     * @throws ActionException
     */
    private JSONObject getFeatureTypesTextOrNumeric(WFSLayerConfiguration lc, String layer_id) throws ActionException {
        try {
            return WFSDescribeFeatureHelper.getFeatureTypesTextOrNumeric(lc, layer_id);
        } catch (ServiceException ex) {
            throw new ActionException("Error getting properties", ex);
        }
    }
}

@@ -31,16 +31,45 @@
 layer;%0A%0A
+import org.json.JSONObject;%0A%0A
 import f
@@ -111,16 +111,16 @@
 nRoute;%0A
-
 import f
@@ -767,36 +767,8 @@
 per;
-%0Aimport org.json.JSONObject;
 %0A%0A/*
@@ -2080,24 +2080,39 @@
 NObject();%0A%0A
+        try %7B%0A%09
         if (
@@ -2119,24 +2119,25 @@
 id != -1) %7B%0A
+%09
             
@@ -2174,24 +2174,25 @@
  ala Oskari%0A
+%09
             
@@ -2263,24 +2263,25 @@
 ration(id);%0A
+%09
             
@@ -2294,24 +2294,25 @@
  != null) %7B%0A
+%09
             
@@ -2376,16 +2376,17 @@
 cluded)%0A
+%09
         
@@ -2408,36 +2408,59 @@
 e = 
-getFeatureTypesTextOrNumeric
+WFSDescribeFeatureHelper.getWFSFeaturePropertyTypes
 (lc,
@@ -2463,32 +2463,33 @@
 (lc, layer_id);%0A
+%09
                 
@@ -2506,16 +2506,17 @@
  params%0A
+%09
         
@@ -2615,16 +2615,17 @@
 ms()));%0A
+%09
         
@@ -2622,33 +2622,35 @@
 %0A%09            %7D%0A
-%0A
+%09%0A%09
         %7D else i
@@ -2686,32 +2686,33 @@
 PREFIX) %3E -1) %7B%0A
+%09
             // S
@@ -2741,16 +2741,17 @@
 d types%0A
+%09
         
@@ -2825,32 +2825,93 @@
 ypes(layer_id);%0A
+%09        %7D%0A        %7D catch (ServiceException ex) %7B%0A        %09%0A
         %7D%0A%0A     

Change GetWFSDescribeFeature to get full fields types