/*******************************************************************************
 * Copyright (c) 2015, 2016 CNRS, IRIT, IRT AESE (IRT Saint Exupéry).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the LGPL 3.0
 * which accompanies this distribution, and is available at
 * https://www.gnu.org/licenses/lgpl-3.0-standalone.html
 *
 * Contributors:
 *     Faiez Zalila - initial API and implementation and/or initial documentation
 *     Jorge Enrique Gutierrez Zuluaga
 *******************************************************************************/
package com.irtsaintexupery.fiacre.simulator.core;

import java.util.Map;

import org.apache.thrift.TException;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import com.irtsaintexupery.fiacre.instance.fiacreinstance.FireableStatementBlock;
import com.irtsaintexupery.fiacre.simulator.staticjson.extractor.DynamicJsonExtractor;

import fr.laas.libtts.LibTTS;
import tts.Error;
import tts.NotFireable;
import tts.StateNotFound;
import tts.Transition;
import tts.TransitionNotFound;

public class FireEvent extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		if (selection != null & selection instanceof IStructuredSelection) {
			
			try {
				IStructuredSelection strucSelection = (IStructuredSelection) selection;
				String tr_name = ((FireableStatementBlock) strucSelection.toList().get(0)).getName();
				LibTTS libtts = FiacreSimulator.getLibTTS();
				DynamicJsonExtractor dje = FiacreSimulator.sendDje();
				long tr_long = -1;
				for (Map.Entry<Long, Transition> tr : libtts.getTTSSimulator().getAllTransitions().entrySet()) {
					if (tr.getValue().getName().equals(tr_name)) {
						tr_long = tr.getKey();
					}
				}
				libtts.getTTSSimulator().fireTransition(tr_long);
				dje.extractExecutedStatements(libtts.getTTSSimulator().getAllTransitions().get(tr_long).name);
				dje.extractStates();
				dje.extractExecutableStatements();
				dje.extractValues();
				dje.saveInstanceResource();
			} catch (Error e) {
				Utils.showException("Error", e);
				// e.printStackTrace();
			} catch (StateNotFound e) {
				Utils.showException("StateNotFound", e);
				// e.printStackTrace();
			} catch (TransitionNotFound e) {
				Utils.showException("TransitionNotFound", e);
				// e.printStackTrace();
			} catch (NotFireable e) {
				Utils.showException("NotFireable", e);
				// e.printStackTrace();
			} catch (TException e) {
				Utils.showException("TException", e);
				// e.printStackTrace();
			} catch (NullPointerException e) {
				Utils.showException("NullPointerException", e);
				// e.printStackTrace();
			}

		}
		return null;
	}
}
