// 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 de.plushnikov.intellij.plugin.jps;

import com.intellij.compiler.server.BuildProcessParametersProvider;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiUtilCore;
import de.plushnikov.intellij.plugin.LombokClassNames;
import de.plushnikov.intellij.plugin.Version;
import de.plushnikov.intellij.plugin.activity.LombokProjectValidatorActivity;
import de.plushnikov.intellij.plugin.settings.ProjectSettings;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public final class LombokBuildProcessParametersProvider extends BuildProcessParametersProvider {

  private final Project myProject;

  public LombokBuildProcessParametersProvider(Project project) {
    myProject = project;
  }

  @Override
  public @NotNull List<String> getVMArguments() {
    if (ProjectSettings.isLombokEnabledInProject(myProject) && LombokProjectValidatorActivity.hasLombokLibrary(myProject)) {
      return CachedValuesManager.getManager(myProject).getCachedValue(myProject,
                                                               () -> {
                                                                 List<String> disableOptions =
                                                                   ReadAction.compute(() -> disableOptimisation())
                                                                   ? Collections.singletonList("-Djps.track.ap.dependencies=false")
                                                                   : Collections.emptyList();
                                                                 return new CachedValueProvider.Result<>(disableOptions, ProjectRootManager.getInstance(myProject));
                                                               });

    }
    return super.getVMArguments();
  }

  private boolean disableOptimisation() {
    PsiClass aClass = JavaPsiFacade.getInstance(myProject).findClass(LombokClassNames.BUILDER, GlobalSearchScope.allScope(myProject));
    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(aClass);
    if (virtualFile != null) {
      List<OrderEntry> entries = ProjectRootManager.getInstance(myProject).getFileIndex().getOrderEntriesForFile(virtualFile);
      if (!entries.isEmpty()) {
        String lombokVersion = Version.parseLombokVersion(entries.get(0));
        if (lombokVersion != null && Version.compareVersionString(lombokVersion, "1.18.16") < 0) {
          return true;
        }
      }
    }
    return false;
  }
}

@@ -447,53 +447,8 @@
 er;%0A
-import com.intellij.openapi.vfs.VirtualFile;%0A
 impo
@@ -509,21 +509,25 @@
 .psi.Psi
-Class
+Directory
 ;%0Aimport
@@ -548,31 +548,17 @@
 psi.
-search.GlobalSearchScop
+PsiPackag
 e;%0Ai
@@ -660,105 +660,8 @@
 er;%0A
-import com.intellij.psi.util.PsiUtilCore;%0Aimport de.plushnikov.intellij.plugin.LombokClassNames;%0A
 impo
@@ -2276,20 +2276,24 @@
  Psi
-Class aClass
+Package aPackage
  = J
@@ -2336,154 +2336,140 @@
 find
-Class(LombokClassNames.BUILDER, GlobalSearchScope.allScope(myProject));%0A    VirtualFile virtualFile = PsiUtilCor
+Package(%22lombok.experimental%22);%0A    if (aPackage != null) %7B%0A      PsiDirectory%5B%5D directories = aPackag
 e.get
-VirtualFile(aClass
+Directories(
 );%0A
+  
     if (
 virt
@@ -2468,31 +2468,36 @@
 if (
-virtualFile != null
+directories.length %3E 0
 ) %7B%0A
+  
     
@@ -2605,17 +2605,35 @@
 ForFile(
-v
+directories%5B0%5D.getV
 irtualFi
@@ -2634,19 +2634,23 @@
 tualFile
+()
 );%0A
+  
       if
@@ -2669,24 +2669,26 @@
 sEmpty()) %7B%0A
+  
         Stri
@@ -2754,24 +2754,26 @@
 ));%0A        
+  
 if (lombokVe
@@ -2857,24 +2857,26 @@
 %7B%0A          
+  
 return true;
@@ -2876,16 +2876,28 @@
 n true;%0A
+          %7D%0A
         

fix INRE on startup