/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;


public class MarkerFactory {
	
	//Marker ID
	public static final String MARKER = "com.irtsaintexupery.fiacre.simulator.marking.marker";
	
	//Annotation ID
	public static final String ANNOTATION = "com.irtsaintexupery.fiacre.simulator.marking.annotation";
	
	/*
	 * Creates a Marker
	 */
	public static IMarker createMarker(IResource res, Position position)
    throws CoreException {
            IMarker marker = null;
            //note: you use the id that is defined in your plugin.xml
            marker = res.createMarker(MARKER);
            marker.setAttribute(IMarker.MESSAGE, "My Marker");
            //compute and set char start and char end
            int start = position.getOffset();
            int end = position.getOffset() + position.getLength();
            marker.setAttribute(IMarker.CHAR_START, start);
            marker.setAttribute(IMarker.CHAR_END, end);
            return marker;
    }

	/*
	 * returns a list of a resources markers
	 */
	public static List<IMarker> findMarkers(IResource resource) {
	     try {
	         return Arrays.asList(resource.findMarkers(MARKER, true, IResource.DEPTH_ZERO));
	     } catch (CoreException e) {
	         return new ArrayList<IMarker>();
	    }
	}
	
	/*
	 * Returns a list of markers that are linked to the resource or any sub resource of the resource
	 */
	public static List<IMarker> findAllMarkers(IResource  resource) {
        try {
            return Arrays.asList(resource.findMarkers(MARKER, true, IResource.DEPTH_INFINITE));
        } catch (CoreException e) {
            return new ArrayList<IMarker>();
        }
    }
	
	/*
	 * Returns the selection of the package explorer
	 */
	public static TreeSelection getTreeSelection() {

		ISelection selection = MarkerPluginActivator.getActiveWorkbenchWindow().getSelectionService().getSelection();
		if (selection instanceof TreeSelection) {
			return (TreeSelection)selection;
		}
		return null;
	}

	/*
	 * Returns the selection of the package explorer
	 */
	public static TextSelection getTextSelection() {

		ISelection selection = MarkerPluginActivator.getActiveWorkbenchWindow().getSelectionService().getSelection();
		if (selection instanceof TextSelection) {
			return (TextSelection)selection;
		}
		return null;
	}
	
//	public static void addAnnotation(IMarker marker, ITextSelection selection, ITextEditor editor) {
//	      //The DocumentProvider enables to get the document currently loaded in the editor
//	      IDocumentProvider idp = editor.getDocumentProvider();
//
//	      //This is the document we want to connect to. This is taken from the current editor input.
//	      IDocument document = idp.getDocument(editor.getEditorInput());
//
//	      //The IannotationModel enables to add/remove/change annoatation to a Document loaded in an Editor
//	      IAnnotationModel iamf = idp.getAnnotationModel(editor.getEditorInput());
//
//	      //Note: The annotation type id specify that you want to create one of your annotations
//	      SimpleMarkerAnnotation ma = new SimpleMarkerAnnotation(ANNOTATION, marker);
//
//	      //Finally add the new annotation to the model
//	      iamf.connect(document);
//	      iamf.addAnnotation(ma,new Position(selection.getOffset(),selection.getLength()));
//	      iamf.disconnect(document);
//	}
	
}
