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

import static org.eclipse.core.resources.IResource.DEPTH_ZERO;
import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;

import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;

import com.redhat.ceylon.compiler.java.codegen.CeylonFileObject;

final class CompileErrorReporter implements
		DiagnosticListener<JavaFileObject> {
    
	private IProject project;

    public CompileErrorReporter(IProject project) {
	    this.project = project;
    }

    @Override
	public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
		JavaFileObject source = diagnostic.getSource();
		if (source instanceof CeylonFileObject) {
			IFile file = getWorkspace().getRoot()
					.getFileForLocation(new Path(source.getName()));
			try {
				for (IMarker m: file.findMarkers(CeylonBuilder.PROBLEM_MARKER_ID, false, DEPTH_ZERO)) {
					int sev = ((Integer)m.getAttribute(IMarker.SEVERITY)).intValue();
					if (sev==IMarker.SEVERITY_ERROR) {
						return;
					}
				}
				IMarker marker = file.createMarker(CeylonBuilder.PROBLEM_MARKER_ID+".backend");
				setupMarker(marker, diagnostic);
			} 
			catch (CoreException e) {
				e.printStackTrace();
			}
		}
		else if (source == null) {
		    // no source file
			if (!diagnostic.toString().startsWith("Note: Created module")) {
				try {
					IMarker marker = project.createMarker(CeylonBuilder.PROBLEM_MARKER_ID+".backend");
					setupMarker(marker, diagnostic);
				} 
				catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

    private void setupMarker(IMarker marker, Diagnostic<? extends JavaFileObject> diagnostic) throws CoreException {
        long line = diagnostic.getLineNumber();
        if (line>=0) {
            //Javac doesn't have line number 
            //info for certain errors
            marker.setAttribute(IMarker.LINE_NUMBER, (int)line);
            marker.setAttribute(IMarker.CHAR_START, (int)diagnostic.getStartPosition());
            marker.setAttribute(IMarker.CHAR_END, (int)diagnostic.getEndPosition());
        }
        marker.setAttribute(IMarker.MESSAGE, diagnostic.getMessage(Locale.getDefault()));
        marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
        switch (diagnostic.getKind()) {
        case ERROR:
        	marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
        	break;
        case WARNING:
        case MANDATORY_WARNING:
        	marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
        	break;
        default:
        	marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
        }
    }
}
@@ -527,74 +527,8 @@
 h;%0A%0A
-import com.redhat.ceylon.compiler.java.codegen.CeylonFileObject;%0A%0A
 fina
@@ -879,36 +879,389 @@
 rce 
-instanceof CeylonFileObject)
+== null) %7B%0A%09%09    // no source file%0A%09%09    if (!diagnostic.toString().startsWith(%22Note: Created module%22)) %7B%0A%09%09        try %7B%0A%09%09            IMarker marker = project.createMarker(CeylonBuilder.PROBLEM_MARKER_ID+%22.backend%22);%0A%09%09            setupMarker(marker, diagnostic);%0A%09%09        %7D %0A%09%09        catch (CoreException e) %7B%0A%09%09            e.printStackTrace();%0A%09%09        %7D%0A%09%09    %7D%0A%09%09%7D %0A%09%09else
  %7B%0A%09
@@ -1745,24 +1745,24 @@
 eption e) %7B%0A
+
 %09%09%09%09e.printS
@@ -1787,343 +1787,8 @@
 %09%09%7D%0A
-%09%09else if (source == null) %7B%0A%09%09    // no source file%0A%09%09%09if (!diagnostic.toString().startsWith(%22Note: Created module%22)) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09IMarker marker = project.createMarker(CeylonBuilder.PROBLEM_MARKER_ID+%22.backend%22);%0A%09%09%09%09%09setupMarker(marker, diagnostic);%0A%09%09%09%09%7D %0A%09%09%09%09catch (CoreException e) %7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%0A

fix #560