package org.coursera.courier;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.psi.PsiElement;
import org.coursera.courier.psi.CourierEnumSymbol;
import org.coursera.courier.psi.CourierFieldName;
import org.coursera.courier.psi.CourierPropNameDeclaration;
import org.coursera.courier.psi.CourierTypeNameDeclaration;
import org.coursera.courier.psi.CourierTypeReference;
import org.coursera.courier.psi.CourierVisitor;
import org.jetbrains.annotations.NotNull;

public class CourierAnnotator implements Annotator {
  @Override
  public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
    element.accept(new CourierVisitor() {
      @Override
      public void visitFieldName(@NotNull CourierFieldName o) {
        super.visitFieldName(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.FIELD);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitTypeNameDeclaration(@NotNull CourierTypeNameDeclaration o) {
        super.visitTypeNameDeclaration(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.TYPE_NAME);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitTypeReference(@NotNull CourierTypeReference o) {
        super.visitTypeReference(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.TYPE_REFERENCE);
        if (!o.getFullname().isPrimitive()) {
          if (o.getReference() == null) {
            holder.createErrorAnnotation(o, "Cannot resolve symbol '" + o.getText() + "'");
          }
        }
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitPropNameDeclaration(@NotNull CourierPropNameDeclaration o) {
        super.visitPropNameDeclaration(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.PROPERTY);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitEnumSymbol(@NotNull CourierEnumSymbol o) {
        super.visitEnumSymbol(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.FIELD);
      }
    });
  }

  private static void setHighlighting(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
                                      @NotNull TextAttributesKey key) {
    holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
    holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(
        EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key));
  }
}

@@ -24,16 +24,79 @@
 urier;%0A%0A
+import com.intellij.codeInsight.daemon.impl.HighlightInfoType;%0A
 import c
@@ -507,16 +507,74 @@
 ldName;%0A
+import org.coursera.courier.psi.CourierImportDeclaration;%0A
 import o
@@ -2483,16 +2483,571 @@
     %7D);%0A
+    element.accept(new CourierVisitor() %7B%0A      @Override%0A      public void visitImportDeclaration(@NotNull CourierImportDeclaration o) %7B%0A        super.visitImportDeclaration(o);%0A        boolean found = false;%0A        for (CourierTypeReference ref: o.getCourierFile().getTypeReferences()) %7B%0A          if (ref.getFullname().equals(o.getFullname())) %7B%0A            found = true;%0A            break;%0A          %7D%0A        %7D%0A        if (!found) %7B%0A          setHighlighting(o, holder, HighlightInfoType.UNUSED_SYMBOL.getAttributesKey());%0A        %7D%0A      %7D%0A    %7D);%0A
   %7D%0A%0A  p

Add unused import detection to Courier IntelliJ plugin.