/*
 * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
 *
 * 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.controlflow;

import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.regex.Pattern;

public class FallthruInSwitchStatementInspection extends BaseInspection {

  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message("fallthru.in.switch.statement.display.name");
  }

  @NotNull
  public String getID() {
    return "fallthrough";
  }

  @NotNull
  protected String buildErrorString(Object... infos) {
    return InspectionGadgetsBundle.message("fallthru.in.switch.statement.problem.descriptor");
  }

  @Nullable
  protected InspectionGadgetsFix buildFix(Object... infos) {
    return new FallthruInSwitchStatementFix();
  }

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

  private static class FallthruInSwitchStatementFix extends InspectionGadgetsFix {

    @NotNull
    public String getName() {
      return InspectionGadgetsBundle.message("fallthru.in.switch.statement.quickfix");
    }

    protected void doFix(Project project, ProblemDescriptor descriptor) {
      final PsiSwitchLabelStatement labelStatement = (PsiSwitchLabelStatement)descriptor.getPsiElement();
      final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
      final PsiElementFactory factory = psiFacade.getElementFactory();
      final PsiStatement breakStatement = factory.createStatementFromText("break;", labelStatement);
      final PsiElement parent = labelStatement.getParent();
      parent.addBefore(breakStatement, labelStatement);
    }
  }

  private static class FallthroughInSwitchStatementVisitor extends BaseInspectionVisitor {

    private final Pattern commentPattern = Pattern.compile("(?i)falls?\\s*thro?u");

    @Override
    public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) {
      super.visitSwitchStatement(statement);
      final PsiCodeBlock body = statement.getBody();
      if (body == null) {
        return;
      }
      boolean switchLabelValid = true;
      final PsiStatement[] statements = body.getStatements();
      for (final PsiStatement child : statements) {
        if (child instanceof PsiSwitchLabelStatement) {
          if (switchLabelValid) {
            continue;
          }
          final PsiElement previousSibling = PsiTreeUtil.skipSiblingsBackward(child, PsiWhiteSpace.class);
          if (previousSibling instanceof PsiComment) {
            final PsiComment comment = (PsiComment)previousSibling;
            final String commentText = comment.getText();
            if (commentPattern.matcher(commentText).find()) {
              continue;
            }
          }
          registerError(child);
          //switchLabelValid = true;
        }
        else {
          switchLabelValid = !ControlFlowUtils.statementMayCompleteNormally(child);
        }
      }
    }
  }
}
@@ -2841,16 +2841,23 @@
 private 
+static 
 final Pa
@@ -3165,47 +3165,8 @@
   %7D%0A
-      boolean switchLabelValid = true;%0A
     
@@ -3234,16 +3234,65 @@
    for (
+int i = 1; i %3C statements.length; i++) %7B%0A        
 final Ps
@@ -3308,17 +3308,17 @@
 t child 
-:
+=
  stateme
@@ -3320,19 +3320,20 @@
 atements
-) %7B
+%5Bi%5D;
 %0A       
@@ -3336,16 +3336,18 @@
      if 
+(!
 (child i
@@ -3384,47 +3384,12 @@
 ent)
- %7B%0A          if (switchLabelValid
 ) %7B%0A
-  
     
@@ -3404,38 +3404,34 @@
 ntinue;%0A        
-  %7D%0A  
+%7D%0A
         final Ps
@@ -3523,26 +3523,24 @@
 s);%0A        
-  
 if (previous
@@ -3568,26 +3568,24 @@
 iComment) %7B%0A
-  
           fi
@@ -3644,26 +3644,24 @@
 ;%0A          
- 
- 
 final String
@@ -3690,26 +3690,24 @@
 .getText();%0A
-  
           if
@@ -3750,26 +3750,24 @@
 ).find()) %7B%0A
-  
             
@@ -3778,38 +3778,34 @@
 inue;%0A          
-  %7D%0A  
+%7D%0A
         %7D%0A      
@@ -3810,169 +3810,211 @@
     
-  registerError(child);%0A          //switchLabelValid = true;%0A        %7D%0A        else %7B%0A          switchLabelValid = !ControlFlowUtils.statementMayCompleteNormally
+final PsiStatement previousStatement = PsiTreeUtil.getPrevSiblingOfType(child, PsiStatement.class);%0A        if (ControlFlowUtils.statementMayCompleteNormally(previousStatement)) %7B%0A          registerError
 (chi

simplify code a little