package org.monospark.actioncontrol.config;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.monospark.actioncontrol.handler.ActionHandler;
import org.monospark.actioncontrol.handler.ActionMatcher;
import org.monospark.actioncontrol.handler.ActionResponse;
import org.monospark.actioncontrol.handler.ActionSettings;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

final class ConfigCategory {

	private Map<ActionHandler<?, ?>, ActionSettings<?>> settings;

	private ConfigCategory(Map<ActionHandler<?, ?>, ActionSettings<?>> settings) {
		this.settings = settings;
	}

	Map<ActionHandler<?, ?>, ActionSettings<?>> getSettings() {
		return settings;
	}

	
	static final class Deserializer implements JsonDeserializer<ConfigCategory> {

		@Override
		public ConfigCategory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {
			JsonObject object = json.getAsJsonObject();
			Map<ActionHandler<?, ?>, ActionSettings<?>> settings =
					new HashMap<ActionHandler<?, ?>, ActionSettings<?>>();
			for(ActionHandler<?, ?> handler : ActionHandler.ALL) {
				JsonObject settingsElement = object.get(handler.getName()).getAsJsonObject();
				JsonElement responseElement = settingsElement.get("response");
				ActionResponse response = context.deserialize(responseElement, ActionResponse.class);
				JsonElement matcherElement = settingsElement.get("matcher");
				ActionMatcher matcher = context.deserialize(matcherElement, handler.getMatcherClass());
				settings.put(handler, new ActionSettings<ActionMatcher>(response, matcher));
			}
			return new ConfigCategory(settings);
		}
	}
}

@@ -1311,21 +1311,22 @@
 %09%09%09%09Json
-Objec
+Elemen
 t settin
@@ -1366,16 +1366,125 @@
 tName())
+;%0A%09%09%09%09if(settingsElement == null) %7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09JsonObject settingsObject = settingsElement
 .getAsJs
@@ -1533,30 +1533,29 @@
 t = settings
-Elemen
+Objec
 t.get(%22respo
@@ -1688,30 +1688,29 @@
 t = settings
-Elemen
+Objec
 t.get(%22match

Fix a config parsing bug