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

public class EntityGate extends LinkedService {

	@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;

	{
		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 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();
	}

}

@@ -1073,11 +1073,99 @@
 y;%0A%0A
+%09private DisplayEntity servedEntity; // the entity about to be released from the queue%0A%0A
 %09%7B%0A
-
 %09%09re
@@ -1393,16 +1393,102 @@
 e() %7B%7D%0A%0A
+%09@Override%0A%09public void earlyInit() %7B%0A%09%09super.earlyInit();%0A%09%09servedEntity = null;%0A%09%7D%0A%0A
 %09@Overri
@@ -2215,32 +2215,71 @@
 the next entity%0A
+%09%09servedEntity = this.getNextEntity();%0A
 %09%09this.scheduleP
@@ -2567,43 +2567,44 @@
 ent(
-waitQueue.getValue().removeFirst())
+servedEntity);%0A%09%09servedEntity = null
 ;%0A%0A%09

set obj to the entity being released from the queue by EntityGate