// 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.internal.statistic.collectors.fus.fileTypes;

import com.intellij.internal.statistic.beans.UsageDescriptor;
import com.intellij.internal.statistic.service.fus.collectors.ProjectUsagesCollector;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.SourceFolder;
import com.intellij.util.containers.JBIterable;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.module.JpsModuleSourceRootType;
import org.jetbrains.jps.model.serialization.JpsElementPropertiesSerializer;
import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.intellij.internal.statistic.utils.StatisticsUtilKt.getCountingUsage;

/**
 * @author gregsh
 */
public class ProjectStructureUsageCollector extends ProjectUsagesCollector {
  @NotNull
  @Override
  public Set<UsageDescriptor> getUsages(@NotNull Project project) {
    Map<? extends JpsModuleSourceRootType<?>, String> typeNames = JBIterable.from(JpsModelSerializerExtension.getExtensions())
      .flatMap(JpsModelSerializerExtension::getModuleSourceRootPropertiesSerializers)
      .toMap(JpsElementPropertiesSerializer::getType, JpsElementPropertiesSerializer::getTypeId);

    Set<UsageDescriptor> result = new HashSet<>();
    TObjectIntHashMap<String> types = new TObjectIntHashMap<>();
    Module[] modules = ModuleManager.getInstance(project).getModules();
    result.add(getCountingUsage("modules.count", modules.length));
    for (Module module : modules) {
      ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
      result.add(getCountingUsage("content.roots.count", rootManager.getContentEntries().length));
      result.add(getCountingUsage("source.roots.count", rootManager.getSourceRoots(true).length));
      result.add(getCountingUsage("excluded.roots.count", rootManager.getExcludeRoots().length));
      for (ContentEntry entry : rootManager.getContentEntries()) {
        for (SourceFolder source : entry.getSourceFolders()) {
          String key = typeNames.get(source.getRootType());
          if (key == null) continue;
          if (!types.increment(key)) {
            types.put(key, 1);
          }
        }
      }
      types.forEachEntry((key, count) -> result.add(getCountingUsage("source.root." + key, count)));
      types.clear();
    }
    return result;
  }

  @NotNull
  @Override
  public String getGroupId() {
    return "statistics.project.structure";
  }
}

@@ -632,24 +632,114 @@
 urceFolder;%0A
+import com.intellij.openapi.util.text.StringUtil;%0Aimport com.intellij.util.PlatformUtils;%0A
 import com.i
@@ -770,24 +770,24 @@
 JBIterable;%0A
-
 import gnu.t
@@ -1730,16 +1730,55 @@
 TypeId);
+%0A    boolean packagePrefixUsed = false;
 %0A%0A    Se
@@ -2561,24 +2561,125 @@
 olders()) %7B%0A
+          packagePrefixUsed = packagePrefixUsed %7C%7C StringUtil.isNotEmpty(source.getPackagePrefix());%0A
           St
@@ -2960,24 +2960,24 @@
 , count)));%0A
-
       types.
@@ -2987,24 +2987,178 @@
 ar();%0A    %7D%0A
+    if (PlatformUtils.isIntelliJ()) %7B%0A      result.add(new UsageDescriptor(packagePrefixUsed ? %22package.prefix.used%22 : %22package.prefix.not.used%22));%0A    %7D%0A
     return r

add packagePrefix info for IntelliJ