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

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.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme;
import com.intellij.openapi.options.SchemeManager;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColorUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

public class EditorColorSchemesUsagesCollector extends ApplicationUsagesCollector {

  private final static int CURR_VERSION = 2;

  public static final String SCHEME_NAME_OTHER = "Other";
  public final static String[] KNOWN_NAMES = {
    "Default",
    "Darcula",
    "Obsidian",
    "Visual Studio",
    "Solarized",
    "Wombat",
    "Monkai",
    "XCode",
    "Sublime",
    "Oblivion",
    "Zenburn",
    "Cobalt",
    "Netbeans",
    "Eclipse",
    "Aptana",
    "Flash Builder",
    "IdeaLight",
    "High сontrast",
    "ReSharper",
    "Rider"
  };

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

  @NotNull
  @Override
  public Set<MetricEvent> getMetrics() {
    EditorColorsScheme currentScheme = EditorColorsManager.getInstance().getGlobalScheme();
    Set<MetricEvent> usages = new HashSet<>();
    if (currentScheme instanceof AbstractColorsScheme) {
      String schemeName = currentScheme.getName();
      if (schemeName.startsWith(SchemeManager.EDITABLE_COPY_PREFIX)) {
        EditorColorsScheme original = ((AbstractColorsScheme)currentScheme).getOriginal();
        if (original != null) {
          schemeName = original.getName();
        }
      }
      final String reportableName = getKnownSchemeName(schemeName);
      usages.add(MetricEventFactoryKt.newMetric(reportableName, ColorUtil.isDark(currentScheme.getDefaultBackground())));
    }
    return usages;
  }

  @NotNull
  private static String getKnownSchemeName(@NonNls @NotNull String schemeName) {
    for (@NonNls String knownName : KNOWN_NAMES) {
      if (StringUtil.toUpperCase(schemeName).contains(StringUtil.toUpperCase(knownName))) {
        return knownName;
      }
    }
    return SCHEME_NAME_OTHER;
  }

  @NotNull
  @Override
  public String getGroupId() {
    return "ui.editor.color.schemes";
  }

  @Nullable
  @Override
  public FeatureUsageData getData() {
    return new FeatureUsageData().addOS();
  }
}

@@ -890,51 +890,8 @@
 ull;
-%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aim
@@ -1064,17 +1064,17 @@
 RSION = 
-2
+3
 ;%0A%0A  pub
@@ -2149,31 +2149,81 @@
 nal 
-String reportableName =
+FeatureUsageData data = new FeatureUsageData().%0A        addData(%22scheme%22,
  get
@@ -2253,128 +2253,171 @@
 ame)
-;
+).
 %0A      
-usages.add(MetricEventFactoryKt.newMetric(reportableName, ColorUtil.isDark(currentScheme.getDefaultBackground())
+  addData(%22is_dark%22, ColorUtil.isDark(currentScheme.getDefaultBackground()));%0A      usages.add(MetricEventFactoryKt.newMetric(%22enabled.color.scheme%22, data
 ));%0A
@@ -2855,117 +2855,7 @@
 %0A  %7D
-%0A%0A  @Nullable%0A  @Override%0A  public FeatureUsageData getData() %7B%0A    return new FeatureUsageData().addOS();%0A  %7D
 %0A%7D%0A

move scheme name to event data in "ui.editor.color.schemes" (FUS-515)