package org.gemoc.execution.engine.io.views.obeo;

import java.util.Observable;
import java.util.Observer;

import org.gemoc.execution.engine.core.ObservableBasicExecutionEngine;
import org.gemoc.execution.engine.trace.gemoc_execution_trace.Choice;
import org.gemoc.execution.engine.trace.gemoc_execution_trace.ExecutionTraceModel;

import fr.obeo.timeline.view.AbstractTimelineProvider;

public class TimelineProvider extends AbstractTimelineProvider implements Observer {

	private ObservableBasicExecutionEngine _engine;
	
	public TimelineProvider(ObservableBasicExecutionEngine engine) {
		_engine = engine;
		_engine.addObserver(this);
	}
	
	private ExecutionTraceModel getExecutionTrace() {
		return _engine.getExecutionTrace();
	}

	@Override
	public int getNumberOfTicks() {
		int numberOfChoices = getExecutionTrace().getChoices().size();
		return numberOfChoices;
	}

	@Override
	public int getNumberOfchoicesAt(int index) {
		int numberOfPossibleSteps = getExecutionTrace().getChoices().get(index).getPossibleLogicalSteps().size();
		return numberOfPossibleSteps;
	}

	@Override
	public String getTextAt(int index) {
		return String.valueOf(index);
	}

	@Override
	public Object getAt(int index, int choice) {
		return getExecutionTrace().getChoices().get(index).getPossibleLogicalSteps().get(choice);
	}

	@Override
	public String getTextAt(int index, int choice) {
		return "titi";
	}

	@Override
	public int getFollowing(int index, int choice) {
		int result = -1;
		if (index < getNumberOfTicks())
		{
			Choice gemocChoice = getExecutionTrace().getChoices().get(index);
			int chosenLogicalStepIndex = gemocChoice.getPossibleLogicalSteps().indexOf(gemocChoice.getChosenLogicalStep());
			if (chosenLogicalStepIndex == choice
				&& gemocChoice.getNextChoice() != null)
			{
				Choice nextChoice = gemocChoice.getNextChoice();
				result = nextChoice.getPossibleLogicalSteps().indexOf(nextChoice.getChosenLogicalStep());
			}			
		}
		return result;
	}

	@Override
	public int getPreceding(int index, int choice) {
		int result = -1;
		if (index < getNumberOfTicks())
		{
			Choice gemocChoice = getExecutionTrace().getChoices().get(index);
			int chosenLogicalStepIndex = gemocChoice.getPossibleLogicalSteps().indexOf(gemocChoice.getChosenLogicalStep());
			if (chosenLogicalStepIndex == choice
				&& index > 0)
			{
				Choice lastChoice = getExecutionTrace().getChoices().get(index-1);
				result = lastChoice.getPossibleLogicalSteps().indexOf(lastChoice.getChosenLogicalStep());
			}			
		}
		return result;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		notifyNumberOfticksChanged(getExecutionTrace().getChoices().size());
	}

	@Override
	public boolean isSelected(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

//	@Override
//	public int getSelectedChoice(int index) {
//		Choice gemocChoice = getExecutionTrace().getChoices().get(index);
//		return gemocChoice.getPossibleLogicalSteps().indexOf(gemocChoice.getChosenLogicalStep());
//	}

}

@@ -2746,32 +2746,36 @@
 size());%0D%0A%09%7D%0D%0A%0D%0A
+%0D%0A//
 %09@Override%0D%0A%09pub
@@ -2762,32 +2762,34 @@
 %0D%0A//%09@Override%0D%0A
+//
 %09public boolean 
@@ -2822,16 +2822,18 @@
 rg1) %7B%0D%0A
+//
 %09%09// TOD
@@ -2862,16 +2862,18 @@
 d stub%0D%0A
+//
 %09%09return
@@ -2885,16 +2885,16 @@
 e;%0D%0A
+//
 %09%7D%0D%0A%0D%0A
-//
 %09@Ov
@@ -2893,34 +2893,32 @@
 %0D%0A%0D%0A%09@Override%0D%0A
-//
 %09public int getS
@@ -2945,18 +2945,16 @@
 dex) %7B%0D%0A
-//
 %09%09Choice
@@ -3014,18 +3014,16 @@
 ndex);%0D%0A
-//
 %09%09return
@@ -3107,18 +3107,16 @@
 ep());%0D%0A
-//
 %09%7D%0D%0A%0D%0A%7D%0D

Fix timeline integration