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

import com.intellij.ide.plugins.DisabledPluginsState;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.internal.statistic.beans.MetricEvent;
import com.intellij.internal.statistic.beans.MetricEventFactoryKt;
import com.intellij.internal.statistic.eventLog.FeatureUsageData;
import com.intellij.internal.statistic.service.fus.collectors.ApplicationUsagesCollector;
import com.intellij.internal.statistic.utils.PluginInfo;
import com.intellij.internal.statistic.utils.PluginInfoDetectorKt;
import com.intellij.openapi.extensions.PluginId;
import org.jetbrains.annotations.NotNull;

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

final class PluginsUsagesCollector extends ApplicationUsagesCollector {
  @Override
  @NotNull
  public String getGroupId() {
    return "plugins";
  }

  @Override
  public int getVersion() {
    return 2;
  }

  @Override
  @NotNull
  public Set<MetricEvent> getMetrics() {
    Set<MetricEvent> result = new HashSet<>();
    for (PluginId id : DisabledPluginsState.disabledPlugins()) {
      PluginInfo info = PluginInfoDetectorKt.getPluginInfoById(id);
      FeatureUsageData data = new FeatureUsageData().addPluginInfo(info);
      result.add(MetricEventFactoryKt.newMetric("disabled.plugin", data));
    }

    for (IdeaPluginDescriptor descriptor : PluginManagerCore.getLoadedPlugins()) {
      if (descriptor.isEnabled() && !descriptor.isBundled()) {
        PluginInfo info = PluginInfoDetectorKt.getPluginInfoByDescriptor(descriptor);
        FeatureUsageData data = new FeatureUsageData().addPluginInfo(info);
        result.add(MetricEventFactoryKt.newMetric("enabled.not.bundled.plugin", data));
      }
    }
    return result;
  }
}
@@ -531,32 +531,228 @@
 ventLog.
-FeatureUsageData
+EventLogGroup;%0Aimport com.intellij.internal.statistic.eventLog.FeatureUsageData;%0Aimport com.intellij.internal.statistic.eventLog.events.EventFields;%0Aimport com.intellij.internal.statistic.eventLog.events.EventId1
 ;%0Aimport
@@ -1178,85 +1178,359 @@
 %7B%0A  
-@Override%0A  @NotNull%0A  public String getGroupId() %7B%0A    return %22plugins%22;%0A  %7D
+private static final EventLogGroup GROUP = new EventLogGroup(%22plugins%22, 3);%0A  public static final EventId1%3CPluginInfo%3E DISABLED_PLUGIN = GROUP.registerEvent(%22disabled.plugin%22, EventFields.PluginInfo);%0A  public static final EventId1%3CPluginInfo%3E ENABLED_NOT_BUNDLED_PLUGIN =%0A    GROUP.registerEvent(%22enabled.not.bundled.plugin%22, EventFields.PluginInfo);
 %0A%0A  
@@ -1552,22 +1552,30 @@
 lic 
-int getVersion
+EventLogGroup getGroup
 () %7B
@@ -1586,17 +1586,21 @@
  return 
-2
+GROUP
 ;%0A  %7D%0A%0A 
@@ -1852,147 +1852,46 @@
     
-FeatureUsageData data = new FeatureUsageData().addPluginInfo(info);%0A      result.add(MetricEventFactoryKt.newMetric(%22disabled.plugin%22, data
+result.add(DISABLED_PLUGIN.metric(info
 ));%0A
@@ -2141,160 +2141,57 @@
     
-FeatureUsageData data = new FeatureUsageData().addPluginInfo(info);%0A        result.add(MetricEventFactoryKt.newMetric(%22enabled.not.bundled.plugin%22, data
+result.add(ENABLED_NOT_BUNDLED_PLUGIN.metric(info
 ));%0A

migrate plugins collector to new API