// 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.featureStatistics.fusCollectors;

import com.intellij.internal.statistic.eventLog.FeatureUsageData;
import com.intellij.internal.statistic.eventLog.FeatureUsageGroup;
import com.intellij.internal.statistic.eventLog.FeatureUsageLogger;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

public class LifecycleUsageTriggerCollector {
  private static final Logger LOG = Logger.getInstance("#com.intellij.featureStatistics.fusCollectors.LifecycleUsageTriggerCollector");
  private static final FeatureUsageGroup LIFECYCLE = new FeatureUsageGroup("lifecycle", 2);

  public static void onIdeStart() {
    FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "ide.start");
  }

  public static void onIdeClose(boolean restart) {
    final Map<String, Object> data = new FeatureUsageData().addData("restart", restart).build();
    FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "ide.close", data);
  }

  public static void onProjectOpenFinished(@NotNull Project project, long time) {
    final Map<String, Object> data = new FeatureUsageData().
      addProject(project).
      addData("time_ms", time).build();
    FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "project.opening.finished", data);
  }

  public static void onProjectOpened(@NotNull Project project) {
    final Map<String, Object> data = new FeatureUsageData().addProject(project).build();
    FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "project.opened", data);
  }

  public static void onProjectClosed(@NotNull Project project) {
    final Map<String, Object> data = new FeatureUsageData().addProject(project).build();
    FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "project.closed", data);
  }

  public static void onFreeze(int lengthInSeconds) {
    final FeatureUsageData builder =
      new FeatureUsageData().addData("duration_s", lengthInSeconds).addData("duration_grouped", toLengthGroup(lengthInSeconds));
    FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "ide.freeze", builder.build());
  }

  public static void onError(boolean isOOM, boolean isMappingFailed, @Nullable String pluginId) {
    try {
      final FeatureUsageData builder =
        new FeatureUsageData().
          addData("oom", isOOM).
          addData("mapping_failed", isMappingFailed).
          addData("plugin", StringUtil.notNullize(pluginId, "unknown"));
      FeatureUsageLogger.INSTANCE.log(LIFECYCLE, "ide.error", builder.build());
    }
    catch (Exception e) {
      LOG.warn(e);
    }
  }

  @NotNull
  private static String toLengthGroup(int seconds) {
    if (seconds >= 60) {
      return "60+";
    }
    if (seconds > 10) {
      seconds -= (seconds % 10);
      return seconds + "+";
    }
    return String.valueOf(seconds);
  }
}

@@ -361,32 +361,49 @@
 atistic.
-eventLog.Feature
+service.fus.collectors.FUSCounter
 UsageLog
@@ -639,31 +639,8 @@
 e;%0A%0A
-import java.util.Map;%0A%0A
 publ
@@ -947,30 +947,33 @@
 rt() %7B%0A    F
-eature
+USCounter
 UsageLogger.
@@ -968,36 +968,32 @@
 UsageLogger.
-INSTANCE.log
+logEvent
 (LIFECYCLE, 
@@ -1072,35 +1072,32 @@
   final 
-Map%3CString, Object%3E
+FeatureUsageData
  data = 
@@ -1150,53 +1150,44 @@
 art)
-.build();%0A    FeatureUsageLogger.INSTANCE.log
+;%0A    FUSCounterUsageLogger.logEvent
 (LIF
@@ -1311,35 +1311,32 @@
   final 
-Map%3CString, Object%3E
+FeatureUsageData
  data = 
@@ -1420,53 +1420,44 @@
 ime)
-.build();%0A    FeatureUsageLogger.INSTANCE.log
+;%0A    FUSCounterUsageLogger.logEvent
 (LIF
@@ -1579,35 +1579,32 @@
   final 
-Map%3CString, Object%3E
+FeatureUsageData
  data = 
@@ -1649,53 +1649,44 @@
 ect)
-.build();%0A    FeatureUsageLogger.INSTANCE.log
+;%0A    FUSCounterUsageLogger.logEvent
 (LIF
@@ -1802,27 +1802,24 @@
 nal 
-Map%3CString, Object%3E
+FeatureUsageData
  dat
@@ -1868,53 +1868,44 @@
 ect)
-.build();%0A    FeatureUsageLogger.INSTANCE.log
+;%0A    FUSCounterUsageLogger.logEvent
 (LIF
@@ -2014,39 +2014,36 @@
 eatureUsageData 
-builder
+data
  =%0A      new Fea
@@ -2159,30 +2159,33 @@
 nds));%0A    F
-eature
+USCounter
 UsageLogger.
@@ -2180,36 +2180,32 @@
 UsageLogger.
-INSTANCE.log
+logEvent
 (LIFECYCLE, 
@@ -2218,31 +2218,20 @@
 reeze%22, 
-builder.build()
+data
 );%0A  %7D%0A%0A
@@ -2367,23 +2367,20 @@
 ageData 
-builder
+data
  =%0A     
@@ -2573,22 +2573,25 @@
 %0A      F
-eature
+USCounter
 UsageLog
@@ -2598,20 +2598,16 @@
 ger.
-INSTANCE.log
+logEvent
 (LIF
@@ -2631,23 +2631,12 @@
 r%22, 
-builder.build()
+data
 );%0A 

switch lifecycle collector to event data