/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.devkit.inspections.internal;

import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.devkit.inspections.quickfix.ConvertToJBColorConstantQuickFix;
import org.jetbrains.idea.devkit.inspections.quickfix.ConvertToJBColorQuickFix;

import java.awt.*;

/**
 * @author Konstantin Bulenkov
 */
public class UseJBColorInspection extends InternalInspection {
  @Override
  public PsiElementVisitor buildInternalVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitNewExpression(PsiNewExpression expression) {
        final ProblemDescriptor descriptor = checkNewExpression(expression, holder.getManager(), isOnTheFly);
        if (descriptor != null) {
          holder.registerProblem(descriptor);
        }
        super.visitNewExpression(expression);
      }

      @Override
      public void visitReferenceExpression(PsiReferenceExpression expression) {
        super.visitReferenceExpression(expression);
        final PsiElement colorField = expression.resolve();
        if (colorField != null && colorField instanceof PsiField && ((PsiField)colorField).hasModifierProperty(PsiModifier.STATIC)) {
          final PsiClass colorClass = ((PsiField)colorField).getContainingClass();
          if (colorClass != null && Color.class.getName().equals(colorClass.getQualifiedName())) {
            String text = expression.getText();
            if (text.contains(".")) {
              text = text.substring(text.lastIndexOf('.'));
            }
            if (text.startsWith(".")) {
              text = text.substring(1);
            }
            if (text.equalsIgnoreCase("lightGray")) {
              text = "LIGHT_GRAY";
            } else if (text.equalsIgnoreCase("darkGray")) {
              text = "DARK_GRAY";
            }
            final ProblemDescriptor descriptor = holder.getManager()
              .createProblemDescriptor(expression, "Change to JBColor." + text.toUpperCase(), new ConvertToJBColorConstantQuickFix(text.toUpperCase()),
                                       ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly);
            holder.registerProblem(descriptor);
          }
        }
      }
    };
  }

  @Nullable
  private static ProblemDescriptor checkNewExpression(PsiNewExpression expression, InspectionManager manager, boolean isOnTheFly) {
    final Project project = manager.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final PsiClass jbColorClass = facade.findClass(JBColor.class.getName(), GlobalSearchScope.allScope(project));
    final PsiType type = expression.getType();
    if (type != null && jbColorClass != null) {
      if (!facade.getResolveHelper().isAccessible(jbColorClass, expression, jbColorClass)) return null;
      final PsiExpressionList arguments = expression.getArgumentList();
      if (arguments != null) {
        if ("java.awt.Color".equals(type.getCanonicalText())) {
          final PsiElement parent = expression.getParent();
          if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiNewExpression) {
            final PsiType parentType = ((PsiNewExpression)parent.getParent()).getType();
            if (parentType == null || JBColor.class.getName().equals(parentType.getCanonicalText())) return null;
          }
            return manager.createProblemDescriptor(expression, "Replace with JBColor", new ConvertToJBColorQuickFix(),
                                                   ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly);
        }
      }
    }
    return null;
  }

  @Nls
  @NotNull
  @Override
  public String getDisplayName() {
    return "Use Darcula aware JBColor";
  }

  @NotNull
  @Override
  public String getShortName() {
    return "UseJBColor";
  }
}

@@ -2082,24 +2082,207 @@
 xpression);%0A
+        final PsiElement parent = expression.getParent();%0A        if (parent instanceof PsiMethodCallExpression) %7B%0A          //do not resolve method names%0A          return;%0A        %7D%0A
         fina
@@ -3696,24 +3696,227 @@
 tProject();%0A
+    final PsiType type = expression.getType();%0A    final PsiExpressionList arguments = expression.getArgumentList();%0A    if (type != null && arguments != null && type.equalsToText(%22java.awt.Color%22)) %7B%0A  
     final Ja
@@ -3972,16 +3972,18 @@
 oject);%0A
+  
     fina
@@ -4096,75 +4096,14 @@
     
-final PsiType type = expression.getType();%0A    if (type != null && 
+  if (
 jbCo
@@ -4122,23 +4122,12 @@
 null
-) %7B%0A      if (!
+ && 
 faca
@@ -4206,190 +4206,10 @@
 s)) 
-return null;%0A      final PsiExpressionList arguments = expression.getArgumentList();%0A      if (arguments != null) %7B%0A        if (%22java.awt.Color%22.equals(type.getCanonicalText())) %7B%0A  
+%7B%0A
     
@@ -4258,26 +4258,24 @@
 etParent();%0A
-  
         if (
@@ -4355,34 +4355,32 @@
 ewExpression) %7B%0A
-  
           final 
@@ -4456,26 +4456,24 @@
 ;%0A          
-  
 if (parentTy
@@ -4570,31 +4570,26 @@
         
-  %7D%0A   
+%7D%0A
         
  return 
@@ -4576,25 +4576,24 @@
   %7D%0A        
- 
 return manag
@@ -4726,36 +4726,32 @@
                 
-    
 ProblemHighlight
@@ -4794,26 +4794,16 @@
 heFly);%0A
-        %7D%0A
       %7D%0A

use JBColor inspection optimization