/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada 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.Samples;

import java.util.ArrayList;
import java.util.Collections;

import com.jaamsim.basicsim.Entity;
import com.jaamsim.input.Input;
import com.jaamsim.input.InputErrorException;
import com.jaamsim.input.KeywordIndex;
import com.jaamsim.units.Unit;

public class SampleInput extends Input<SampleProvider> {
	private Class<? extends Unit> unitType;
	private Entity thisEnt;
	private double minValue = Double.NEGATIVE_INFINITY;
	private double maxValue = Double.POSITIVE_INFINITY;

	public SampleInput(String key, String cat, SampleProvider def) {
		super(key, cat, def);
	}

	public void setUnitType(Class<? extends Unit> u) {
		if (u != unitType)
			this.reset();
		unitType = u;
		if (defValue instanceof SampleConstant)
			((SampleConstant)defValue).setUnitType(unitType);
		if (defValue instanceof TimeSeriesConstantDouble)
			((TimeSeriesConstantDouble)defValue).setUnitType(unitType);
	}

	public void setEntity(Entity ent) {
		thisEnt = ent;
	}

	public void setValidRange(double min, double max) {
		minValue = min;
		maxValue = max;
	}

	@Override
	public void parse(KeywordIndex kw)
	throws InputErrorException {
		value = Input.parseSampleExp(kw, thisEnt, minValue, maxValue, unitType);
	}

	@Override
	public ArrayList<String> getValidOptions() {
		ArrayList<String> list = new ArrayList<>();
		for (Entity each : Entity.getClonesOfIterator(Entity.class, SampleProvider.class)) {
			SampleProvider sp = (SampleProvider)each;
			if (sp.getUnitType() == unitType)
				list.add(each.getName());
		}
		Collections.sort(list, Input.uiSortOrder);
		return list;
	}


	@Override
	public void getValueTokens(ArrayList<String> toks) {
		if (value == null) return;


		if (value instanceof SampleExpression) {
			SampleExpression se = (SampleExpression)value;
			toks.add(se.getExpressionString());
			return;
		}
		if (	value instanceof SampleConstant ||
				value instanceof SampleOutput) {
			super.getValueTokens(toks);
			return;
		}
		else {
			toks.add(((Entity)value).getName());
		}
	}

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

		if (value == null) return;
		if (value instanceof SampleExpression) return;
		if (value instanceof SampleConstant) return;

		Input.assertUnitsMatch(unitType, value.getUnitType());

		if (value.getMinValue() < minValue)
			throw new InputErrorException("The minimum value allowed for keyword: '%s' is: %s.\n" +
					"The specified entity: '%s' can return values as small as: %s.",
					this.getKeyword(), minValue, ((Entity)value).getName(), value.getMinValue());

		if (value.getMaxValue() > maxValue)
			throw new InputErrorException("The maximum value allowed for keyword: '%s' is: %s.\n" +
					"The specified entity: '%s' can return values as large as: %s.",
					this.getKeyword(), maxValue, ((Entity)value).getName(), value.getMaxValue());
	}
}

@@ -2244,17 +2244,16 @@
 st;%0A%09%7D%0A%0A
-%0A
 %09@Overri
@@ -2343,163 +2343,70 @@
 n;%0A%0A
-%0A
 %09%09
-if (value instanceof SampleExpression) %7B%0A%09%09%09SampleExpression se = (SampleExpression)value;%0A%09%09%09toks.add(se.getExpressionString());%0A%09%09%09return;%0A%09%09%7D
+// Preserve the exact text for a constant value input
 %0A%09%09if (
-%09
 valu
@@ -2436,45 +2436,8 @@
 tant
- %7C%7C%0A%09%09%09%09value instanceof SampleOutput
 ) %7B%0A
@@ -2486,60 +2486,86 @@
 %09%09%7D%0A
+%0A
 %09%09
-else %7B%0A%09%09%09toks.add(((Entity)value).getName());%0A%09%09%7D
+// All other inputs can be built from scratch%0A%09%09toks.add(value.toString());
 %0A%09%7D%0A

simplify getValueTokens for SampleInput