/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 * Copyright (C) 2016-2020 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.ProcessFlow;

import java.util.ArrayList;

import com.jaamsim.Commands.KeywordCommand;
import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.basicsim.Entity;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.InterfaceEntityListInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.KeywordIndex;
import com.jaamsim.states.StateEntity;

public class Duplicate extends LinkedComponent {

	@Keyword(description = "The list of components that will receive the duplicated entities. " +
			"One duplicated entity will be sent to each entry in the list.",
	         exampleList = {"Assign1 Queue1"})
	protected final InterfaceEntityListInput<Linkable> targetComponentList;

	{
		targetComponentList = new InterfaceEntityListInput<>( Linkable.class, "TargetComponentList", KEY_INPUTS, null);
		targetComponentList.setUnique(false);
		targetComponentList.setRequired(true);
		this.addInput( targetComponentList);
	}

	public Duplicate() {}

	@Override
	public void addEntity(DisplayEntity ent) {
		super.addEntity(ent);
		double simTime = getSimTime();

		// Make the duplicates and send them to the targets
		int n = 1;
		for (Linkable target : targetComponentList.getValue()) {

			// Create the duplicated entity
			StringBuilder sb = new StringBuilder();
			sb.append(ent.getName()).append("_Dup").append(n);
			DisplayEntity dup = InputAgent.generateEntityWithName(getJaamSimModel(), ent.getClass(), sb.toString());
			Entity.fastCopyInputs(ent, dup);

			// Set the state for the duplicated entity
			if (dup instanceof StateEntity) {
				String state = ((StateEntity) ent).getPresentState(simTime);
				((StateEntity) dup).setPresentState(state);
			}

			// Set the graphics for the duplicated entity
			dup.setRegion(ent.getCurrentRegion());
			dup.setPosition(ent.getPosition());
			dup.setDisplayModelList(ent.getDisplayModelList());
			dup.setSize(ent.getSize());
			dup.setOrientation(ent.getOrientation());
			dup.setAlignment(ent.getAlignment());

			// Send the duplicate to the target component
			target.addEntity(dup);
			n++;
		}

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

	// LinkDisplayable
	@Override
	public ArrayList<DisplayEntity> getDestinationEntities() {
		ArrayList<DisplayEntity> ret = new ArrayList<>();
		ArrayList<Linkable> ls = targetComponentList.getValue();
		if (ls == null)
			return ret;

		for (Linkable l : ls) {
			if (l != null && (l instanceof DisplayEntity)) {
				ret.add((DisplayEntity)l);
			}
		}
		return ret;
	}

	@Override
	public void linkTo(DisplayEntity nextEnt) {
		if (!(nextEnt instanceof Linkable) || nextEnt instanceof EntityGenerator) {
			return;
		}

		ArrayList<String> toks = new ArrayList<>();
		targetComponentList.getValueTokens(toks);
		toks.add(nextEnt.getName());
		KeywordIndex kw = new KeywordIndex(targetComponentList.getKeyword(), toks, null);
		InputAgent.storeAndExecute(new KeywordCommand(this, kw));
	}

}

@@ -1045,16 +1045,54 @@
 dIndex;%0A
+import com.jaamsim.input.StringInput;%0A
 import c
@@ -1454,16 +1454,274 @@
 tList;%0A%0A
+%09@Keyword(description = %22The base for the names assigned to the duplicated entities. %22%0A%09                     + %22The duplicated entities will be named Name1, Name2, etc.%22,%0A%09         exampleList = %7B%22Customer%22, %22Package%22%7D)%0A%09private final StringInput baseName;%0A%0A
 %09%7B%0A%09%09tar
@@ -1948,16 +1948,149 @@
 ntList);
+%0A%0A%09%09baseName = new StringInput(%22BaseName%22, KEY_INPUTS, null);%0A%09%09baseName.setDefaultText(%22EntityName_Dup%22);%0A%09%09this.addInput(baseName);
 %0A%09%7D%0A%0A%09pu
@@ -2223,16 +2223,190 @@
 ime();%0A%0A
+%09%09// Set the base name for the duplicates%0A%09%09String name = baseName.getValue();%0A%09%09if (name == null) %7B%0A%09%09%09name = ent.getName() + %22_Dup%22;%0A%09%09%09name = name.replace(%22.%22, %22_%22);%0A%09%09%7D%0A%0A
 %09%09// Mak
@@ -2563,105 +2563,8 @@
 ity%0A
-%09%09%09StringBuilder sb = new StringBuilder();%0A%09%09%09sb.append(ent.getName()).append(%22_Dup%22).append(n);%0A
 %09%09%09D
@@ -2655,21 +2655,16 @@
 (), 
-sb.toString()
+name + n
 );%0A%09

add 'BaseName' keyword to Duplicate