package org.yakindu.sct.simulation.ui.model.presenter;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.yakindu.sct.model.sexec.ReactionFired;
import org.yakindu.sct.model.sexec.TraceBeginRunCycle;
import org.yakindu.sct.model.sexec.TraceEndRunCycle;
import org.yakindu.sct.model.sexec.TraceNodeExecuted;
import org.yakindu.sct.model.sexec.TraceStateEntered;
import org.yakindu.sct.model.sexec.TraceStateExited;
import org.yakindu.sct.model.sgraph.RegularState;
import org.yakindu.sct.simulation.core.runtime.IExecutionContext;

import de.itemis.gmf.runtime.commons.highlighting.HighlightingParameters;
import de.itemis.gmf.runtime.commons.highlighting.IHighlightingSupport;
import de.itemis.gmf.runtime.commons.highlighting.IHighlightingSupport.Action;

/**
 * This dynamic notation handler highlight the current states, the previous
 * active states and the execution paths that are the transitions and pseudo
 * states that were taken in the last run to completion step (runCycle).
 * 
 * @author terfloth
 */
public class ExecutionPathDynamicNotationHandler extends
		DefaultDynamicNotationHandler {

	/** stores the highlighting actions for the last execution cycle */
	protected List<Action> cycleUpdates = new ArrayList<Action>();

	protected List<RegularState> active = new ArrayList<RegularState>();
	protected List<RegularState> previousActive = new ArrayList<RegularState>();
	protected List<EObject> executionPathElements = new ArrayList<EObject>();

	public void restoreNotationState(IExecutionContext context) {
		List<Action> actionList = new ArrayList<Action>();

		for (RegularState state : context.getAllActiveStates()) {
			active.add(state);
			actionList.add(new IHighlightingSupport.Highlight(state,
					STATE_HIGHLIGHT_PARAMS));
		}

		getHighlightingSupport().executeBatch(actionList);

	}

	public void visualizeStep(final TraceBeginRunCycle trace) {

		cycleUpdates.clear();

		for (EObject obj : previousActive) {
			cycleUpdates.add(new IHighlightingSupport.Highlight(obj, null));
		}
		previousActive.clear();

		for (EObject obj : executionPathElements) {
			cycleUpdates.add(new IHighlightingSupport.Highlight(obj, null));
		}
		executionPathElements.clear();

	}

	public void visualizeStep(final TraceEndRunCycle trace) {

		List<Action> actions = new ArrayList<Action>(cycleUpdates);
		getHighlightingSupport().executeBatch(actions);

	}

	public void visualizeStep(final TraceStateEntered trace) {

		active.add((RegularState) trace.getState().getSourceElement());
		cycleUpdates.add(new IHighlightingSupport.Highlight(trace.getState()
				.getSourceElement(), HighlightingParameters.DEFAULT));

	}

	public void visualizeStep(final TraceStateExited trace) {

		RegularState state = (RegularState) trace.getState().getSourceElement();

		active.remove(state);
		// We copy the state here, otherwise the previous active states would be
		// deleted in visualizeSteps cycleUpdates.clear() for self transitions
		previousActive.add(EcoreUtil.copy(state));
		cycleUpdates.add(new IHighlightingSupport.Highlight(trace.getState()
				.getSourceElement(), VERTEX_TRANSIENT_PARAMS));

	}

	public void visualizeStep(final ReactionFired trace) {

		executionPathElements.add(trace.getReaction().getSourceElement());
		cycleUpdates.add(new IHighlightingSupport.Highlight(trace.getReaction()
				.getSourceElement(), TRANSITION_PARAMS));

	}

	public void visualizeStep(final TraceNodeExecuted trace) {

		executionPathElements.add(trace.getNode().getSourceElement());
		cycleUpdates.add(new IHighlightingSupport.Highlight(trace.getNode()
				.getSourceElement(), VERTEX_TRANSIENT_PARAMS));

	}

}

@@ -1,8 +1,9 @@
+%0A
 package 
@@ -144,53 +144,8 @@
 ct;%0A
-import org.eclipse.emf.ecore.util.EcoreUtil;%0A
 impo
@@ -1985,24 +1985,192 @@
 usActive) %7B%0A
+%09%09%09// remove highlighting of previously active state only if it is not%0A%09%09%09// currently active (which is the case for self-transitions)%0A%09%09%09if (!active.contains(obj)) %7B%0A%09
 %09%09%09cycleUpda
@@ -2227,17 +2227,23 @@
 ll));%0A%09%09
-%7D
+%09%7D%0A%09%09%7D%0A
 %0A%09%09previ
@@ -3021,156 +3021,8 @@
 e);%0A
-%09%09// We copy the state here, otherwise the previous active states would be%0A%09%09// deleted in visualizeSteps cycleUpdates.clear() for self transitions%0A
 %09%09pr
@@ -3042,31 +3042,16 @@
 add(
-EcoreUtil.copy(
 state)
-)
 ;
+%0A
 %0A%09%09c

remove highlighting of previously active state only if it is not currently active (which is the case for self-transitions)