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

import com.intellij.internal.statistic.connect.StatisticsService;
import com.intellij.internal.statistic.eventLog.*;
import com.intellij.internal.statistic.persistence.UsageStatisticsPersistenceComponent;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public final class StatisticsUploadAssistant {
  private static final String IDEA_SUPPRESS_REPORT_STATISTICS = "idea.suppress.statistics.report";
  private static final String ENABLE_LOCAL_STATISTICS_WITHOUT_REPORT = "idea.local.statistics.without.report";

  public static final Object LOCK = new Object();

  private StatisticsUploadAssistant() {}

  public static boolean isSendAllowed() {
    if (Boolean.getBoolean(IDEA_SUPPRESS_REPORT_STATISTICS) || Boolean.getBoolean(ENABLE_LOCAL_STATISTICS_WITHOUT_REPORT)) {
      return false;
    }

    UsageStatisticsPersistenceComponent settings = UsageStatisticsPersistenceComponent.getInstance();
    return settings != null && settings.isAllowed();
  }

  public static boolean isCollectAllowed() {
    final UsageStatisticsPersistenceComponent settings = UsageStatisticsPersistenceComponent.getInstance();
    return (settings != null && settings.isAllowed()) || Boolean.getBoolean(ENABLE_LOCAL_STATISTICS_WITHOUT_REPORT);
  }

  public static boolean isTestStatisticsEnabled() {
    return Boolean.getBoolean(ENABLE_LOCAL_STATISTICS_WITHOUT_REPORT) || StringUtil.isNotEmpty(System.getenv("TEAMCITY_VERSION"));
  }

  public static @NotNull StatisticsService getEventLogStatisticsService(@NotNull String recorderId) {
    EventLogSendListener listener = new EventLogSendListener() {
      @Override
      public void onLogsSend(@NotNull List<String> successfullySentFiles, int failed, int totalLocalFiles) {
        EventLogSystemLogger.logFilesSend(recorderId, totalLocalFiles, successfullySentFiles.size(), failed, false, successfullySentFiles);
      }
    };

    return new EventLogStatisticsService(
      new DeviceConfiguration(EventLogConfiguration.INSTANCE.getDeviceId(), EventLogConfiguration.INSTANCE.getBucket()),
      new EventLogInternalRecorderConfig(recorderId),
      new EventLogInternalApplicationInfo(recorderId, false), listener
    );
  }

  public static EventLogUploadSettingsService createExternalSettings(@NotNull String recorderId, boolean isTest) {
    return new EventLogUploadSettingsService(recorderId, new EventLogInternalApplicationInfo(recorderId, isTest));
  }
}

@@ -387,16 +387,76 @@
 ponent;%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -497,16 +497,16 @@
 ngUtil;%0A
-
 import o
@@ -611,16 +611,115 @@
 stant %7B%0A
+  private static final String IDEA_HEADLESS_ENABLE_STATISTICS = %22idea.headless.enable.statistics%22;%0A
   privat
@@ -1208,16 +1208,82 @@
 %0A    %7D%0A%0A
+    if (isHeadlessStatisticsEnabled()) %7B%0A      return true;%0A    %7D%0A
     Usag
@@ -1475,24 +1475,90 @@
 Allowed() %7B%0A
+    if (isHeadlessStatisticsEnabled()) %7B%0A      return true;%0A    %7D%0A
     final Us
@@ -1645,32 +1645,32 @@
 .getInstance();%0A
-
     return (sett
@@ -1767,32 +1767,221 @@
 T_REPORT);%0A  %7D%0A%0A
+  private static boolean isHeadlessStatisticsEnabled() %7B%0A    return Boolean.getBoolean(IDEA_HEADLESS_ENABLE_STATISTICS) && ApplicationManager.getApplication().isHeadlessEnvironment();%0A  %7D%0A%0A
   public static 

add vmoption to enable logging in headless mode (IDEA-244178)