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

public class Unpack extends LinkedService {

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

	private int numberToRemove;   // Number of entities to remove from the present EntityContainer

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

	private EntityContainer container;	// the received EntityContainer
	private int numberRemoved;   // Number of entities removed from the received EntityContainer

	public Unpack() {}

	@Override
	public void earlyInit() {
		super.earlyInit();
		container = null;
		numberRemoved = 0;
	}

	@Override
	public void startAction() {

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

		// Determine the match value
		Integer m = this.getNextMatchValue(getSimTime());

		// Is there a container waiting to be unpacked?
		if (container == null && waitQueue.getValue().getMatchCount(m) == 0) {
			this.setBusy(false);
			this.setPresentState();
			return;
		}

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

		if (container == null) {

			// Remove the container from the queue
			container = (EntityContainer)this.getNextEntityForMatch(m);
			numberToRemove = this.getNumberToRemove();
			numberRemoved = 0;

			// Position the container over the unpack object
			this.moveToProcessPosition(container);
		}

		// Schedule the removal of the next entity
		startTime = this.getSimTime();
		duration = 0.0;
		if (numberRemoved < numberToRemove && container.getCount() > 0)
			duration = serviceTime.getValue().getNextSample(startTime);
		this.scheduleProcess(duration, 5, endActionTarget, endActionHandle);
	}

	protected void disposeContainer(EntityContainer c) {
		c.kill();
	}

	protected int getNumberToRemove() {
		return Integer.MAX_VALUE;
	}

	@Override
	public void endAction() {

		// Remove the next entity from the container
		if (numberRemoved < numberToRemove && container.getCount() > 0) {
			this.sendToNextComponent(container.removeEntity());
			numberRemoved++;
		}

		// Stop when the desired number of entities have been removed
		if (container.getCount() == 0 || numberRemoved == numberToRemove) {
			this.disposeContainer(container);
			container = null;
		}

		// Continue the process
		this.startAction();
	}

}

@@ -1823,31 +1823,55 @@
 e%0A%09p
-ublic void startAction(
+rotected boolean startProcessing(double simTime
 ) %7B%0A
@@ -2028,65 +2028,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2257,65 +2257,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2353,65 +2353,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2675,300 +2675,19 @@
 %0A%0A%09%09
-// Schedule the removal of the next entity%0A%09%09startTime = this.getSimTime();%0A%09%09duration = 0.0;%0A%09%09if (numberRemoved %3C numberToRemove && container.getCount() %3E 0)%0A%09%09%09duration = serviceTime.getValue().getNextSample(startTime);%0A%09%09this.scheduleProcess(duration, 5, endActionTarget, endActionHandle)
+return true
 ;%0A%09%7D
@@ -2836,29 +2836,32 @@
 @Override%0A%09p
-ublic
+rotected
  void endAct
@@ -2857,23 +2857,41 @@
 void end
-Action(
+Processing(double simTime
 ) %7B%0A%0A%09%09/
@@ -3282,55 +3282,231 @@
 %09%09%7D%0A
-%0A%09%09// Continue the process%0A%09%09this.startAction()
+%09%7D%0A%0A%09@Override%0A%09protected double getProcessingTime(double simTime) %7B%0A%09%09double dur = 0.0;%0A%09%09if (numberRemoved %3C numberToRemove && container.getCount() %3E 0)%0A%09%09%09dur = serviceTime.getValue().getNextSample(simTime);%0A%09%09return dur
 ;%0A%09%7D

use the new LinkedService framework for Unpack