/*
 * 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.input.EntityInput;
import com.jaamsim.input.InputErrorException;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.ValueInput;
import com.jaamsim.units.TimeUnit;

public class EntityGate extends LinkedService {

	@Keyword(description = "The queue in which the waiting DisplayEntities will be placed.",
	         example = "EntityGate1 WaitQueue { Queue1 }")
	private final EntityInput<Queue> waitQueue;

	@Keyword(description = "The time delay before each queued entity is released.\n" +
			"Entities arriving at an open gate are not delayed.",
	         example = "EntityGate1 ReleaseDelay { 5.0 s }")
	private final ValueInput releaseDelay;

	{
		waitQueue = new EntityInput<>(Queue.class, "WaitQueue", "Key Inputs", null);
		this.addInput(waitQueue);

		releaseDelay = new ValueInput("ReleaseDelay", "Key Inputs", 0.0);
		releaseDelay.setUnitType(TimeUnit.class);
		releaseDelay.setValidRange(0.0, Double.POSITIVE_INFINITY);
		this.addInput(releaseDelay);
	}

	public EntityGate() {}

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

		// Confirm that the target queue has been specified
		if (waitQueue.getValue() == null) {
			throw new InputErrorException("The keyword WaitQueue must be set.");
		}
	}

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

		// If the gate is closed or other entities are already queued, then add the entity to the queue
		Queue queue = waitQueue.getValue();
		if (queue.getCount() > 0 || this.isClosed()) {
			queue.addEntity(ent);
			return;
		}

		// If the gate is open and there are no other entities still in the queue, then send the entity to the next component
		this.sendToNextComponent(ent);
	}

	@Override
	public void startAction() {

		// Stop if the gate has closed or the queue has become empty
		if (this.isClosed() || waitQueue.getValue().getCount() == 0) {
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Schedule the release of the next entity
		this.scheduleProcess(releaseDelay.getValue(), 5, endActionTarget);
	}

	/**
	 * Loop recursively through the queued entities, releasing them one by one.
	 */
	@Override
	public void endAction() {

		// Release the first element in the queue and send to the next component
		this.sendToNextComponent(waitQueue.getValue().removeFirst());

		// Try to release another entity
		this.startAction();
	}

}

@@ -630,16 +630,45 @@
 jects;%0A%0A
+import java.util.ArrayList;%0A%0A
 import c
@@ -939,16 +939,37 @@
 Service 
+implements QueueUser 
 %7B%0A%0A%09@Key
@@ -2428,24 +2428,371 @@
 t(ent);%0A%09%7D%0A%0A
+%09@Override%0A%09public ArrayList%3CQueue%3E getQueues() %7B%0A%09%09ArrayList%3CQueue%3E ret = new ArrayList%3C%3E();%0A%09%09ret.add(waitQueue.getValue());%0A%09%09return ret;%0A%09%7D%0A%0A%09@Override%0A%09public void queueChanged() %7B%0A%0A%09%09// If necessary, restart processing%0A%09%09if (this.isOpen() && !this.isBusy()) %7B%0A%09%09%09this.setBusy(true);%0A%09%09%09this.setPresentState();%0A%09%09%09this.startAction();%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Override%0A%09

implement the QueueUser interface for EntityGate