// 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.actions.persistence;

import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.internal.statistic.beans.ConvertUsagesUtil;
import com.intellij.internal.statistic.eventLog.FeatureUsageLogger;
import com.intellij.internal.statistic.persistence.UsageStatisticsPersistenceComponent;
import com.intellij.internal.statistic.utils.StatisticsUtilKt;
import com.intellij.openapi.components.*;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.ToolWindowEP;
import com.intellij.util.xmlb.annotations.MapAnnotation;
import com.intellij.util.xmlb.annotations.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Konstantin Bulenkov
 */
@State(
  name = "ToolWindowCollector",
  storages = {
    @Storage(value = UsageStatisticsPersistenceComponent.USAGE_STATISTICS_XML, roamingType = RoamingType.DISABLED)
  }
)
public class ToolWindowCollector implements PersistentStateComponent<ToolWindowCollector.State> {
  private static final String DEFAULT_ID = "third.party.plugin.toolwindow";

  public static ToolWindowCollector getInstance() {
    return ServiceManager.getService(ToolWindowCollector.class);
  }

  public void recordActivation(String toolWindowId) {
    record(toolWindowId, "Activation");
  }

  //todo[kb] provide a proper way to track activations by clicks
  public void recordClick(String toolWindowId) {
    record(toolWindowId, "Click");
  }

  private void record(@Nullable String toolWindowId, @NotNull String source) {
    if (toolWindowId == null) return;
    State state = getState();
    if (state == null) return;

    boolean isJB = isDevelopedByJetBrains(toolWindowId);
    final String key = ConvertUsagesUtil.escapeDescriptorName((isJB ? toolWindowId : DEFAULT_ID) + "_by_" + source);
    FeatureUsageLogger.INSTANCE.log("toolwindow", key);
    final Integer count = state.myValues.get(key);
    int value = count == null ? 1 : count + 1;
    state.myValues.put(key, value);
  }

  public static boolean isDevelopedByJetBrains(@NotNull String toolWindowId) {
    for (ToolWindowEP ep : ToolWindowEP.EP_NAME.getExtensions()) {
      if (StringUtil.equals(toolWindowId, ep.id)) {
        final PluginId id = StringUtil.isNotEmpty(ep.factoryClass) ? PluginManagerCore.getPluginByClassName(ep.factoryClass) : null;
        return StatisticsUtilKt.isDevelopedByJetBrains(id);
      }
    }
    return false;
  }

  private State myState = new State();

  @Nullable
  @Override
  public State getState() {
    return myState;
  }

  @Override
  public void loadState(@NotNull State state) {
    myState = state;
  }

  public final static class State {
    @Tag("counts")
    @MapAnnotation(surroundWithTag = false, keyAttributeName = "toolWindow", valueAttributeName = "count")
    public Map<String, Integer> myValues = new HashMap<>();
  }
}

@@ -215,59 +215,8 @@
 e;%0A%0A
-import com.intellij.ide.plugins.PluginManagerCore;%0A
 impo
@@ -455,235 +455,28 @@
 lij.
-internal.statistic.utils.StatisticsUtilKt;%0Aimport com.intellij.openapi.components.*;%0Aimport com.intellij.openapi.extensions.PluginId;%0Aimport com.intellij.openapi.util.text.StringUtil;%0Aimport com.intellij.openapi.wm.ToolWindowEP
+openapi.components.*
 ;%0Aim
@@ -1029,85 +1029,8 @@
 %3E %7B%0A
-  private static final String DEFAULT_ID = %22third.party.plugin.toolwindow%22;%0A%0A
   pu
@@ -1583,65 +1583,8 @@
 n;%0A%0A
-    boolean isJB = isDevelopedByJetBrains(toolWindowId);%0A
     
@@ -1645,16 +1645,8 @@
 ame(
-(isJB ? 
 tool
@@ -1657,22 +1657,8 @@
 owId
- : DEFAULT_ID)
  + %22
@@ -1873,436 +1873,8 @@
  %7D%0A%0A
-  public static boolean isDevelopedByJetBrains(@NotNull String toolWindowId) %7B%0A    for (ToolWindowEP ep : ToolWindowEP.EP_NAME.getExtensions()) %7B%0A      if (StringUtil.equals(toolWindowId, ep.id)) %7B%0A        final PluginId id = StringUtil.isNotEmpty(ep.factoryClass) ? PluginManagerCore.getPluginByClassName(ep.factoryClass) : null;%0A        return StatisticsUtilKt.isDevelopedByJetBrains(id);%0A      %7D%0A    %7D%0A    return false;%0A  %7D%0A%0A
   pr

revert filtering for toolwindows from third-party plugins