/*
 * 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 com.intellij.codeInsight.intention.impl;

import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Dmitry Batkovich
 */
public class CreateSwitchIntention extends BaseElementAtCaretIntentionAction {
  public static final String TEXT = "Create switch statement";

  @Override
  public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    final PsiExpressionStatement expressionStatement = resolveExpressionStatement(element);
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
    PsiSwitchStatement switchStatement = (PsiSwitchStatement)elementFactory
      .createStatementFromText(String.format("switch (%s) {}", expressionStatement.getExpression().getText()), null);
    switchStatement = (PsiSwitchStatement)expressionStatement.replace(switchStatement);
    CodeStyleManager.getInstance(project).reformat(switchStatement);

    final PsiJavaToken lBrace = switchStatement.getBody().getLBrace();
    editor.getCaretModel().moveToOffset(lBrace.getTextOffset() + lBrace.getTextLength());
  }

  @Override
  public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
    final PsiExpressionStatement expressionStatement = resolveExpressionStatement(element);
    return expressionStatement != null && isValidTypeForSwitch(expressionStatement.getExpression().getType(), expressionStatement);
  }

  private static PsiExpressionStatement resolveExpressionStatement(final PsiElement element) {
    if (element instanceof PsiExpressionStatement) {
      return (PsiExpressionStatement)element;
    } else {
      final PsiStatement psiStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
      return psiStatement instanceof PsiExpressionStatement ? (PsiExpressionStatement)psiStatement : null;
    }
  }

  private static boolean isValidTypeForSwitch(@Nullable final PsiType type, final PsiElement context) {
    if (type == null) {
      return false;
    }

    if (type instanceof PsiClassType) {
      final PsiClass resolvedClass = ((PsiClassType)type).resolve();
      if (resolvedClass == null) {
        return false;
      }
      return (PsiUtil.isLanguageLevel5OrHigher(context) && resolvedClass.isEnum())
             || (PsiUtil.isLanguageLevel7OrHigher(context) && CommonClassNames.JAVA_LANG_STRING.equals(resolvedClass.getQualifiedName()));
    }
    return type.equals(PsiType.INT) || type.equals(PsiType.BYTE) || type.equals(PsiType.SHORT) || type.equals(PsiType.CHAR);
  }

  @NotNull
  @Override
  public String getFamilyName() {
    return TEXT;
  }

  @NotNull
  @Override
  public String getText() {
    return getFamilyName();
  }
}

@@ -1321,38 +1321,32 @@
 invoke(@NotNull 
-final 
 Project project,
@@ -1337,38 +1337,32 @@
 Project project,
- final
  Editor editor, 
@@ -1362,38 +1362,32 @@
 ditor, @NotNull 
-final 
 PsiElement eleme
@@ -1427,30 +1427,24 @@
 ption %7B%0A    
-final 
 PsiExpressio
@@ -1513,30 +1513,24 @@
 ement);%0A    
-final 
 PsiElementFa
@@ -1604,24 +1604,98 @@
 tFactory();%0A
+    String valueToSwitch = expressionStatement.getExpression().getText();%0A
     PsiSwitc
@@ -1757,23 +1757,16 @@
 tFactory
-%0A      
 .createS
@@ -1786,86 +1786,43 @@
 ext(
-String.format(%22switch (%25s) %7B%7D%22, expressionStatement.getExpression().getText())
+%22switch (%22 + valueToSwitch + %22) %7B%7D%22
 , nu
@@ -1988,22 +1988,16 @@
 );%0A%0A    
-final 
 PsiJavaT
@@ -2194,22 +2194,16 @@
 NotNull 
-final 
 Project 
@@ -2210,22 +2210,16 @@
 project,
- final
  Editor 
@@ -2235,22 +2235,16 @@
 NotNull 
-final 
 PsiEleme
@@ -2261,22 +2261,16 @@
 ) %7B%0A    
-final 
 PsiExpre
@@ -2547,22 +2547,16 @@
 atement(
-final 
 PsiEleme
@@ -2687,22 +2687,16 @@
 %7B%0A      
-final 
 PsiState
@@ -2947,22 +2947,16 @@
 ullable 
-final 
 PsiType 
@@ -2960,22 +2960,16 @@
 pe type,
- final
  PsiElem
@@ -3078,20 +3078,14 @@
 ) %7B%0A
+
       
-final 
 PsiC

create switch intention cleanup