// 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.platform;

import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
final class PlatformProjectConfigurator implements DirectoryProjectConfigurator {
  private static final Logger LOG = Logger.getInstance(PlatformProjectConfigurator.class);

  @Override
  public void configureProject(@NotNull Project project, @NotNull VirtualFile baseDir, @NotNull Ref<Module> moduleRef) {
    final ModuleManager moduleManager = ModuleManager.getInstance(project);
    final Module[] modules = moduleManager.getModules();
    if (modules.length != 0) {
      LOG.info("PlatformProjectConfigurator is not applicable because modules are already configured (module count: " + modules.length + ")");
      return;
    }

    ApplicationManager.getApplication().runWriteAction(() -> {
      String moduleName = baseDir.getName().replace(":", "");     // correct module name when opening root of drive as project (RUBY-5181)
      String imlName = baseDir.getPath() + "/.idea/" + moduleName + ModuleFileType.DOT_DEFAULT_EXTENSION;
      ModuleTypeManager instance = ModuleTypeManager.getInstance();
      String id = instance == null ? "unknown" : instance.getDefaultModuleType().getId();
      final Module module = moduleManager.newModule(imlName, id);
      ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
      ModifiableRootModel rootModel = rootManager.getModifiableModel();
      if (rootModel.getContentRoots().length == 0) {
        rootModel.addContentEntry(baseDir);
      }
      rootModel.inheritSdk();
      rootModel.commit();
      moduleRef.set(module);
    });
  }
}

@@ -1418,133 +1418,8 @@
     
-ApplicationManager.getApplication().runWriteAction(() -%3E %7B%0A      String moduleName = baseDir.getName().replace(%22:%22, %22%22);     
 // c
@@ -1489,16 +1489,74 @@
 5181)%0A  
+  String moduleName = baseDir.getName().replace(%22:%22, %22%22);%0A
     Stri
@@ -1651,26 +1651,24 @@
 ENSION;%0A    
-  
 ModuleTypeMa
@@ -1673,24 +1673,33 @@
 Manager 
-instance
+moduleTypeManager
  = Modul
@@ -1730,109 +1730,77 @@
 ();%0A
-  
+%0A
     
-String id = instance == null ? %22unknown%22 : instance.getDefaultModuleType().getId();%0A      final
+ApplicationManager.getApplication().runWriteAction(() -%3E %7B%0A     
  Mod
@@ -1849,37 +1849,26 @@
 me, 
-id);%0A      M
+m
 odule
-Root
+Type
 Manager 
 root
@@ -1867,32 +1867,40 @@
 ger 
-rootManager = M
+== null ? %22unknown%22 : m
 odule
-Root
+Type
 Mana
@@ -1910,23 +1910,35 @@
 .get
-Instance(module
+DefaultModuleType().getId()
 );%0A 
@@ -1966,21 +1966,23 @@
 del 
-rootM
+m
 odel = 
-r
+ModuleR
 ootM
@@ -1984,24 +1984,44 @@
 RootManager.
+getInstance(module).
 getModifiabl
@@ -2040,39 +2040,100 @@
     
-if (rootModel.getContentRoots()
+try %7B%0A        VirtualFile%5B%5D contentRoots = model.getContentRoots();%0A        if (contentRoots
 .len
@@ -2148,29 +2148,27 @@
 ) %7B%0A        
-rootM
+  m
 odel.addCont
@@ -2196,16 +2196,18 @@
     
+  
 %7D%0A      
 root
@@ -2206,57 +2206,378 @@
     
-rootModel.inheritSdk();%0A      rootModel.commit();
+  else %7B%0A          LOG.info(%22PlatformProjectConfigurator did not add content entry because content roots are already configured %22 +%0A                   %22(content root count: %22 + contentRoots.length + %22)%22);%0A        %7D%0A        model.inheritSdk();%0A        model.commit();%0A      %7D%0A      finally %7B%0A        if (!model.isDisposed()) %7B%0A          model.dispose();%0A        %7D%0A      %7D
 %0A   

add LOG.info if content root is not added