package fi.nls.oskari.control.data;

import fi.mml.portti.service.search.SearchCriteria;
import fi.mml.portti.service.search.SearchService;
import fi.mml.portti.service.search.SearchServiceImpl;
import fi.nls.oskari.SearchWorker;
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.util.PropertyUtil;
import fi.nls.oskari.util.ResponseHelper;
import org.json.JSONObject;

import java.util.Locale;

@OskariActionRoute("GetSearchResult")
public class GetSearchResultHandler extends ActionHandler {

    private static final String PARAM_SEARCH_KEY = "searchKey";
    private static final String PARAM_EPSG_KEY = "epsg";
    private static final String PARAM_CHANNELIDS_KEY = "channels";
    private static final String PARAM_AUTOCOMPLETE = "autocomplete";
    private static final SearchService searchService = new SearchServiceImpl();

    private String[] channels = new String[0];

    public void init() {
        channels = PropertyUtil.getCommaSeparatedList("actionhandler.GetSearchResult.channels");
    }


    public void handleAction(final ActionParameters params) throws ActionException {
        final String search = params.getHttpParam(PARAM_SEARCH_KEY);
        if (search == null) {
            throw new ActionParamsException("Search string was null");
        }
        final String epsg = params.getHttpParam(PARAM_EPSG_KEY);

        final String error = SearchWorker.checkLegalSearch(search);

        if (!SearchWorker.STR_TRUE.equals(error)) {
            // write error message key
            ResponseHelper.writeResponse(params, error);
            return;
        }
        final Locale locale = params.getLocale();

        final SearchCriteria sc = new SearchCriteria(params.getUser());

        // default to configuration
        String[] channelIds = channels;
        final String channelParam = params.getHttpParam(PARAM_CHANNELIDS_KEY, "").trim();

        // if channels defined in request, use channels from request
        if(!channelParam.isEmpty()) {
            channelIds = channelParam.split("\\s*,\\s*");
        }
        // service will add defaults if channels not defined
        for (String id :  channelIds) {
            sc.addChannel(id);
        }
        sc.setSearchString(search);
        sc.setSRS(epsg);  // eg. EPSG:3067
        sc.setLocale(locale.getLanguage());

        String autocomplete = params.getHttpParam(PARAM_AUTOCOMPLETE, "");
        JSONObject result = null;
        if (autocomplete.isEmpty() || autocomplete.equals("false")) {
            result = SearchWorker.doSearch(sc);
        }
        else if (autocomplete.equals("true")){
            result = searchService.doSearchAutocomplete(sc);
        }
        ResponseHelper.writeResponse(params, result);
    }
}

@@ -2537,30 +2537,12 @@
     
-String autocomplete = 
+if (
 para
@@ -2581,112 +2581,13 @@
 TE, 
-%22%22);%0A        JSONObject result = null;%0A        if (autocomplete.isEmpty() %7C%7C autocomplete.equals(%22false%22
+false
 )) %7B
@@ -2603,121 +2603,44 @@
     
-result = SearchWorker.doSearch(sc);%0A        %7D%0A        else if (autocomplete.equals(%22true%22))%7B%0A            result =
+ResponseHelper.writeResponse(params,
  sea
@@ -2674,16 +2674,17 @@
 lete(sc)
+)
 ;%0A      
@@ -2678,33 +2678,44 @@
 (sc));%0A        %7D
-%0A
+ else %7B%0A    
         Response
@@ -2747,16 +2747,45 @@
 ms, 
-result);
+SearchWorker.doSearch(sc));%0A        %7D
 %0A   

Use the boolean version of getHttpParam for cleaner code