package org.devocative.wickomp;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.request.IRequestParameters;
import org.apache.wicket.request.Response;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.util.string.StringValue;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * Some useful annotations:
 *
 * @JsonIgnore
 *
 * @JsonInclude(JsonInclude.Include.NON_NULL)
 *
 * @JsonRawValue
 *
 * @JsonProperty("<as property name>")
 *
 * @JsonValue
 */
public class WebUtil {
	public static String toJson(Object obj) {
		StringWriter sw = new StringWriter();
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
		try {
			mapper.writeValue(sw, obj);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return sw.toString();
	}

	public static <T> T fromJson(String json, Class<T> cls) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return mapper.readValue(json, cls);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T fromJson(String json, TypeReference<T> typeReference) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return mapper.readValue(json, typeReference);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void writeJQueryCall(String script, boolean decorateWithInit) {
		AjaxRequestTarget target = RequestCycle.get().find(AjaxRequestTarget.class);
		if (target != null) {
			target.appendJavaScript(script);
		} else {
			Response response = RequestCycle.get().getResponse();
			if (decorateWithInit) {
				response.write(String.format("<script>$(function(){%s});</script>", script));
			} else {
				response.write(String.format("<script>%s</script>", script));
			}
		}
	}

	public static Map<String, List<String>> toMap(IRequestParameters parameters, boolean lowercaseParam) {
		Map<String, List<String>> result = new HashMap<>();
		for (String param : parameters.getParameterNames()) {
			List<String> values = new ArrayList<>();

			for (StringValue stringValue : parameters.getParameterValues(param)) {
				values.add(stringValue.toString());
			}

			if (lowercaseParam) {
				param = param.toLowerCase();
			}

			if (result.containsKey(param)) {
				result.get(param).addAll(values);
			} else {
				result.put(param, values);
			}
		}
		return result;
	}
}

@@ -2406,24 +2406,45 @@
 wercaseParam
+, boolean ignoreEmpty
 ) %7B%0A%09%09Map%3CSt
@@ -2672,50 +2672,231 @@
 %09%09%09%09
-values.add(stringValue.toString());%0A%09%09%09%7D%0A%0A
+if (ignoreEmpty) %7B%0A%09%09%09%09%09if (!stringValue.isNull() && !stringValue.isEmpty()) %7B%0A%09%09%09%09%09%09values.add(stringValue.toString());%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09values.add(stringValue.toString());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09if (values.size() %3E 0) %7B%0A%09
 %09%09%09i
@@ -2920,16 +2920,17 @@
 ) %7B%0A%09%09%09%09
+%09
 param = 
@@ -2949,27 +2949,29 @@
 rCase();%0A%09%09%09
+%09
 %7D%0A%0A
+%09
 %09%09%09if (resul
@@ -2994,24 +2994,25 @@
 ram)) %7B%0A%09%09%09%09
+%09
 result.get(p
@@ -3032,24 +3032,25 @@
 values);%0A%09%09%09
+%09
 %7D else %7B%0A%09%09%09
@@ -3042,24 +3042,25 @@
 %09%09%09%7D else %7B%0A
+%09
 %09%09%09%09result.p
@@ -3078,16 +3078,22 @@
 alues);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

support ignore empty parameters in toMap()