/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2017-2018 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.net.URI;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.ProcessFlow.LinkedComponent;
import com.jaamsim.basicsim.Entity;
import com.jaamsim.input.ExpError;
import com.jaamsim.input.ExpEvaluator;
import com.jaamsim.input.ExpParser;
import com.jaamsim.input.ExpResult;
import com.jaamsim.input.FileInput;
import com.jaamsim.input.Input;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.TimeUnit;
import com.jaamsim.input.ExpParser.Expression;

public abstract class FileToArray extends LinkedComponent {

	@Keyword(description = "A file containing entries that are delimited by spaces and/or tabs.",
	         exampleList = {"'c:/test/data.txt'"})
	private final FileInput dataFile;

	{
		nextComponent.setRequired(false);

		dataFile = new FileInput("DataFile", KEY_INPUTS, null);
		dataFile.setRequired(true);
		this.addInput(dataFile);
	}

	public FileToArray() {}

	@Override
	public void updateForInput(Input<?> in) {
		super.updateForInput(in);

		if (in == dataFile) {
			if (dataFile.getValue() == null) {
				clearValue();
				return;
			}
			setValueForURI(dataFile.getValue(), 0.0d);
			return;
		}
	}

	@Override
	public void earlyInit() {
		super.earlyInit();
		setValueForURI(dataFile.getValue(), 0.0d);
	}

	@Override
	public void addEntity(DisplayEntity ent) {
		super.addEntity(ent);
		setValueForURI(dataFile.getValue(), getSimTime());
		sendToNextComponent(ent);
	}

	protected ExpResult getExpResult(int i, String str, double simTime) {

		// Is the entry a time stamp?
		if (Input.isRFC8601DateTime(str)) {
			try {
				double time = Input.parseRFC8601DateTime(str)/1e6;
				return ExpResult.makeNumResult(time, TimeUnit.class);
			}
			catch (Exception e) {}
		}

		// Is the entry an entity?
		Entity ent = Entity.getNamedEntity(str);
		if (ent != null) {
			return ExpResult.makeEntityResult(ent);
		}

		// Is the entry a valid expression?
		try {
			ExpEvaluator.EntityParseContext pc = ExpEvaluator.getParseContext(this, str);
			Expression exp = ExpParser.parseExpression(pc, str);
			return ExpEvaluator.evaluateExpression(exp, simTime);
		}
		catch (ExpError e) {}

		// If all else fails, return a string
		return ExpResult.makeStringResult(str);
	}

	protected abstract void setValueForURI(URI uri, double simTime);
	protected abstract void clearValue();

}

@@ -1247,16 +1247,21 @@
 on = %22A 
+text 
 file con
@@ -1272,56 +1272,1285 @@
 ing 
-entries that are delimited by spaces and/or tabs
+one or more records whose entries are %22%0A%09                     + %22delimited by tabs and/or spaces.%5Cn%5Cn%22%0A%09                     + %22The following types of entries can be used. %22%0A%09                     + %22If an entry includes spaces, double quotation marks, or curly braces, %22%0A%09                     + %22it must be enclosed by single quotation marks.%5Cn%22%0A%09                     + %22- Comments. Records that begin with a # symbol are ignored (e.g. # abc)%5Cn%22%0A%09                     + %22- Numbers with or without units, specified in expression format (e.g. 5.2%5Bm%5D)%5Cn%22%0A%09                     + %22- Strings (e.g. 'quick red fox' or quick_red_fox)%5Cn%22%0A%09                     + %22- Entity names (e.g. DisplayEntity1)%5Cn%22%0A%09                     + %22- Time stamps in YYYY-MM-DD HH:MM:SS.SSS or YYYY-MM-DDTHH:MM:SS.SSS format %22%0A%09                     + %22(e.g. '2018-06-31 13:00:00.000' or 2018-06-31T13:00:00.000)%5Cn%22%0A%09                     + %22- Arrays of numbers, entities, strings, or arrays, specified in expression format %22%0A%09                     + %22(e.g. '%7B 5%5Bm%5D, %5C%22abc%5C%22, %5BDisplayEntity1%5D %7D'%5Cn%22%0A%09                     + %22- Expressions. A valid expression is executed and saved when the file is read %22%0A%09                     + %22(e.g. 1%5Bm%5D/2%5Bs%5D is saved as 1.0%5Bm/s%5D). An invalid expression is saved as a string
 .%22,%0A

revise the description of the DataFile keyword for FileToArray