package rhogenwizard.builder.rhodes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.UIJob;

import rhogenwizard.Activator;
import rhogenwizard.ConsoleHelper;
import rhogenwizard.PlatformType;
import rhogenwizard.sdk.facade.RhoTaskHolder;
import rhogenwizard.sdk.task.BuildPlatformTask;
import rhogenwizard.sdk.task.CleanAllPlatfromTask;
import rhogenwizard.sdk.task.CompileRubyPartTask;

class SelectPlatformJob extends UIJob 
{
	private PlatformType m_selectPlatform = PlatformType.eUnknown;
	private String       m_workDir = null;
	
	public SelectPlatformJob(String name, String workDir) 
	{
		super(name);
		
		m_workDir = workDir;
	}

	@Override
	public IStatus runInUIThread(IProgressMonitor monitor) 
	{
		Shell windowShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		
		SelectPlatformDialog selectDlg = new SelectPlatformDialog(windowShell);
		m_selectPlatform = selectDlg.open();
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(BuildPlatformTask.workDir, m_workDir);
		params.put(BuildPlatformTask.platformType, m_selectPlatform);

		RhoTaskHolder.getInstance().runTask(BuildPlatformTask.class, params);

		return new Status(BUILD, Activator.PLUGIN_ID, "select platform");
	}
	
	public PlatformType getSelectedPlatform()
	{
		return m_selectPlatform;
	}
}

public class Builder extends IncrementalProjectBuilder 
{
	public  static final String BUILDER_ID = "com.rhomobile.rhostudio.rhogenBuilder";

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.internal.events.InternalBuilder#build(int,
	 *      java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected IProject[] build(int kind, Map args, final IProgressMonitor monitor) throws CoreException 
	{ 
		fullBuild(monitor);
		
//		try 
//		{
//			SelectPlatformJob buildJob = new SelectPlatformJob("select platform", getProject().getLocation().toOSString());
//			buildJob.run(monitor);
//			buildJob.join();
//		} 
//		catch (InterruptedException e) 
//		{
//			e.printStackTrace();
//		}
		
		return null;
	}

	@Override
	protected void clean(IProgressMonitor monitor) throws CoreException 
	{
		try 
		{
			ConsoleHelper.consoleBuildPrint("Clean project started");

			Map<String, Object> params = new HashMap<String, Object>();
			params.put(CleanAllPlatfromTask.workDir, getProject().getLocation().toOSString());

			RhoTaskHolder.getInstance().runTask(CleanAllPlatfromTask.class, params);
			
			ConsoleHelper.consoleBuildPrint("Clean application cancelled");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}

		super.clean(monitor);
	}

	protected void fullBuild(final IProgressMonitor monitor) throws CoreException 
	{
		try 
		{
			List<String> out = compileRubyPart();
			getProject().accept(new ResourceVisitor(out));
		} 
		catch (CoreException e) 
		{
			e.printStackTrace();
		}
	}

	protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException 
	{
		// the visitor does the work.
		delta.accept(new DeltaVisitor());
	}
	
	private List<String> compileRubyPart()
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(CompileRubyPartTask.workDir, getProject().getLocation().toOSString());
		params.put(CompileRubyPartTask.platformType, PlatformType.eWm);
		
		Map<String, ?> res = RhoTaskHolder.getInstance().runTask(CompileRubyPartTask.class, params);
		
		List<String> errLines = (List<String>) res.get(CompileRubyPartTask.outStrings);

		return errLines;
	}
}

@@ -1367,24 +1367,145 @@
 .open();%0A%09%09%0A
+%09%09if (m_selectPlatform == PlatformType.eUnknown)%0A%09%09%09return new Status(NONE, Activator.PLUGIN_ID, %22select platform%22);%0A%09%09%09%0A
 %09%09Map%3CString
@@ -1546,32 +1546,32 @@
 ing, Object%3E();%0A
-
 %09%09params.put(Bui
@@ -2038,16 +2038,53 @@
 lder%22;%0A%0A
+%09public Builder()%0A%09%7B%0A%09%09super();%0A%09%7D%0A%09%0A
 %09/*%0A%09 * 
@@ -2347,17 +2347,16 @@
 tion %0A%09%7B
- 
 %0A%09%09fullB
@@ -2377,26 +2377,8 @@
 %0A%09%09%0A
-//%09%09try %0A//%09%09%7B%0A//%09
 %09%09Se
@@ -2487,19 +2487,16 @@
 ing());%0A
-//%09
 %09%09buildJ
@@ -2516,111 +2516,8 @@
 r);%0A
-//%09%09%09buildJob.join();%0A//%09%09%7D %0A//%09%09catch (InterruptedException e) %0A//%09%09%7B%0A//%09%09%09e.printStackTrace();%0A//%09%09%7D%0A
 %09%09%0A%09
@@ -3334,187 +3334,8 @@
 %09%7D%0A%0A
-%09protected void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) throws CoreException %0A%09%7B%0A%09%09// the visitor does the work.%0A%09%09delta.accept(new DeltaVisitor());%0A%09%7D%0A%09%0A
 %09pri

fix rebuild