/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetbrains.edu.learning.statistics;

import com.intellij.internal.statistic.CollectUsagesException;
import com.intellij.internal.statistic.UsagesCollector;
import com.intellij.internal.statistic.beans.GroupDescriptor;
import com.intellij.internal.statistic.beans.UsageDescriptor;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.util.containers.FactoryMap;
import com.intellij.util.containers.hash.HashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Set;

public class EduUsagesCollector extends UsagesCollector {
  private static final String GROUP_ID = "educational";

  private final Map<String, UsageDescriptor> myUsageDescriptors = FactoryMap.createMap(key -> new UsageDescriptor(key, 0));

  public static void projectTypeCreated(@NotNull String projectTypeId) {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("project.created." + projectTypeId).advance();
  }

  public static void projectTypeOpened(@NotNull String projectTypeId) {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("project.opened." + projectTypeId).advance();
  }

  public static void taskChecked() {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("checkTask.").advance();
  }

  public static void hintShown() {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("showHint.").advance();
  }

  public static void taskNavigation() {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("navigateToTask.").advance();
  }

  public static void courseUploaded() {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("uploadCourse.").advance();
  }

  public static void createdCourseArchive() {
    ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get("courseArchive.").advance();
  }

  @NotNull
  @Override
  public Set<UsageDescriptor> getUsages() throws CollectUsagesException {
    HashSet<UsageDescriptor> descriptors = new HashSet<>();
    descriptors.addAll(myUsageDescriptors.values());
    myUsageDescriptors.clear();
    return descriptors;
  }


  @NotNull
  @Override
  public GroupDescriptor getGroupId() {
    return GroupDescriptor.create(GROUP_ID);
  }
}

@@ -980,68 +980,56 @@
 ers.
-FactoryMap;%0Aimport com.intellij.util.containers.hash.
+hash.HashSet;%0Aimport gnu.trove.TObjectInt
 Hash
-Set
+Map
 ;%0Aim
@@ -1073,30 +1073,8 @@
 l;%0A%0A
-import java.util.Map;%0A
 impo
@@ -1227,113 +1227,79 @@
 nal 
-Map%3CString, UsageDescriptor%3E myUsageDescriptors = FactoryMap.createMap(key -%3E new UsageDescriptor(key, 0)
+TObjectIntHashMap%3CString%3E myUsageDescriptors = new TObjectIntHashMap%3C%3E(
 );%0A%0A
@@ -1379,82 +1379,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22pr
@@ -1417,34 +1417,24 @@
 ojectTypeId)
-.advance()
 ;%0A  %7D%0A%0A  pub
@@ -1508,82 +1508,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22pr
@@ -1549,26 +1549,16 @@
 tTypeId)
-.advance()
 ;%0A  %7D%0A%0A 
@@ -1601,82 +1601,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22ch
@@ -1617,34 +1617,24 @@
 checkTask.%22)
-.advance()
 ;%0A  %7D%0A%0A  pub
@@ -1671,82 +1671,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22sh
@@ -1690,26 +1690,16 @@
 wHint.%22)
-.advance()
 ;%0A  %7D%0A%0A 
@@ -1745,82 +1745,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22na
@@ -1770,26 +1770,16 @@
 oTask.%22)
-.advance()
 ;%0A  %7D%0A%0A 
@@ -1825,82 +1825,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22up
@@ -1844,34 +1844,24 @@
 oadCourse.%22)
-.advance()
 ;%0A  %7D%0A%0A  pub
@@ -1909,82 +1909,18 @@
     
-ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors.get
+advanceKey
 (%22co
@@ -1937,18 +1937,8 @@
 e.%22)
-.advance()
 ;%0A  
@@ -2093,24 +2093,80 @@
 ashSet%3C%3E();%0A
+    myUsageDescriptors.forEachEntry((key, value) -%3E %7B%0A  
     descript
@@ -2176,14 +2176,13 @@
 .add
-All(my
+(new 
 Usag
@@ -2196,18 +2196,47 @@
 ptor
-s.values()
+(key, value));%0A      return true;%0A    %7D
 );%0A 
@@ -2407,11 +2407,269 @@
 ID);%0A  %7D
+%0A%0A  private static void advanceKey(@NotNull String key) %7B%0A    TObjectIntHashMap%3CString%3E descriptors = ServiceManager.getService(EduUsagesCollector.class).myUsageDescriptors;%0A    int oldValue = descriptors.get(key);%0A    descriptors.put(key, oldValue + 1);%0A  %7D
 %0A%7D%0A

fix compilation