/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 * Copyright (C) 2016-2021 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.input;

import java.util.ArrayList;

import com.jaamsim.basicsim.Entity;
import com.jaamsim.basicsim.JaamSimModel;


public class AssignmentListInput extends ListInput<ArrayList<ExpParser.Assignment>> {

	private ArrayList<ExpEvaluator.EntityParseContext> parseContextList;

	public AssignmentListInput(String key, String cat, ArrayList<ExpParser.Assignment> def){
		super(key, cat, def);
	}

	@Override
	public void copyFrom(Entity thisEnt, Input<?> in) {
		super.copyFrom(thisEnt, in);

		// An expression input must be re-parsed to reset the entity referred to by "this"
		parseFrom(thisEnt, in);
	}

	@Override
	public void parse(Entity thisEnt, KeywordIndex kw) throws InputErrorException {

		// Divide up the inputs by the inner braces
		ArrayList<KeywordIndex> subArgs = kw.getSubArgs();
		ArrayList<ExpParser.Assignment> temp = new ArrayList<>(subArgs.size());
		ArrayList<ExpEvaluator.EntityParseContext> pcList = new ArrayList<>(subArgs.size());

		// Parse the inputs within each inner brace
		for (int i = 0; i < subArgs.size(); i++) {
			KeywordIndex subArg = subArgs.get(i);
			Input.assertCount(subArg, 1);
			try {
				// Parse the assignment expression
				String assignmentString = subArg.getArg(0);
				ExpEvaluator.EntityParseContext pc = ExpEvaluator.getParseContext(thisEnt, assignmentString);
				ExpParser.Assignment ass = ExpParser.parseAssignment(pc, assignmentString);

				// Save the data for this assignment
				pcList.add(pc);
				temp.add(ass);

			} catch (ExpError e) {
				throw new InputErrorException(e);
			}
		}

		// Save the data for each assignment
		parseContextList = pcList;
		value = temp;
	}

	@Override
	public void getValueTokens(ArrayList<String> toks) {
		if (value == null || isDefault())
			return;

		for (int i = 0; i < value.size(); i++) {
			toks.add("{");
			toks.add(parseContextList.get(i).getUpdatedSource());
			toks.add("}");
		}
	}

	@Override
	public int getListSize() {
		if (value == null)
			return 0;
		else
			return value.size();
	}

	@Override
	public String getDefaultString(JaamSimModel simModel) {
		return "";
	}

	@Override
	public void appendEntityReferences(ArrayList<Entity> list) {
		if (value == null)
			return;
		try {
			for (ExpParser.Assignment assign : value) {
				ExpParser.appendEntityReferences(assign, list);
			}
		}
		catch (ExpError e) {}
	}

	@Override
	public boolean useExpressionBuilder() {
		return true;
	}

}

@@ -112,17 +112,17 @@
 2016-202
-1
+2
  JaamSim
@@ -791,16 +791,60 @@
 Entity;%0A
+import com.jaamsim.basicsim.ErrorException;%0A
 import c
@@ -3173,10 +3173,283 @@
 ue;%0A%09%7D%0A%0A
+%09public void executeAssignments(Entity thisEnt, double simTime) %7B%0A%09%09try %7B%0A%09%09%09for (ExpParser.Assignment ass : value) %7B%0A%09%09%09%09ExpEvaluator.evaluateExpression(ass, thisEnt, simTime);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09catch (ExpError e) %7B%0A%09%09%09throw new ErrorException(thisEnt, getKeyword(), e);%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

add method 'executeAssignments' to AssignmentListInput