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

import java.util.ArrayList;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.Samples.SampleInput;
import com.jaamsim.input.EntityListInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.TimeUnit;

public class Combine extends LinkedService {

	@Keyword(description = "The service time required to perform the assembly process.",
	         exampleList = { "3.0 h", "NormalDistribution1", "'1[s] + 0.5*[TimeSeries1].PresentValue'" })
	private final SampleInput serviceTime;

	@Keyword(description = "A list of Queue objects in which to place the arriving sub-component entities.",
	         exampleList = {"Queue1 Queue2 Queue3"})
	private final EntityListInput<Queue> waitQueueList;

	private DisplayEntity processedEntity;	// the DisplayEntity being processed

	{
		waitQueue.setHidden(true);
		match.setHidden(true);

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

		waitQueueList = new EntityListInput<>(Queue.class, "WaitQueueList", "Key Inputs", null);
		waitQueueList.setRequired(true);
		this.addInput(waitQueueList);
	}

	public Combine() {}

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

	@Override
	public void addEntity( DisplayEntity ent ) {
		error("An entity cannot be sent directly to an Combine object. It must be sent to the appropriate queue.");
	}

	@Override
	public ArrayList<Queue> getQueues() {
		return waitQueueList.getValue();
	}

	@Override
	public void startAction() {

		// Stop if there is a forced downtime activity about to begin
		if (forcedDowntimePending) {
			forcedDowntimePending = false;
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Do any of the thresholds stop the generator?
		if (!this.isOpen()) {
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Do the queues have enough entities?
		ArrayList<Queue> queueList = waitQueueList.getValue();
		Integer m = Queue.selectMatchValue(queueList, null);
		if (m == null) {
			this.setBusy(false);
			this.setPresentState();
			return;
		}
		this.setMatchValue(m);

		// Remove one entity from each queue
		for (int i=queueList.size()-1; i>=0; i--) {
			DisplayEntity ent = queueList.get(i).removeFirstForMatch(m);
			if (ent == null)
				error("An entity with the specified match value %s was not found in %s.",
						m, queueList.get(i));
			this.registerEntity(ent);

			// Destroy all the entities but the first
			if (i == 0)
				processedEntity = ent;
			else
				ent.kill();
		}

		// Position the processed entity relative to the Assemble object
		this.moveToProcessPosition(processedEntity);

		// Schedule the completion of processing
		startTime = this.getSimTime();
		duration = serviceTime.getValue().getNextSample(startTime);
		this.scheduleProcess(duration, 5, endActionTarget, endActionHandle);
	}

	@Override
	public void endAction() {

		// Send the first entity to the next element in the chain
		this.sendToNextComponent(processedEntity);
		processedEntity = null;

		// Try to combine another set of entities
		this.startAction();
	}

}

@@ -2433,31 +2433,55 @@
 e%0A%09p
-ublic void startAction(
+rotected boolean startProcessing(double simTime
 ) %7B%0A
@@ -2617,65 +2617,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2713,65 +2713,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2907,65 +2907,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -3489,213 +3489,19 @@
 %0A%0A%09%09
-// Schedule the completion of processing%0A%09%09startTime = this.getSimTime();%0A%09%09duration = serviceTime.getValue().getNextSample(startTime);%0A%09%09this.scheduleProcess(duration, 5, endActionTarget, endActionHandle)
+return true
 ;%0A%09%7D
@@ -3511,29 +3511,32 @@
 @Override%0A%09p
-ublic
+rotected
  void endAct
@@ -3532,23 +3532,41 @@
 void end
-Action(
+Processing(double simTime
 ) %7B%0A%0A%09%09/
@@ -3697,72 +3697,130 @@
 ll;%0A
-%0A%09%09// Try to combine another set of entities%0A%09%09this.startAction(
+%09%7D%0A%0A%09@Override%0A%09protected double getProcessingTime(double simTime) %7B%0A%09%09return serviceTime.getValue().getNextSample(simTime
 );%0A%09

use the new LinkedService framework for Combine