// Copyright 2000-2018 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.openapi.externalSystem.service.project.manage;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.externalSystem.ExternalSystemManager;
import com.intellij.openapi.externalSystem.model.DataNode;
import com.intellij.openapi.externalSystem.model.ExternalProjectInfo;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
import com.intellij.openapi.externalSystem.model.project.ContentRootData;
import com.intellij.openapi.externalSystem.model.project.ProjectData;
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl;
import com.intellij.openapi.externalSystem.service.project.ProjectDataManager;
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

import static com.intellij.openapi.externalSystem.model.ProjectKeys.CONTENT_ROOT;

public class ReprocessContentRootDataActivity implements StartupActivity, DumbAware {

  private static final Logger LOG = Logger.getInstance(ReprocessContentRootDataActivity.class);

  @Override
  public void runActivity(@NotNull Project project) {

    final ProjectDataManager dataManager = ProjectDataManager.getInstance();
    final ContentRootDataService service = new ContentRootDataService();
    final IdeModifiableModelsProviderImpl modifiableModelsProvider = new IdeModifiableModelsProviderImpl(project);

    if (ApplicationManager.getApplication().isUnitTestMode()) {
      LOG.info("Adding 'reprocess content root data' activity to 'runWhenSmart' queue in project [hash=" + project.hashCode() + "]");
    }
    DumbService.getInstance(project).runWhenSmart(
        () -> {

          if (ApplicationManager.getApplication().isUnitTestMode()) {
            LOG.info("Reprocessing content root data for project [hash=" + project.hashCode() + "]" );
          }
          final boolean haveModulesToProcess = ModuleManager.getInstance(project).getModules().length > 0;
          if (!haveModulesToProcess) {
            return;
          }

          try {
            for (ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
              ProjectSystemId id = manager.getSystemId();
              for (ExternalProjectInfo info : dataManager.getExternalProjectsData(project, id)) {
                DataNode<ProjectData> projectStructure = info.getExternalProjectStructure();
                if (projectStructure != null) {
                  Collection<DataNode<ContentRootData>> roots = ExternalSystemApiUtil.findAllRecursively(projectStructure, CONTENT_ROOT);
                  service.importData(roots, null, project, modifiableModelsProvider);
                }
              }
            }
          } finally {
            ExternalSystemApiUtil.doWriteAction(() -> modifiableModelsProvider.commit());
          }
        }
    );


  }
}

@@ -203,16 +203,69 @@
 anage;%0A%0A
+import com.intellij.openapi.application.Application;%0A
 import c
@@ -1111,57 +1111,8 @@
 re;%0A
-import com.intellij.openapi.project.DumbService;%0A
 impo
@@ -1878,28 +1878,56 @@
 ject);%0A%0A    
-if (
+final Application application = 
 ApplicationM
@@ -1941,32 +1941,53 @@
 getApplication()
+;%0A    if (application
 .isUnitTestMode(
@@ -2139,53 +2139,31 @@
     
-DumbService.getInstance(project).runWhenSmart
+application.invokeLater
 (%0A  
@@ -2176,17 +2176,16 @@
 () -%3E %7B%0A
-%0A
         
@@ -2190,39 +2190,17 @@
     if (
-ApplicationManager.getA
+a
 pplicati
@@ -2201,18 +2201,16 @@
 lication
-()
 .isUnitT

fix flaky test (do not wait for smart mode)