package lucee.runtime.functions.query;

import java.util.Map;

import lucee.runtime.PageContext;
import lucee.runtime.exp.ApplicationException;
import lucee.runtime.exp.FunctionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.type.Collection.Key;
import lucee.runtime.type.KeyImpl;
import lucee.runtime.type.Query;
import lucee.runtime.type.QueryImpl;

/**
 * implements BIF QueryRowData
 */
public class QueryRowByIndex extends BIF {

	private static final long serialVersionUID = -1462555083727605910L;

	public static double call(PageContext pc, Query query, String index) throws PageException {
		return Caster.toDoubleValue(getIndex(query, index));
	}

	public static double call(PageContext pc, Query query, String index, double defaultValue) {
		return Caster.toDoubleValue(getIndex(query, index, (int) defaultValue));
	}

	@Override
	public Object invoke(PageContext pc, Object[] args) throws PageException {
		if (args.length == 2) return call(pc, Caster.toQuery(args[0]), Caster.toString(args[1]));
		else if (args.length == 3) return call(pc, Caster.toQuery(args[0]), Caster.toString(args[1]), Caster.toDoubleValue(args[2]));
		throw new FunctionException(pc, "QueryRowByIndex", 2, 3, args.length);
	}

	public static int getIndex(Query query, String index) throws ApplicationException {
		Map<Key, Integer> indexes = ((QueryImpl) query).getIndexes();
		if (indexes == null) throw new ApplicationException("the query does not have no index table defined [" + index + "]");
		Integer indx = indexes.get(KeyImpl.getInstance(index));
		if (indx == null) throw new ApplicationException("there is no index with the value [" + index + "]");
		return indx;
	}

	public static int getIndex(Query query, String index, int defaultValue) {
		Map<Key, Integer> indexes = ((QueryImpl) query).getIndexes();
		if (indexes == null) return defaultValue;
		Integer indx = indexes.get(KeyImpl.getInstance(index));
		if (indx == null) return defaultValue;
		return indx;
	}
}
@@ -1509,54 +1509,35 @@
 on(%22
-the q
+Q
 uery 
-doe
+i
 s not 
-have no index table defined
+indexed, index
  %5B%22 
@@ -1544,24 +1544,34 @@
 + index + %22%5D
+ not found
 %22);%0A%09%09Intege
@@ -1676,40 +1676,51 @@
 on(%22
-there is no
+Query does not have an
  index 
-with
+for
  the 
-value
+column
  %5B%22 
@@ -2055,8 +2055,9 @@
 dx;%0A%09%7D%0A%7D
+%0A

improve indexed error messages