package pl.zalewski.mikolaj.symbdev.actions;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianCommandHelper;

/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class StartConsoleAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	private IProject currentProject=null;
	
	/**
	 * The constructor.
	 */
	public StartConsoleAction() {
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
	    if (currentProject==null)
	    {
	        MessageDialog.openError(window.getShell(), SymbdevPlugin.MESSAGE_TITLE,
	                "Internal error: couldn't retrieve current project.");
	        return;
	    }
	    
	    
	    String workDir=SymbianCommandHelper.getWorkingDirectory(currentProject, window.getShell(), true);
	    if (workDir==null) return;
	    String[] envTab;
	    try {	        
	        envTab=SymbianCommandHelper.buildEnvironment(currentProject);
	    }
	    catch (CoreException e)
	    {
	        ErrorDialog.openError(window.getShell(), SymbdevPlugin.MESSAGE_TITLE,
	                "Couln't setup shell environment", e.getStatus());
	        return;
	    }
	    
		String[] cmd=new String[4];
		
		cmd[0]=SymbianCommandHelper.getOSShell();
		cmd[1]="/c";
		cmd[2]="start";
		cmd[3]=SymbianCommandHelper.getOSShell();
		
		try
		{
			Runtime.getRuntime().exec(cmd, envTab, new File(workDir));
		}
		catch (IOException e)
		{
		    MessageDialog.openError(
					window.getShell(),
					SymbdevPlugin.MESSAGE_TITLE,
					"Couldn't start command shell: "+e.getLocalizedMessage());
			e.printStackTrace();
		}	    
	}

	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection)selection;
			Iterator it=sel.iterator();
			while (it.hasNext())
			{
			    Object obj = it.next();
				if (obj instanceof IResource) {
			    	currentProject=((IResource)obj).getProject();
			    	return;
				}
				if (obj instanceof ICElement) {
			    	currentProject=((ICElement)obj).getCProject().getProject();
			    	return;
				}
			}
		}
		currentProject=null;
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}