package simulator.states;

import java.util.HashMap;
import java.util.Map;

import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Term;
import models.dataConstraintModel.DataConstraintModel;
import models.dataConstraintModel.JsonTerm;

public class JsonResourceState extends CompositeResourceState {
	private Map<String, ResourceState> children = new HashMap<>();

	@Override
	public Expression getValue() {
		JsonTerm value = new JsonTerm();
		value.setType(DataConstraintModel.typeJson);
		for (Map.Entry<String, ResourceState> childEnt: children.entrySet()) {
			value.addMember(childEnt.getKey(), childEnt.getValue().getValue());
		}
		return value;
	}
	
	@Override
	public Map<String, ResourceState> getChildStates() {
		return children;
	}

	public void addChildState(String param, ResourceState childState) {
		children.put(param, childState);
	}

	@Override
	public void replaceChildState(ResourceState state, ResourceState newState) {
		for (Map.Entry<String, ResourceState> childEnt: children.entrySet()) {
			if (childEnt.getValue().equals(state)) {
				children.put(childEnt.getKey(), newState);
			}
		}
	}
	
	public Object clone() {
		JsonResourceState newJsonResourceState = new JsonResourceState();
		for (String key: children.keySet()) {
			newJsonResourceState.children.put(key, (ResourceState) children.get(key).clone());
		}
		return newJsonResourceState;
	}
}
