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.MapTerm;

public class MapResourceState extends CompositeResourceState {
	private Map<String, ResourceState> children = new HashMap<>();
	
	@Override
	public Expression getValue() {
		MapTerm value = new MapTerm();
		value.setType(DataConstraintModel.typeMap);
		for (Map.Entry<String, ResourceState> childEnt: children.entrySet()) {
			value.insert(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() {
		MapResourceState newMapResourceState = new MapResourceState();
		for (String key: children.keySet()) {
			newMapResourceState.children.put(key, (ResourceState) children.get(key).clone());
		}
		return newMapResourceState;
	}
}
