package com.irtsaintexupery.fiacre.simulator.msc.generator.popup.actions;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.irtsaintexupery.fiacre.simulator.msc.generator.Activator;
import com.irtsaintexupery.fiacre.simulator.msc.msc.MSC;


public class GenerateMSC implements IObjectActionDelegate {

	private Shell shell;
	private ISelection selection;
	/**
	 * Constructor for Action1.
	 */
	public GenerateMSC() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
    public void run(IAction action) {
        // Créer un objet resourceSetImpl qui contiendra une ressource EMF (notre modèle)
        ResourceSet resSet = new ResourceSetImpl();

        // Charger la ressource (notre modèle)
        URI modelURI = URI.createURI(getFile().getLocationURI().toString());
        Resource resource = resSet.getResource(modelURI, true);
       
        // Récupérer le premier élément du modèle (élément racine)
        MSC msc = (MSC) resource.getContents().get(0);
        //System.out.println("process"+ msc);
       MSCGenerator mscgen= new MSCGenerator(msc);
       
        try{
            PrintWriter writer = new PrintWriter(getFile().getLocation().toString()+".msc", "UTF-8");
            writer.println(mscgen.generate());
            writer.close();
        } catch (IOException e) {
           // do something
        }
        GeneratePng(getFile());
        try {
			getFile().getParent().refreshLocal(1, null);
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    // added getFile() method
    private IFile getFile() {
        return (IFile) ((IStructuredSelection) selection).getFirstElement();
    }
   
    /**
     * @see IActionDelegate#selectionChanged(IAction, ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
        // Don't forget this line.
        // It is the most important line.
        this.selection = selection;
    }
	protected static void GeneratePng(IFile file) {
		try {
			ProcessBuilder pb_make = new ProcessBuilder(Activator.MSCGEN_PATH,"-T",  "png", "-o", file.getLocation().toString()+".png", 
					file.getLocation().toString()+".msc");

			
			pb_make.directory(new File(file.getParent().getLocation().toString()));
			Process p = pb_make.start();
			p.waitFor();
			//System.out.println("Generate MSC chronogram");
		} catch (IOException e) {
			e.printStackTrace();

		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	}
}

