package li.strolch.model.json;

import java.util.Collections;
import java.util.Set;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import li.strolch.exception.StrolchModelException;
import li.strolch.model.*;
import li.strolch.model.Tags.Json;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import li.strolch.utils.collections.MapOfSets;
import li.strolch.utils.dbc.DBC;

/**
 * <p>
 * Maps a given {@link JsonObject} to a {@link StrolchRootElement}. All {@link Parameter Parameters} on the element are
 * iterated and expected to be found as a member on the {@link JsonObject}.
 * </p>
 * <p>
 * To ignore {@link Parameter Parameters} or {@link ParameterBag ParameterBags} use the
 * {@link #ignoreParameter(String, String)} and {@link #ignoreBag(String)} methods
 * </p>
 * <p>
 * {@link Parameter} can be made optional by using the {@link #optionalParameter(String, String)} method
 * </p>
 *
 * @author Robert von Burg <eitch@eitchnet.ch>
 */
public class FromFlatJsonVisitor implements StrolchRootElementVisitor<Void> {

	private MapOfSets<String, String> ignoredKeys;
	private MapOfSets<String, String> optionalKeys;

	private JsonObject srcObject;

	public FromFlatJsonVisitor() {
		this.ignoredKeys = new MapOfSets<>();
		this.optionalKeys = new MapOfSets<>();
	}

	public FromFlatJsonVisitor(JsonObject srcObject) {
		this.srcObject = srcObject;
		this.ignoredKeys = new MapOfSets<>();
		this.optionalKeys = new MapOfSets<>();
	}

	public FromFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
		this.ignoredKeys = new MapOfSets<>();
		this.optionalKeys = new MapOfSets<>();
	}

	public FromFlatJsonVisitor ignoreBag(String bagId) {
		this.ignoredKeys.addSet(bagId, Collections.emptySet());
		return this;
	}

	public FromFlatJsonVisitor ignoreParameter(String bagId, String paramId) {
		this.ignoredKeys.addElement(bagId, paramId);
		return this;
	}

	public FromFlatJsonVisitor optionalParameter(String bagId, String paramId) {
		this.optionalKeys.addElement(bagId, paramId);
		return this;
	}

	@Override
	public Void visitResource(Resource dstElement) {
		if (this.srcObject.has(Json.OBJECT_TYPE)) {
			DBC.PRE.assertEquals("objectType must be the same, if set on JsonObject!", dstElement.getObjectType(),
					srcObject.get(Json.OBJECT_TYPE).getAsString());
		}

		visit(dstElement);
		return null;
	}

	@Override
	public Void visitOrder(Order dstElement) {
		if (this.srcObject.has(Json.OBJECT_TYPE)) {
			DBC.PRE.assertEquals("objectType must be the same, if set on JsonObject!", dstElement.getObjectType(),
					srcObject.get(Json.OBJECT_TYPE).getAsString());
		}

		visit(dstElement);
		return null;
	}

	@Override
	public Void visitActivity(Activity dstElement) {
		throw new UnsupportedOperationException("Activity elements are not supported by this visitor!");
	}

	public void visit(StrolchRootElement dstElement, JsonObject srcObject) {
		if (this.srcObject != null && this.srcObject != srcObject) {
			throw new IllegalStateException("The srcObject was already set with a different value in the constructor!");
		}

		DBC.PRE.assertTrue("objectType must be set!", srcObject.has(Json.OBJECT_TYPE));
		DBC.PRE.assertEquals("objectType must be the same!", dstElement.getObjectType(),
				srcObject.get(Json.OBJECT_TYPE).getAsString());

		this.srcObject = srcObject;
		visit(dstElement);
	}

	public void visit(StrolchRootElement dstElement) {

		// types must be the same, if exists on source element
		if (this.srcObject.has(Json.TYPE))
			DBC.PRE.assertEquals("type must be the same!", dstElement.getObjectType(),
					this.srcObject.get(Json.TYPE).getAsString());

		// update name if possible
		if (this.srcObject.has(Json.NAME))
			dstElement.setName(srcObject.get(Json.NAME).getAsString());

		Set<String> bagKeySet = dstElement.getParameterBagKeySet();
		for (String bagId : bagKeySet) {

			// see if we have to ignore this bag i.e. empty set existing
			Set<String> ignoredParamIds = this.ignoredKeys.getSet(bagId);
			if (ignoredParamIds != null && ignoredParamIds.isEmpty())
				continue;

			ParameterBag parameterBag = dstElement.getParameterBag(bagId);

			Set<String> parameterKeySet = parameterBag.getParameterKeySet();
			for (String paramId : parameterKeySet) {

				// see if this parameter must be ignored
				if (ignoredParamIds != null && ignoredParamIds.contains(paramId))
					continue;

				JsonElement jsonElement = this.srcObject.get(paramId);
				if (jsonElement == null) {
					if (this.optionalKeys.containsElement(bagId, paramId))
						continue;
					throw new StrolchModelException("JsonObject is missing member " + paramId);
				}

				if (!jsonElement.isJsonPrimitive()) {
					throw new StrolchModelException(
							"JsonElement " + paramId + " is not a json primitive but a " + jsonElement.getClass()
									.getName());
				}

				Parameter<?> parameter = parameterBag.getParameter(paramId);
				try {
					parameter.setValueFromString(jsonElement.getAsString());
				} catch (Exception e) {
					throw new IllegalStateException("Failed to set parameter " + parameter.getLocator(), e);
				}
			}
		}
	}
}

@@ -224,17 +224,137 @@
 h.model.
-*
+Order;%0Aimport li.strolch.model.ParameterBag;%0Aimport li.strolch.model.Resource;%0Aimport li.strolch.model.StrolchRootElement
 ;%0Aimport
@@ -919,19 +919,16 @@
  use the
-%0A *
  %7B@link 
@@ -943,32 +943,37 @@
 arameter(String,
+%0A * *
  String)%7D and %7B@
@@ -3764,38 +3764,32 @@
 , dstElement.get
-Object
 Type(),%0A%09%09%09%09%09thi
@@ -3920,16 +3920,21 @@
 setName(
+this.
 srcObjec

validate type with type, not objectType in FromFlatJsonVisitor