/*
 * Copyright 2000-2011 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.refactoring.typeMigration;

import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.typeMigration.ui.TypeMigrationDialog;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import org.jetbrains.annotations.NotNull;

public class ChangeTypeSignatureHandler implements RefactoringActionHandler {
  private static final Logger LOG = Logger.getInstance("#" + ChangeTypeSignatureHandler.class.getName());

  public static final String REFACTORING_NAME = "Type Migration";

  public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    final int offset = TargetElementUtilBase.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset());
    final PsiElement element = file.findElementAt(offset);
    PsiTypeElement typeElement = PsiTreeUtil.getParentOfType(element, PsiTypeElement.class);
    while (typeElement != null) {
      final PsiElement parent = typeElement.getParent();
      if (parent instanceof PsiVariable || parent instanceof PsiMember || (parent instanceof PsiReferenceParameterList && PsiTreeUtil.getParentOfType(parent, PsiMember.class) instanceof PsiClass)) {
        invoke(project, parent, null, editor);
        return;
      }
      typeElement = PsiTreeUtil.getParentOfType(parent, PsiTypeElement.class, false);
    }
    CommonRefactoringUtil.showErrorHint(project, editor,
                                        "The caret should be positioned on type of field, variable, method or method parameter to be refactored",
                                        REFACTORING_NAME, "refactoring.migrateType");
  }


  public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
    LOG.assertTrue(elements.length == 1);
    final PsiElement element = elements[0];
    invokeOnElement(project, element);
  }

  public static boolean invokeOnElement(final Project project, final PsiElement element) {
    if (element instanceof PsiVariable || element instanceof PsiMember || element instanceof PsiFile) {
      invoke(project, element, null, null);
      return true;
    }
    if (element instanceof PsiReferenceParameterList && PsiTreeUtil.getParentOfType(element, PsiMember.class) instanceof PsiClass) {
      invoke(project, element, null, null);
      return true;
    }
    return false;
  }

  public static void invoke(final Project project, final PsiElement root, final TypeMigrationRules rules, final Editor editor) {
    if (Util.canBeMigrated(root)) {
      TypeMigrationDialog dialog = new TypeMigrationDialog(project, root, rules);
      dialog.show();
      return;
    }

    CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.message("only.fields.variables.of.methods.of.valid.type.can.be.considered"),
                                   RefactoringBundle.message("unable.to.start.type.migration"), null);

  }
}
@@ -2120,16 +2120,17 @@
 able %7C%7C 
+(
 parent i
@@ -2149,19 +2149,20 @@
 iMember 
-%7C%7C 
+&& !
 (parent 
@@ -2179,106 +2179,41 @@
  Psi
-ReferenceParameterList && PsiTreeUtil.getParentOfType(parent, PsiMember.class) instanceof PsiClass
+Class)) %7C%7C isClassArgument(parent
 )) %7B
@@ -3054,24 +3054,25 @@
 Variable %7C%7C 
+(
 element inst
@@ -3087,16 +3087,51 @@
 siMember
+ && !(element instanceof PsiClass))
  %7C%7C elem
@@ -3221,24 +3221,217 @@
 true;%0A    %7D%0A
+    if (isClassArgument(element)) %7B%0A      invoke(project, element, null, null);%0A      return true;%0A    %7D%0A    return false;%0A  %7D%0A%0A  protected static boolean isClassArgument(PsiElement element) %7B%0A
     if (elem
@@ -3470,19 +3470,50 @@
 eterList
- &&
+) %7B%0A      final PsiMember member =
  PsiTree
@@ -3558,16 +3558,34 @@
 r.class)
+;%0A      if (member
  instanc
@@ -3610,64 +3610,321 @@
     
+  
+f
 in
-voke(project, element, null, null);%0A      return true;
+al PsiReferenceList implementsList = ((PsiClass)member).getImplementsList();%0A        final PsiReferenceList extendsList = ((PsiClass)member).getExtendsList();%0A        return PsiTreeUtil.isAncestor(implementsList, element, false) %7C%7C %0A               PsiTreeUtil.isAncestor(extendsList, element, false);%0A      %7D
 %0A   

disable 'change type signature' for classes and other invalid constructs