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.control.ActionParamsException;
import fi.nls.oskari.domain.map.stats.StatsVisualization;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.stats.VisualizationService;
import fi.nls.oskari.map.stats.VisualizationServiceIbatisImpl;
import fi.nls.oskari.util.ConversionHelper;
import fi.nls.oskari.util.ResponseHelper;
import org.apache.axiom.om.OMElement;

import javax.servlet.http.HttpServletResponse;

@OskariActionRoute("GetStatsLayerSLD")
public class GetStatsLayerSLDHandler extends ActionHandler {

    private static final Logger log = LogFactory.getLogger(GetStatsLayerSLDHandler.class);
    final private VisualizationService service = new VisualizationServiceIbatisImpl();

    // we need this
    final public static String PARAM_VISUALIZATION_ID = "visId";
    // OR these
    final public static String PARAM_VISUALIZATION_NAME = "name"; // name=ows:Kunnat2013
    final public static String PARAM_VISUALIZATION_FILTER_PROPERTY = "attr"; // attr=Kuntakoodi
    final public static String PARAM_VISUALIZATION_CLASSES = "classes"; // classes=020,091|186,086,982|111,139,740
    final public static String PARAM_VISUALIZATION_VIS = "vis"; // vis=choro:ccffcc|99cc99|669966

    final public static String PARAM_LANGUAGE = "lang";
    final public static String PARAM_LAYER_ID = "layerId";
    // mode is for debugging
    final private static String PARAM_MODE = "mode";
    final private static String MODE_XML = "XML";

    public void handleAction(final ActionParameters params)
            throws ActionException {

        final HttpServletResponse response = params.getResponse();
        response.setContentType("text/xml");
        ResponseHelper.writeResponse(params, getSLD(params));
    }

    private String getSLD(final ActionParameters params)
            throws ActionException {

        final String lang = params.getHttpParam(PARAM_LANGUAGE, params
                .getLocale().getLanguage());

        final boolean modeXML = MODE_XML.equals(params.getHttpParam(PARAM_MODE,
                "").toUpperCase());

        final StatsVisualization vis = getVisualization(params);
        if (vis == null) {
            throw new ActionParamsException(
                    "Couldn't get requested visualization");
        }

        log.debug("Found visualization:", vis);
        final OMElement xml = service.getXML(vis, lang);
        try {
            if (modeXML) {
                return xml.toString();
            } else {
                return service.transform(xml, service.getDefaultXSLT());
            }
        } catch (Exception e) {
            throw new ActionException("Unable to create SLD", e);
        }
    }

    /**
     * Constructs visualization parameters from request parameters
     *
     * @param params request parameters
     * @return Visualization parameters required to generate an SLD or empty/invalid StatsVisualization object if parameters were missing
     */
    private StatsVisualization getVisualization(final ActionParameters params) {
        final int visId = ConversionHelper.getInt(
                params.getHttpParam(PARAM_VISUALIZATION_ID), -1);
        final StatsVisualization vis = service.getVisualization(
                visId,
                params.getHttpParam(PARAM_VISUALIZATION_CLASSES),
                params.getHttpParam(PARAM_VISUALIZATION_NAME),
                params.getHttpParam(PARAM_VISUALIZATION_FILTER_PROPERTY),
                params.getHttpParam(PARAM_VISUALIZATION_VIS, "")
        );
        if (vis != null) {
            return vis;
        } else {
            return new StatsVisualization();
        }
    }

}
@@ -31,16 +31,47 @@
 layer;%0A%0A
+import java.util.Enumeration;%0A%0A
 import f
@@ -628,24 +628,25 @@
 onseHelper;%0A
+%0A
 import org.a
@@ -2112,32 +2112,68 @@
 ctionException %7B
+%0A    %09%0A    %09printParameters(params);
 %0A%0A        final 
@@ -2799,32 +2799,34 @@
        %7D else %7B%0A
+//
                 
@@ -2898,123 +2898,694 @@
     
-%7D%0A        %7D catch (Exception e) %7B%0A            throw new ActionException(%22Unable to create SLD%22, e);%0A        %7D%0A    %7D
+%09String xmlString = service.transform(xml, service.getDefaultXSLT());%0A            %09%0A            %09return xmlString;%0A            %7D%0A        %7D catch (Exception e) %7B%0A            throw new ActionException(%22Unable to create SLD%22, e);%0A        %7D%0A    %7D%0A    %0A    private void printParameters(ActionParameters params)%7B%0A    %09Enumeration e = params.getRequest().getParameterNames();%0A    %09%0A    %09log.debug(%22print keys%22);%0A    %09while(e.hasMoreElements())%7B%0A    %09%09String key = (String)e.nextElement();%0A    %09%09log.debug(%22Key: %22 +key);%0A    %09%09String%5B%5D values = params.getRequest().getParameterValues(key);%0A    %09%09%0A    %09%09for(String value : values)%7B%0A    %09%09%09log.debug(%22  value: %22 + value);%0A    %09%09%7D%0A    %09%7D%0A    %7D    
 %0A%0A  

debug lines added