package org.metaborg.runtime.task.interop;

import java.util.Collection;

import org.metaborg.runtime.task.TaskEngine;
import org.metaborg.runtime.task.TaskManager;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoList;
import org.spoofax.interpreter.terms.IStrategoString;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.IStrategoTuple;
import org.spoofax.interpreter.terms.ITermFactory;
import org.strategoxt.lang.Context;
import org.strategoxt.lang.Strategy;

public class task_api_debug_info_0_1 extends Strategy {
	public static task_api_debug_info_0_1 instance = new task_api_debug_info_0_1();

	@Override
	public IStrategoTerm invoke(Context context, IStrategoTerm current, IStrategoTerm partitionOrID) {
		final ITermFactory factory = context.getFactory();
		final TaskEngine engine = TaskManager.getInstance().getCurrent();

		if(Tools.isTermString(partitionOrID)) {
			Collection<IStrategoTerm> taskIDs = engine.getInPartition((IStrategoString) partitionOrID);
			IStrategoList list = factory.makeList();
			for(IStrategoTerm taskID : taskIDs) {
				list = factory.makeListCons(createDebugTuple(taskID, engine, factory), list);
			}

			return list;
		} else if(Tools.isTermInt(partitionOrID)) {
			return createDebugTuple(partitionOrID, engine, factory);
		} else if(Tools.isTermTuple(partitionOrID)) {
			IStrategoList list = factory.makeList();
			for(IStrategoTerm taskID : engine.getTaskIDs()) {
				list = factory.makeListCons(createDebugTuple(taskID, engine, factory), list);
			}

			return list;
		}

		return null;
	}

	private IStrategoTuple createDebugTuple(IStrategoTerm taskID, TaskEngine engine, ITermFactory factory) {
		IStrategoTerm instruction = engine.getInstruction(taskID);
		IStrategoList dependencies = factory.makeList(engine.getDependencies(taskID));
		IStrategoTerm message = engine.getMessage(taskID);
		IStrategoTerm result =
			engine.hasFailed(taskID) ? factory.makeAppl(factory.makeConstructor("Fail", 0)) : engine.getResult(taskID);

		return factory.makeTuple(taskID, instruction, dependencies,
			result == null ? factory.makeAppl(factory.makeConstructor("None", 0)) : result,
			message == null ? factory.makeAppl(factory.makeConstructor("None", 0)) : message);
	}
}

@@ -41,38 +41,8 @@
 p;%0A%0A
-import java.util.Collection;%0A%0A
 impo
@@ -739,17 +739,24 @@
 egoTerm 
-p
+tupleOrP
 artition
@@ -906,16 +906,22 @@
 Term
-String(p
+Tuple(tupleOrP
 arti
@@ -940,85 +940,111 @@
 %0A%09%09%09
-Collection%3CIStrategoTerm%3E t
+return createDebugTuples(engine.getT
 askIDs
- =
+(),
  engine
-.getInPartition((IStrategoString) p
+, factory);%0A%09%09%7D else if(Tools.isTermString(tupleOrP
 arti
@@ -1056,150 +1056,101 @@
 rID)
-;
+) %7B
 %0A%09%09%09
-IStrategoList list = factory.makeList();%0A%09%09%09for(IStrategoTerm taskID : taskIDs) %7B%0A%09%09%09%09list = factory.makeListCons(createDebugTuple(task
+return createDebugTuples(engine.getInPartition((IStrategoString) tupleOrPartitionOr
 ID
+)
 , en
@@ -1167,37 +1167,8 @@
 ory)
-, list);%0A%09%09%09%7D%0A%0A%09%09%09return list
 ;%0A%09%09
@@ -1193,17 +1193,24 @@
 TermInt(
-p
+tupleOrP
 artition
@@ -1241,25 +1241,32 @@
 eDebugTuple(
-p
+tupleOrP
 artitionOrID
@@ -1292,54 +1292,146 @@
 %0A%09%09%7D
- else if(Tools.isTermTuple(partitionOrID)
+%0A%0A%09%09return null;%0A%09%7D%0A%0A%09private IStrategoList createDebugTuples(Iterable%3CIStrategoTerm%3E taskIDs, TaskEngine engine, ITermFactory factory
 ) %7B%0A
-%09
 %09%09IS
@@ -1471,17 +1471,16 @@
 st();%0A%09%09
-%09
 for(IStr
@@ -1502,32 +1502,19 @@
 D : 
-engine.getT
+t
 askIDs
-()
 ) %7B%0A
-%09
 %09%09%09l
@@ -1592,21 +1592,18 @@
 ist);%0A%09%09
-%09
 %7D%0A
-%0A%09
 %09%09return
@@ -1609,36 +1609,16 @@
 n list;%0A
-%09%09%7D%0A%0A%09%09return null;%0A
 %09%7D%0A%0A%09pri

Add a way to get debug information for all tasks.