package com.redhat.ceylon.eclipse.core.classpath;

import static com.redhat.ceylon.eclipse.core.builder.CeylonBuilder.isModelTypeChecked;
import static org.eclipse.core.resources.IncrementalProjectBuilder.INCREMENTAL_BUILD;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IProject;
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.core.runtime.jobs.Job;
import org.eclipse.jdt.internal.debug.ui.actions.ForceReturnAction;

public class BuildProjectAfterClasspathChangeJob extends Job {
	protected final IProject project;
    private boolean buildReferencedProjects;
    private boolean buildReferencingProjects;
    private boolean forceRebuild;

	public BuildProjectAfterClasspathChangeJob(String name, IProject project, boolean buildReferencedProjects, boolean buildReferencingProjects, boolean forceRebuild) {
		super(name);
		this.project = project;
		this.buildReferencedProjects = buildReferencedProjects;
        this.buildReferencingProjects = buildReferencingProjects;
        this.forceRebuild = forceRebuild;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		if (forceRebuild || ! isModelTypeChecked(project)) {
			try {
				List<IBuildConfiguration> configs = new ArrayList<IBuildConfiguration>();
				configs.add(project.getBuildConfig(IBuildConfiguration.DEFAULT_CONFIG_NAME));
				if (buildReferencingProjects) {
	                for (IProject p: project.getReferencingProjects()) {
	                    if (p.isOpen()) {
	                        configs.add(p.getBuildConfig(IBuildConfiguration.DEFAULT_CONFIG_NAME));
	                    }
	                }
				}
				project.getWorkspace().build(configs.toArray(new IBuildConfiguration[1]), 
						INCREMENTAL_BUILD, buildReferencedProjects, monitor);    	            			
			}
			catch (CoreException e) {
				e.printStackTrace();
			}
		}
		else {
		    // System.out.println("Don't build the project " + project + " since it's already typechecked");
		}
		return Status.OK_STATUS;
	}

	
	
    @Override
    public boolean belongsTo(Object family) {
        if (family instanceof BuildProjectAfterClasspathChangeJob) {
            BuildProjectAfterClasspathChangeJob otherJob = (BuildProjectAfterClasspathChangeJob) family;
            return project.equals(otherJob.project) 
                    && buildReferencedProjects == otherJob.buildReferencedProjects
                    && buildReferencingProjects == otherJob.buildReferencingProjects
                    && forceRebuild == otherJob.forceRebuild;
        }
        return false;
    }

    @Override
    public boolean shouldRun() {
        if (forceRebuild || ! isModelTypeChecked(project)) {
            return true;
        } else {
            // System.out.println("Don't build the project " + project + " since it's already typechecked");
            return false;
        }
    }

    @Override
    public boolean shouldSchedule() {
        if (forceRebuild || ! isModelTypeChecked(project)) {
            return true;
        } else {
            // System.out.println("Don't build the project " + project + " since it's already typechecked");
            return false;
        }
    }
}
@@ -592,76 +592,8 @@
 Job;
-%0Aimport org.eclipse.jdt.internal.debug.ui.actions.ForceReturnAction;
 %0A%0Apu
@@ -1257,16 +1257,37 @@
 ) %7B%0A%09%09if
+ (project.isOpen() &&
  (forceR
@@ -1289,33 +1289,32 @@
 orceRebuild %7C%7C !
- 
 isModelTypeCheck
@@ -1325,16 +1325,17 @@
 roject))
+)
  %7B%0A%09%09%09tr

handle case where project is closed during initialization