/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 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.input.EntityInput;
import com.jaamsim.input.InputErrorException;
import com.jaamsim.input.Keyword;
import com.jaamsim.math.Vec3d;
import com.jaamsim.units.TimeUnit;

/**
 * Server processes entities one by one from a queue.  When finished with an entity, it passes it to the next
 * LinkedComponent in the chain.
 */
public class Server extends LinkedService {

	@Keyword(description = "The service time required to process an entity.\n" +
			"A constant value, a distribution to be sampled, or a time series can be entered.",
	         example = "Server1 ServiceTime { 3.0 h }")
	private final SampleExpInput serviceTimeInput;

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

	private DisplayEntity servedEntity;	// the DisplayEntity being server

	{
		serviceTimeInput = new SampleExpInput("ServiceTime", "Key Inputs", new SampleConstant(TimeUnit.class, 0.0));
		serviceTimeInput.setUnitType(TimeUnit.class);
		serviceTimeInput.setEntity(this);
		serviceTimeInput.setValidRange(0, Double.POSITIVE_INFINITY);
		this.addInput(serviceTimeInput);

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

	public Server() {}

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

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

		serviceTimeInput.validate();
	}

	@Override
	public void earlyInit() {
		super.earlyInit();
		servedEntity = null;
	}

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

		// Add the entity to the queue
		waitQueueInput.getValue().addEntity(ent);

		// If necessary, wake up the server
		if (!this.isBusy() && this.isOpen()) {
			this.setBusy(true);
			this.setPresentState();
			this.startAction();
		}
	}

	@Override
	public void startAction() {

		// Stop if the queue is empty or a threshold is closed
		if (waitQueueInput.getValue().getCount() == 0 || this.isClosed()) {
			servedEntity = null;
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Remove the first entity from the queue
		servedEntity = waitQueueInput.getValue().removeFirst();
		double dt = serviceTimeInput.getValue().getNextSample(getSimTime());

		// Schedule the completion of service
		this.scheduleProcess(dt, 5, endActionTarget);
	}

	@Override
	public void endAction() {

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

		// Remove the next entity from the queue and start processing
		this.startAction();
	}

	@Override
	public void updateGraphics(double simTime) {

		// If an entity is being served, show it at the center of the Server
		if (servedEntity != null) {
			Vec3d serverCenter = this.getPositionForAlignment(new Vec3d());
			servedEntity.setPosition(serverCenter);
		}
	}

}

@@ -1399,21 +1399,16 @@
 viceTime
-Input
 ;%0A%0A%09@Key
@@ -1587,21 +1587,16 @@
 aitQueue
-Input
 ;%0A%0A%09priv
@@ -1678,21 +1678,16 @@
 viceTime
-Input
  = new S
@@ -1780,29 +1780,24 @@
 %09serviceTime
-Input
 .setUnitType
@@ -1823,29 +1823,24 @@
 %09serviceTime
-Input
 .setEntity(t
@@ -1858,21 +1858,16 @@
 viceTime
-Input
 .setVali
@@ -1930,21 +1930,16 @@
 viceTime
-Input
 );%0A%0A%09%09wa
@@ -1945,21 +1945,16 @@
 aitQueue
-Input
  = new E
@@ -2038,21 +2038,16 @@
 aitQueue
-Input
 );%0A%09%7D%0A%0A%09
@@ -2185,37 +2185,32 @@
 %0A%09%09if (waitQueue
-Input
 .getValue() == n
@@ -2306,21 +2306,16 @@
 viceTime
-Input
 .validat
@@ -2525,37 +2525,32 @@
 ueue%0A%09%09waitQueue
-Input
 .getValue().addE
@@ -2827,37 +2827,32 @@
 %0A%09%09if (waitQueue
-Input
 .getValue().getC
@@ -3046,21 +3046,16 @@
 aitQueue
-Input
 .getValu
@@ -3098,21 +3098,16 @@
 viceTime
-Input
 .getValu

use standard naming convention for Server inputs