/*******************************************************************************
 * 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.marking;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.text.Position;
import org.eclipse.xtext.nodemodel.impl.CompositeNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.resource.XtextResourceSetProvider;

import com.google.inject.Injector;
import com.irtsaintexupery.fiacre.instance.fiacreinstance.InstanceStatementBlock;
import com.irtsaintexupery.fiacre.instance.fiacreinstance.StatementBlock;

import fr.irit.fiacre.xtext.fiacre.Statement;
import fr.irit.fiacre.xtext.ui.internal.XtextActivator;

public class MarkingController {
	// added selection attribute
	public static IFile file;
	
	protected static XtextResource getFiacreXtextResource(IFile file) {
		Injector injector = XtextActivator.getInstance().getInjector("fr.irit"
				+ ".fiacre.xtext.Fiacre");
		XtextResourceSetProvider provider = injector
				.getInstance(XtextResourceSetProvider.class);
		ResourceSet resourceSet = provider.get(file.getProject());

		URI xtextURI = URI.createPlatformResourceURI(file
				.getFullPath().toString(), true);

		return (XtextResource) resourceSet.getResource(xtextURI, true);
	}
	
	// Jorge Gutierrez 05/08/2016
		public static void highlightCode(StatementBlock statementblock) {
			
			for (InstanceStatementBlock isb : statementblock
					.getInstanceStatementBlocks()) {
				CompositeNode tr_source_cn = (CompositeNode) NodeModelUtils
						.findActualNodeFor(isb.getTransitionSource());
				try {
					IMarker tr_src_marker = MarkerFactory.createMarker(file,
							new Position(tr_source_cn.getOffset(), tr_source_cn
									.getLength()));
				} catch (CoreException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for (Statement s : isb.getStatements()) {
					CompositeNode s_cn = (CompositeNode) NodeModelUtils
							.findActualNodeFor(s);
					try {
						IMarker smarker = MarkerFactory.createMarker(file,
								new Position(s_cn.getOffset(), s_cn.getLength()));
					} catch (CoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
		//Jorge Gutierrez 08/08/2016
		public static void deleteMarkers (IFile file){
			try {
//				int markers = MarkerFactory.findMarkers(file).size();
//				System.out.println("#marks before " + markers);
//				System.out.println("file " + file);
			    file.deleteMarkers("com.irtsaintexupery.fiacre.simulator"
			    		+ ".marking.marker", true, 0);
//			    markers = MarkerFactory.findMarkers(file).size();
//			    System.out.println("#marks after " + markers);	
			    
			} catch (CoreException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
			}
		}

}
