/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2002-2011 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.sandwell.JavaSimulation3D;

import java.util.ArrayList;

import com.jaamsim.events.ProcessTarget;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.Parser;
import com.jaamsim.input.ValueInput;
import com.jaamsim.units.TimeUnit;
import com.sandwell.JavaSimulation.Entity;
import com.sandwell.JavaSimulation.FileEntity;
import com.sandwell.JavaSimulation.InputErrorException;
import com.sandwell.JavaSimulation.Keyword;
import com.sandwell.JavaSimulation.Process;
import com.sandwell.JavaSimulation.StringInput;
import com.sandwell.JavaSimulation.Util;

public class ScriptEntity extends Entity {


	@Keyword(description = "The name of the script file for the script entity.",
	         example = "ScriptEntity Script { test.scr }")
	private final StringInput scriptFileName;

	@Keyword(description = "The Time keyword appears inside the script file. The value represents the simulation " +
	                "time at which the next set of commands in the script are implemented.",
	         example = "ScriptEntity Time { 24.0 h }")
	private final ValueInput scriptTime; // the time that has been read in the script

	{
		scriptFileName = new StringInput( "Script", "Key Inputs", null );
		this.addInput( scriptFileName, true );

		scriptTime = new ValueInput("Time", "Key Inputs", 0.0d);
		scriptTime.setUnitType(TimeUnit.class);
		scriptTime.setValidRange(0.0d, Double.POSITIVE_INFINITY);
		this.addInput(scriptTime, false);
	}

	public ScriptEntity() {}

	private static class ScriptTarget extends ProcessTarget {
		final ScriptEntity script;

		ScriptTarget(ScriptEntity script) {
			this.script = script;
		}

		@Override
		public String getDescription() {
			return script.getInputName() + ".doScript";
		}

		@Override
		public void process() {
			script.doScript();
		}
	}

	@Override
	public void startUp() {
		Process.start(new ScriptTarget(this));
	}

	/**
	 * Read the script
	 */
	public void doScript() {

		// If there is no script file, do nothing
		if( scriptFileName.getValue() == null ) {
			return;
		}

		// If the script file exists, open it
		FileEntity scriptFile;
		if( FileEntity.fileExists( Util.getAbsoluteFilePath( scriptFileName.getValue() ) ) ) {
			scriptFile = new FileEntity( Util.getAbsoluteFilePath( scriptFileName.getValue() ), FileEntity.FILE_READ, true );
		}
		else {
			throw new InputErrorException( "The script file " + scriptFileName.getValue() + " was not found" );
		}

		ArrayList<String> tokens = new ArrayList<String>();
		while (true) {
			String line = scriptFile.readLine();
			if (line == null)
				break;

			Parser.tokenize(tokens, line, true);
			if (tokens.size() == 0)
				continue;

			InputAgent.processKeywordRecord(tokens, null);
			tokens.clear();

			// If a "Time" record was read, then wait until the time
			long delayTicks = Process.secondsToTicks(scriptTime.getValue()) - getSimTicks();
			if (delayTicks > 0)
				simWaitTicks(delayTicks);
		}
	}
}

@@ -977,35 +977,25 @@
 ulation.
+File
 Input
-ErrorException
 ;%0Aimport
@@ -1079,97 +1079,8 @@
 ess;
-%0Aimport com.sandwell.JavaSimulation.StringInput;%0Aimport com.sandwell.JavaSimulation.Util;
 %0A%0Apu
@@ -1271,22 +1271,20 @@
 e final 
-String
+File
 Input sc
@@ -1664,22 +1664,20 @@
 e = new 
-String
+File
 Input( %22
@@ -2610,186 +2610,40 @@
 File
-;%0A%09%09if( FileEntity.fileExists( Util.getAbsoluteFilePath( scriptFileName.getValue() ) ) ) %7B%0A%09%09%09scriptFile = new FileEntity( Util.getAbsoluteFilePath( scriptFileName.getValue() ), 
+ = scriptFileName.getFileEntity(
 File
@@ -2668,133 +2668,11 @@
 true
- 
 );%0A
-%09%09%7D%0A%09%09else %7B%0A%09%09%09throw new InputErrorException( %22The script file %22 + scriptFileName.getValue() + %22 was not found%22 );%0A%09%09%7D%0A%0A
 %09%09Ar

convert scriptentity to use a FileInput for the input file