/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.jaamsim.BasicObjects;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.Samples.SampleExpInput;
import com.jaamsim.basicsim.ErrorException;
import com.jaamsim.input.EntityListInput;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.InputErrorException;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.DimensionlessUnit;

public class SetGraphics extends LinkedComponent {

	@Keyword(description = "List of entities whose graphics can chosen for assignment to the received entity.",
	         example = "SetGraphics1 GraphicsList{ DisplayEntity1 DisplayEntity2 }")
	private final EntityListInput<DisplayEntity> graphicsList;

	@Keyword(description = "A number that determines the choice of entities from the GraphicsList:\n" +
			"     1 = first entity's graphics, 2 = second entity's graphics, etc.\n" +
			"A constant value, a distribution to be sampled, or a time series can be entered.",
	         example = "SetGraphics1 Choice { 2 }")
	private final SampleExpInput choice;

	{
		graphicsList = new EntityListInput<>( DisplayEntity.class, "GraphicsList", "Key Inputs", null);
		this.addInput( graphicsList);

		choice = new SampleExpInput("Choice", "Key Inputs", new SampleConstant(DimensionlessUnit.class, 1.0));
		choice.setUnitType(DimensionlessUnit.class);
		choice.setEntity(this);
		this.addInput(choice);
	}

	public SetGraphics() {}

	@Override
	public void validate() {
		super.validate();

		// Confirm that the GraphicList has been specified
		if (graphicsList.getValue() == null || graphicsList.getValue().isEmpty()) {
			throw new InputErrorException("The keyword GraphicsList must be set.");
		}
	}

	@Override
	public void addDisplayEntity( DisplayEntity ent ) {
		super.addDisplayEntity(ent);

		// Choose the new graphics for this entity
		int i = (int) choice.getValue().getNextSample(this.getSimTime());
		if (i<1 || i>graphicsList.getValue().size())
			throw new ErrorException("Chosen index i=%s is out of range for GraphicList: %s.", i, graphicsList.getValue());
		DisplayEntity chosen = graphicsList.getValue().get(i-1);

		// Set the graphics for the incoming entity to those for the chosen entity
		String str = chosen.getInput("DisplayModel").getValueString();
		InputAgent.processEntity_Keyword_Value(ent, "DisplayModel", str);

		ent.setSize(chosen.getSize());
		ent.setOrientation(chosen.getOrientation());
		ent.setAlignment(chosen.getAlignment());

		// Send the entity to the next component in the chain
		this.sendToNextComponent(ent);
	}

}

@@ -630,16 +630,45 @@
 jects;%0A%0A
+import java.util.ArrayList;%0A%0A
 import c
@@ -870,24 +870,56 @@
 yListInput;%0A
+import com.jaamsim.input.Input;%0A
 import com.j
@@ -1023,16 +1023,55 @@
 eyword;%0A
+import com.jaamsim.input.KeywordIndex;%0A
 import c
@@ -2918,18 +2918,20 @@
 y%0A%09%09
-String str
+Input%3C?%3E inp
  = c
@@ -2964,94 +2964,211 @@
 el%22)
-.getValueString();%0A%09%09InputAgent.processEntity_Keyword_Value(ent, %22DisplayModel%22, str);
+;%0A%09%09if (!inp.isDefault()) %7B%0A%09%09%09ArrayList%3CString%3E tmp = new ArrayList%3C%3E();%0A%09%09%09inp.getValueTokens(tmp);%0A%09%09%09KeywordIndex kw = new KeywordIndex(inp.getKeyword(), tmp, null);%0A%09%09%09InputAgent.apply(ent, kw);%0A%09%09%7D
 %0A%0A%09%09

use getValueTokens in the SetGraphics Object