package com.intellij.internal.statistic.updater;

import com.apple.eawt.Application;
import com.intellij.internal.statistic.connect.RemotelyConfigurableStatisticsService;
import com.intellij.internal.statistic.persistence.UsageStatisticsPersistenceComponent;
import com.intellij.internal.statistic.configurable.StatisticsConfigurable;
import com.intellij.notification.*;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.openapi.wm.ex.WindowManagerEx;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;

public class StatisticsNotificationManager {
  private StatisticsNotificationManager() {
  }

  public static void showNotification(@NotNull RemotelyConfigurableStatisticsService statisticsService, Project project) {

    Notifications.Bus.notify(new Notification("SendUsagesStatistics", getTitle(),
                                              getText(),
                                              NotificationType.INFORMATION,
                                              new MyNotificationListener(statisticsService, UsageStatisticsPersistenceComponent
                                                .getInstance())), project);
  }

  private static String getText() {
    return
      "<html>Please click <a href='allow'>I agree</a> if you want to help make "+ ApplicationNamesInfo.getInstance().getFullProductName() +
      " better or <a href='decline'>I don't agree</a> otherwise. <a href='settings'>more...</a></html>";
  }

  private static String getTitle() {
    return "Help improve "+ ApplicationNamesInfo.getInstance().getFullProductName() +" by sending anonymous usage statistics to JetBrains";
  }

  private static class MyNotificationListener implements NotificationListener {
    private RemotelyConfigurableStatisticsService myStatisticsService;
    private final UsageStatisticsPersistenceComponent mySettings;

    public MyNotificationListener(@NotNull RemotelyConfigurableStatisticsService statisticsService,
                                  @NotNull UsageStatisticsPersistenceComponent settings) {
      myStatisticsService = statisticsService;
      mySettings = settings;
    }

    @Override
    public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
      if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        final String description = event.getDescription();
        if ("allow".equals(description)) {
          mySettings.setAllowed(true);
          mySettings.setShowNotification(false);
          notification.expire();

          ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
            @Override
            public void run() {
               myStatisticsService.send();
            }
          });
        }
        else if ("decline".equals(description)) {
          mySettings.setAllowed(false);
          mySettings.setShowNotification(false);
          notification.expire();
        }
        else if ("settings".equals(description)) {
          final ShowSettingsUtil util = ShowSettingsUtil.getInstance();
          IdeFrame ideFrame = WindowManagerEx.getInstanceEx().findFrameFor(null);
          util.editConfigurable((JFrame)ideFrame, new StatisticsConfigurable(true));
          notification.expire();
        }
      }
    }
  }
}

@@ -58,30 +58,71 @@
 com.
-apple.eawt.Application
+intellij.internal.statistic.configurable.StatisticsConfigurable
 ;%0Aim
@@ -317,62 +317,88 @@
 lij.
-internal.statistic.configurable.StatisticsConfigurable
+notification.Notification;%0Aimport com.intellij.notification.NotificationListener
 ;%0Aim
@@ -428,17 +428,32 @@
 ication.
-*
+NotificationType
 ;%0Aimport
@@ -470,43 +470,100 @@
 lij.
-openapi.application.ApplicationInfo
+notification.Notifications;%0Aimport com.intellij.notification.impl.NotificationsConfiguration
 ;%0Aim
@@ -1074,16 +1074,95 @@
  %7B%0A  %7D%0A%0A
+  static %7B%0A    NotificationsConfiguration.remove(%22SendUsagesStatistics%22);%0A  %7D%0A%0A
   public
@@ -1324,26 +1324,26 @@
 cation(%22
-Send
+IDE 
 Usage
-s
+ 
 Statisti

fix group display id for IDE statistics