/*******************************************************************************
 * 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 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.window.Window;
import org.eclipse.ui.handlers.HandlerUtil;

import com.irtsaintexupery.fiacre.simulator.perspective.ViewsController;
import com.irtsaintexupery.fiacre.simulator.staticjson.extractor.DynamicJsonExtractor;

import fr.laas.libtts.LibTTS;
import tts.Error;
import tts.InvalidDelay;
import tts.StateNotFound;

public class DelayEvent extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ReadDelay dialog = new ReadDelay(HandlerUtil.getActiveShell(event));
		dialog.create();
		if (dialog.open() == Window.OK) {
		  dialog.getValue();
			LibTTS libtts = FiacreSimulator.getLibTTS();
			DynamicJsonExtractor dje = FiacreSimulator.sendDje();
			try {
				libtts.getTTSSimulator().delay(dialog.getValue());
				dje.extractExecutableStatements();
				dje.saveInstanceResource();
				
				
				//Jorge Gutierrez 03/08/2016
				ViewsController.INSTANCE.updateTransitionView();
				ViewsController.INSTANCE.addDelayToAction(dialog.getValue());
				ViewsController.INSTANCE.updateExecutedActionView();
				
				
			} catch (Error e) {
				// TODO Auto-generated catch block
				Utils.showException("Error",e);
				//e.printStackTrace();
			} catch (StateNotFound e) {
				// TODO Auto-generated catch block
				Utils.showException("StateNotFound",e);
				//e.printStackTrace();
			} catch (InvalidDelay e) {
				// TODO Auto-generated catch block
				Utils.showException("InvalidDelay",e);
				//e.printStackTrace();
			} catch (TException e) {
				// TODO Auto-generated catch block
				Utils.showException("TException",e);
				//e.printStackTrace();
			} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			Utils.showException("NumberFormatException: "+dialog.getValue()+" is not numeric value",e);
			//e.printStackTrace();
		}
		} 
		return null;
	}

}
