// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.intention.impl;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.refactoring.BaseRefactoringIntentionAction;
import com.intellij.refactoring.encapsulateFields.EncapsulateFieldsHandler;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Danila Ponomarenko
 */
public class EncapsulateFieldAction extends BaseRefactoringIntentionAction {

  @NotNull
  @Override
  public String getText() {
    return CodeInsightBundle.message("intention.encapsulate.field.text");
  }

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

  @Override
  public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
    if (element instanceof SyntheticElement) {
      return false;
    }

    return getField(element) != null;
  }

  @Override
  public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final PsiField field = getField(element);
    if (field == null) {
      return;
    }

    DataContext dataContext = DataManager.getInstance().getDataContext(editor.getComponent());
    new EncapsulateFieldsHandler().invoke(project, new PsiElement[]{field}, dataContext);
  }


  @Nullable
  protected static PsiField getField(@Nullable PsiElement element) {
    if (!(element instanceof PsiIdentifier)) {
      return null;
    }

    final PsiElement parent = element.getParent();
    if (!(parent instanceof PsiReferenceExpression)) {
      return null;
    }
    final PsiReferenceExpression ref = (PsiReferenceExpression)parent;

    final PsiElement resolved = ref.resolve();
    return resolved instanceof PsiField ? (PsiField)resolved : null;
  }

  @Override
  public boolean startInWriteAction() {
    return false;
  }
}
@@ -1210,25 +1210,24 @@
 eticElement)
- 
 %7B%0A      retu
@@ -1251,40 +1251,174 @@
     
-return getField(element) != null
+final PsiField field = getField(element);%0A    return field != null && !field.hasModifierProperty(PsiModifier.FINAL) && !field.hasModifierProperty(PsiModifier.PRIVATE)
 ;%0A  
@@ -2198,16 +2198,178 @@
 )parent;
+%0A    final PsiExpression qualifier = ref.getQualifierExpression();%0A    if (qualifier == null %7C%7C qualifier instanceof PsiThisExpression) %7B%0A      return null;%0A    %7D
 %0A%0A    fi
@@ -2413,23 +2413,22 @@
 ();%0A    
-return 
+if (!(
 resolved
@@ -2451,10 +2451,48 @@
 ield
- ?
+)) %7B%0A      return null;%0A    %7D%0A    return
  (Ps
@@ -2510,15 +2510,8 @@
 lved
- : null
 ;%0A  

Revert "IDEA-217722 Encapsulate field: relax intention availability"