package com.jetbrains.python.codeInsight.intentions;

import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonLanguage;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * Author: Alexey.Ivanov
 * Date:   10.03.2010
 * Time:   18:52:52
 */
public class PySplitIfIntention extends BaseIntentionAction {
  @NotNull
  public String getFamilyName() {
    return PyBundle.message("INTN.split.if");
  }

  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    PsiElement element = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class, false);
    if (element == null) {
      return false;
    }

    while (element.getParent() instanceof PyBinaryExpression) {
      element = element.getParent();
    }
    if (((PyBinaryExpression)element).getOperator() != PyTokenTypes.AND_KEYWORD
        || ((PyBinaryExpression) element).getRightExpression() == null) {
      return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PyIfPart)) {
      return false;
    }
    setText(PyBundle.message("INTN.split.if.text"));
    return true;
  }

  public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PyBinaryExpression element = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class);
    while (element.getParent() instanceof PyBinaryExpression) {
      element = (PyBinaryExpression)element.getParent();
    }
    PyIfStatement ifStatement = PsiTreeUtil.getParentOfType(element, PyIfStatement.class);
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    StringBuilder builder = new StringBuilder();
  
    builder.append("if ").append(element.getLeftExpression().getText()).append(":\n");
    builder.append("    if ").append(element.getRightExpression().getText()).append(":");
    appendStatements(ifStatement.getIfPart().getStatementList(), builder);
    final PyIfPart[] elifParts = ifStatement.getElifParts();
    for (PyIfPart elifPart: elifParts) {
      builder.append("\n    elif ").append(elifPart.getCondition().getText()).append(":");
      appendStatements(elifPart.getStatementList(), builder);
    }
    final PyElsePart elsePart = ifStatement.getElsePart();
    if (elsePart != null) {
      builder.append("\n    else:");
      appendStatements(elsePart.getStatementList(), builder);
    }
    ifStatement.getIfPart().replace(elementGenerator.createFromText(PyIfStatement.class, builder.toString()));
  }

  private static void appendStatements(PyStatementList statementList, StringBuilder builder) {
    for (PyStatement statement : statementList.getStatements()) {
      builder.append("\n       ").append(statement.getText());
    }
  }
}

@@ -452,52 +452,8 @@
 es;%0A
-import com.jetbrains.python.PythonLanguage;%0A
 impo
@@ -2117,919 +2117,374 @@
     
-StringBuilder builder = new StringBuilder();%0A  %0A    builder.append(%22if %22).append(element.getLeftExpression().getText()).append(%22:%5Cn%22);%0A    builder.append(%22    if %22).append(element.getRightExpression().getText()).append(%22:%22);%0A    appendStatements(ifStatement.getIfPart().getStatementList(), builder);%0A    final PyIfPart%5B%5D elifParts = ifStatement.getElifParts();%0A    for (PyIfPart elifPart: elifParts) %7B%0A      builder.append(%22%5Cn    elif %22).append(elifPart.getCondition().getText()).append(%22:%22);%0A      appendStatements(elifPart.getStatementList(), builder);%0A    %7D%0A    final PyElsePart elsePart = ifStatement.getElsePart();%0A    if (elsePart != null) %7B%0A      builder.append(%22%5Cn    else:%22);%0A      appendStatements(elsePart.getStatementList(), builder);%0A    %7D%0A    ifStatement.getIfPart().replace(elementGenerator.createFromText(PyIfStatement.class, builder.toString()));%0A  %7D%0A%0A  private static void appendStatements(PyS
+%0A    PyIfStatement subIf = (PyIfStatement) ifStatement.copy();%0A    subIf.getIfPart().getCondition().replace(element.getRightExpression());%0A    ifStatement.getIfPart().getCondition().replace(element.getLeftExpression());%0A    PyStatementList statementList = elementGenerator.createFromText(PyIfStatement.class, %22if a:%5Cn    a = 1%22).getIfPart().getStatementList();%0A    s
 tate
@@ -2487,34 +2487,37 @@
 tatementList
- s
+.getS
 tatement
 List, String
@@ -2508,173 +2508,101 @@
 ment
-List, StringBuilder builder) %7B%0A    for (PyStatement statement : statementList.getStatements()) %7B%0A      builder.append(%22%5Cn       %22).append(statement.getText());%0A    %7D
+s()%5B0%5D.replace(subIf);%0A    ifStatement.getIfPart().getStatementList().replace(statementList);
 %0A  %7D

Improve PySplitIfIntention