package io.myweb.processor;

import com.google.common.base.Joiner;

import io.myweb.api.*;
import io.myweb.processor.model.ParsedMethod;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;
import java.util.*;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class MywebioAnnotationProcessor extends AbstractProcessor {

	private static final boolean quiet = false;

	private void log(String msg) {
		if (!quiet) {
			processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, msg);
		}
	}

	private void error(String msg) {
		processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg);
	}

	@Override
	public Set<String> getSupportedAnnotationTypes() {
		Set<String> annotationTypes = new HashSet<String>();
		annotationTypes.add(GET.class.getName());
		annotationTypes.add(PUT.class.getName());
		annotationTypes.add(DELETE.class.getName());
		annotationTypes.add(POST.class.getName());
		annotationTypes.add(Produces.class.getName());
		return annotationTypes;
	}

	@Override
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
		log("process: annotations=" + annotations + ", roundEnv=" + roundEnv);
		if (annotations.isEmpty()) {
			return false;
		}

		List<ParsedMethod> parsedMethods = new LinkedList<ParsedMethod>();
		Set<ExecutableElement> processed = new HashSet<ExecutableElement>();
		MywebValidator mywebValidator = new MywebValidator(processingEnv.getMessager());
		MywebParser mywebParser = new MywebParser(processingEnv.getMessager(), mywebValidator);
		MywebCodeGenerator mywebCodeGenerator = new MywebCodeGenerator(processingEnv);

		try {
			for (TypeElement te : annotations) {
				Set<? extends Element> elementsAnnotated = roundEnv.getElementsAnnotatedWith(te);
				Set<ExecutableElement> executableElements = ElementFilter.methodsIn(elementsAnnotated);
				for (ExecutableElement ee : executableElements) {
					log("process: executableElement " + ee.getSimpleName().toString() + " hash=" + ee.hashCode());
					if (!processed.contains(ee)) {
						processed.add(ee);
						log("parameter names: " + Joiner.on(", ").join(ee.getParameters()));
						ParsedMethod parsedMethod = mywebParser.parse(ee);
						parsedMethods.add(parsedMethod);
					}
				}
			}
			mywebCodeGenerator.generateCode(parsedMethods);
		} catch (Exception e) {
			processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "after exception " + e);
//			processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "after exception\n" + Throwables.getStackTraceAsString(e));
		}
		return false;
	}
}

@@ -1226,81 +1226,8 @@
 ) %7B%0A
-%09%09log(%22process: annotations=%22 + annotations + %22, roundEnv=%22 + roundEnv);%0A
 %09%09if
@@ -1274,17 +1274,16 @@
 se;%0A%09%09%7D%0A
-%0A
 %09%09List%3CP
@@ -1668,17 +1668,16 @@
 ngEnv);%0A
-%0A
 %09%09try %7B%0A
@@ -1957,236 +1957,61 @@
 %09%09%09%09
-log(%22process: executableElement %22 + ee.getSimpleName().toString() + %22 hash=%22 + ee.hashCode());%0A%09%09%09%09%09if (!processed.contains(ee)) %7B%0A%09%09%09%09%09%09processed.add(ee);%0A%09%09%09%09%09%09log(%22parameter names: %22 + Joiner.on(%22, %22).join(ee.getParameters())
+if (!processed.contains(ee)) %7B%0A%09%09%09%09%09%09processed.add(ee
 );%0A%09
@@ -2295,136 +2295,8 @@
 e);%0A
-//%09%09%09processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, %22after exception%5Cn%22 + Throwables.getStackTraceAsString(e));%0A
 %09%09%7D%0A

Remove not needed logs