/*
 * Copyright 2003-2005 Dave Griffith
 *
 * 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 com.siyeh.ig.classmetrics;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.MoveAnonymousToInnerClassFix;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NotNull;

public class AnonymousClassComplexityInspection
        extends ClassMetricInspection {
    public String getID(){
        return "OverlyComplexAnonymousInnerClass";
    }
    private static final int DEFAULT_COMPLEXITY_LIMIT = 3;
    private final MoveAnonymousToInnerClassFix fix = new MoveAnonymousToInnerClassFix();

    public String getDisplayName() {
        return InspectionGadgetsBundle.message("overly.complex.anonymous.inner.class.display.name");
    }

    public String getGroupDisplayName() {
        return GroupNames.CLASSMETRICS_GROUP_NAME;
    }

    protected int getDefaultLimit() {
        return DEFAULT_COMPLEXITY_LIMIT;
    }

    protected String getConfigurationLabel() {
        return InspectionGadgetsBundle.message("cyclomatic.complexity.limit.option");
    }

    protected InspectionGadgetsFix buildFix(PsiElement location) {
        return fix;
    }

    protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
        return true;
    }

    public String buildErrorString(PsiElement location) {
        final PsiClass aClass = (PsiClass) location.getParent();
        final int totalComplexity = calculateTotalComplexity(aClass);
        return InspectionGadgetsBundle.message("overly.complex.anonymous.inner.class.problem.descriptor", totalComplexity);
    }

    public BaseInspectionVisitor buildVisitor() {
        return new ClassComplexityVisitor();
    }

    private class ClassComplexityVisitor extends BaseInspectionVisitor {

        public void visitClass(@NotNull PsiClass psiClass) {
            // no call to super, to prevent double counting
        }

        public void visitAnonymousClass(@NotNull PsiAnonymousClass aClass) {
            final int totalComplexity = calculateTotalComplexity(aClass);
            if (totalComplexity <= getLimit()) {
                return;
            }
            final PsiJavaCodeReferenceElement classNameIdentifier =
                    aClass.getBaseClassReference();
            registerError(classNameIdentifier);
        }


    }

    private static int calculateTotalComplexity(PsiClass aClass) {
        final PsiMethod[] methods = aClass.getMethods();
        int totalComplexity = calculateComplexityForMethods(methods);
        totalComplexity += calculateInitializerComplexity(aClass);
        return totalComplexity;
    }

    private static int calculateInitializerComplexity(PsiClass aClass) {
        final ComplexityVisitor visitor = new ComplexityVisitor();
        int complexity = 0;
        final PsiClassInitializer[] initializers = aClass.getInitializers();
        for(final PsiClassInitializer initializer : initializers){
            visitor.reset();
            initializer.accept(visitor);
            complexity += visitor.getComplexity();
        }
        return complexity;
    }

    private static int calculateComplexityForMethods(PsiMethod[] methods) {
        final ComplexityVisitor visitor = new ComplexityVisitor();
        int complexity = 0;
        for(final PsiMethod method : methods){
            visitor.reset();
            method.accept(visitor);
            complexity += visitor.getComplexity();
        }
        return complexity;
    }

}

@@ -1019,16 +1019,17 @@
 ction %7B%0A
+%0A
     publ
@@ -1045,16 +1045,17 @@
  getID()
+ 
 %7B%0A      
@@ -1216,16 +1216,28 @@
 ix fix =
+%0A           
  new Mov
@@ -1342,32 +1342,49 @@
 sBundle.message(
+%0A                
 %22overly.complex.
@@ -1708,16 +1708,33 @@
 message(
+%0A                
 %22cycloma
@@ -2196,16 +2196,33 @@
 message(
+%0A                
 %22overly.
@@ -2271,16 +2271,32 @@
 riptor%22,
+%0A               
  totalCo
@@ -2875,136 +2875,16 @@
     
-final PsiJavaCodeReferenceElement classNameIdentifier =%0A                    aClass.getBaseClassReference();%0A            
 register
 Erro
@@ -2883,33 +2883,25 @@
 ster
+Class
 Error(
-c
+aC
 lass
-NameIdentifier
 );%0A 
@@ -2909,18 +2909,16 @@
       %7D%0A
-%0A%0A
     %7D%0A%0A 
@@ -2916,32 +2916,24 @@
 %0A    %7D%0A%0A    
-private 
 static int c
@@ -2967,32 +2967,94 @@
 Class aClass) %7B%0A
+        if (aClass == null) %7B%0A            return 0;%0A        %7D%0A
         final Ps
@@ -4128,7 +4128,5 @@
   %7D%0A
-%0A%7D%0A
+%7D

fix for Tracker Request # 57297