/*
 * Copyright 2003-2006 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.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.ProblemDescriptor;
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.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.StatementInspection;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import com.siyeh.ig.psiutils.EquivalenceChecker;
import org.jetbrains.annotations.NotNull;

public class IfStatementWithIdenticalBranchesInspection
        extends StatementInspection{

    public String getDisplayName(){
        return InspectionGadgetsBundle.message(
                "if.statement.with.identical.branches.display.name");
    }

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

    @NotNull
    public String buildErrorString(Object... infos){
        return InspectionGadgetsBundle.message(
                "if.statement.with.identical.branches.problem.descriptor");
    }

    public InspectionGadgetsFix buildFix(PsiElement location){
        return new CollapseIfFix();
    }

    private static class CollapseIfFix extends InspectionGadgetsFix{

        @NotNull
        public String getName(){
            return InspectionGadgetsBundle.message(
                    "if.statement.with.identical.branches.collapse.quickfix");
        }

        public void doFix(Project project, ProblemDescriptor descriptor)
                throws IncorrectOperationException{
            final PsiElement identifier = descriptor.getPsiElement();
            final PsiIfStatement statement =
                    (PsiIfStatement) identifier.getParent();
            assert statement != null;
            final PsiStatement thenBranch = statement.getThenBranch();
            if(thenBranch == null) {
                return;
            }
            final PsiStatement elseBranch = statement.getElseBranch();
            if (elseBranch == null) {
                statement.delete();
                return;
            }
            if (thenBranch instanceof PsiBlockStatement) {
                final PsiBlockStatement blockStatement =
                        (PsiBlockStatement) thenBranch;
                final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
                final PsiStatement[] statements = codeBlock.getStatements();
                final PsiElement parent = statement.getParent();
                parent.addRangeBefore(statements[0],
                        statements[statements.length -1], statement);
                statement.delete();
            } else {
                final String bodyText = thenBranch.getText();
                replaceStatement(statement, bodyText);
            }
        }
    }

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

    private static class IfStatementWithIdenticalBranchesVisitor
            extends BaseInspectionVisitor{

        public void visitIfStatement(@NotNull PsiIfStatement ifStatement){
            super.visitIfStatement(ifStatement);
            final PsiStatement thenBranch = ifStatement.getThenBranch();
            final PsiStatement elseBranch = ifStatement.getElseBranch();
            if(thenBranch == null) {
                return;
            }
            if (elseBranch == null) {
                checkIfStatementWithoutElseBranch(ifStatement);
            } else if (EquivalenceChecker.statementsAreEquivalent(
                    thenBranch, elseBranch)) {
                registerStatementError(ifStatement);
            }
        }

        private void checkIfStatementWithoutElseBranch(
                PsiIfStatement ifStatement) {
            final PsiStatement thenBranch = ifStatement.getThenBranch();
            if (ControlFlowUtils.statementMayCompleteNormally(
                    thenBranch)) {
                return;
            }
            PsiStatement nextStatement =
                    PsiTreeUtil.getNextSiblingOfType(ifStatement,
                            PsiStatement.class);
            if (thenBranch instanceof PsiBlockStatement) {
                final PsiBlockStatement blockStatement =
                        (PsiBlockStatement) thenBranch;
                final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
                final PsiStatement[] statements = codeBlock.getStatements();
                final PsiStatement lastStatement =
                        statements[statements.length - 1];
                for (PsiStatement statement : statements) {
                    if (nextStatement == null) {
                        if (statement == lastStatement &&
                                statement instanceof PsiReturnStatement) {
                            final PsiReturnStatement returnStatement =
                                    (PsiReturnStatement) statement;
                            if (returnStatement.getReturnValue() == null) {
                                registerStatementError(ifStatement);
                            }
                        }
                        return;
                    } else if (!EquivalenceChecker.statementsAreEquivalent(
                            statement, nextStatement)) {
                        return;
                    }
                    nextStatement =
                            PsiTreeUtil.getNextSiblingOfType(nextStatement,
                                    PsiStatement.class);
                }
            } else if (!EquivalenceChecker.statementsAreEquivalent(
                    thenBranch, nextStatement)) {
                return;
            }
            registerStatementError(ifStatement);
        }
    }
}
@@ -1221,16 +1221,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Apublic
@@ -2211,16 +2211,25 @@
 d doFix(
+@NotNull 
 Project 
@@ -2236,16 +2236,42 @@
 project,
+%0A                         
  Problem
@@ -4829,106 +4829,28 @@
 nt =
-%0A                    PsiTreeUtil.getNextSiblingOfType(ifStatement,%0A                            Psi
+ getNextStatement(if
 Stat
@@ -4846,38 +4846,32 @@
 ment(ifStatement
-.class
 );%0A            i
@@ -6134,124 +6134,30 @@
 nt =
-%0A                            PsiTreeUtil.getNextSiblingOfType(nextStatement,%0A                                    Psi
+ getNextStatement(next
 Stat
@@ -6165,14 +6165,8 @@
 ment
-.class
 );%0A 
@@ -6330,32 +6330,32 @@
 ;%0A            %7D%0A
-
             regi
@@ -6392,20 +6392,988 @@
 );%0A        %7D
+%0A%0A        @Nullable%0A        private static PsiStatement getNextStatement(PsiStatement statement) %7B%0A            PsiStatement nextStatement =%0A                    PsiTreeUtil.getNextSiblingOfType(statement,%0A                            PsiStatement.class);%0A            while (nextStatement == null) %7B%0A                //noinspection AssignmentToMethodParameter%0A                statement = PsiTreeUtil.getParentOfType(statement, %0A                        PsiStatement.class);%0A                if (statement == null) %7B%0A                    return null;%0A                %7D%0A                if (statement instanceof PsiLoopStatement) %7B%0A                    // return in a loop statement is not the same as continuing%0A                    // looping.%0A                    return statement;%0A                %7D%0A                nextStatement = PsiTreeUtil.getNextSiblingOfType(statement,%0A                        PsiStatement.class);%0A            %7D%0A            return nextStatement;%0A        %7D
 %0A    %7D%0A%7D

fix some false positives when if statement does not have an else branch