// Copyright 2000-2020 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.codeInspection.unused;

import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.QuickFix;
import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesBundle;
import com.intellij.lang.properties.PropertiesImplUtil;
import com.intellij.lang.properties.ResourceBundle;
import com.intellij.lang.properties.editor.inspections.ResourceBundleEditorProblemDescriptor;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.psi.Property;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.function.Function;

public class UnusedPropertyUtil {
  private static final Logger LOG = Logger.getInstance(UnusedPropertyUtil.class);
  @NotNull
  public static Function<IProperty[], ResourceBundleEditorProblemDescriptor[]> buildPropertyGroupVisitor(@NotNull ResourceBundle resourceBundle) {
    final Module module = ModuleUtilCore.findModuleForPsiElement(resourceBundle.getDefaultPropertiesFile().getContainingFile());
    if (module == null) return x -> null;
    final UnusedPropertyInspection.UnusedPropertiesSearchHelper helper = new UnusedPropertyInspection.UnusedPropertiesSearchHelper(module);

    return properties -> !UnusedPropertyInspection.isPropertyUsed((Property)properties[0], helper, true) ? new ResourceBundleEditorProblemDescriptor[]{
      new ResourceBundleEditorProblemDescriptor(ProblemHighlightType.LIKE_UNUSED_SYMBOL,
                                                PropertiesBundle.message("unused.property.problem.descriptor.name"),
                                                new RemovePropertiesFromAllLocalesFix((Property)properties[0]))} : null;
  }

  private static class RemovePropertiesFromAllLocalesFix implements QuickFix<ResourceBundleEditorProblemDescriptor> {
    private final SmartPsiElementPointer<Property> myRepresentativePointer;

    private RemovePropertiesFromAllLocalesFix(Property property) {
      myRepresentativePointer = SmartPointerManager.getInstance(property.getProject()).createSmartPsiElementPointer(property);
    }

    @Nls
    @NotNull
    @Override
    public String getFamilyName() {
      return PropertiesBundle.message("remove.property.intention.text");
    }

    @Override
    public boolean startInWriteAction() {
      return false;
    }

    @Override
    public void applyFix(@NotNull Project project, @NotNull ResourceBundleEditorProblemDescriptor descriptor) {
      final Property element = myRepresentativePointer.getElement();
      if (element == null) return;
      final String key = element.getKey();
      if (key == null) return;
      final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(myRepresentativePointer.getContainingFile());
      LOG.assertTrue(file != null);
      file.getResourceBundle()
        .getPropertiesFiles()
        .stream()
        .flatMap(f -> f.findPropertiesByKey(key).stream())
        .filter(Objects::nonNull)
        .map(IProperty::getPsiElement)
        .filter(FileModificationService.getInstance()::preparePsiElementForWrite)
        .forEach(e -> WriteAction.run(() -> e.delete()));
    }
  }
}

@@ -990,44 +990,8 @@
 ct;%0A
-import com.intellij.psi.PsiElement;%0A
 impo
@@ -1075,24 +1075,75 @@
 entPointer;%0A
+import com.intellij.util.containers.ContainerUtil;%0A
 import org.j
@@ -1211,16 +1211,39 @@
 tNull;%0A%0A
+import java.util.List;%0A
 import j
@@ -3465,62 +3465,318 @@
     
-file.getResourceBundle()%0A        .getPropertiesFiles()
+List%3CPropertiesFile%3E propertiesFiles = file.getResourceBundle().getPropertiesFiles();%0A      if (!FileModificationService.getInstance()%0A        .preparePsiElementsForWrite(ContainerUtil.map2List(propertiesFiles, p -%3E p.getContainingFile()))) %7B%0A        return;%0A      %7D%0A      WriteAction.run(() -%3E propertiesFiles
 %0A   
@@ -3936,120 +3936,16 @@
   .f
-ilter(FileModificationService.getInstance()::preparePsiElementForWrite)%0A        .forEach(e -%3E WriteAction.run(()
+orEach(e
  -%3E 
@@ -3955,16 +3955,23 @@
 elete())
+%0A      
 );%0A    %7D

remove unused property: show one clear status dialog