/*
 * Copyright 2013-2013 Eugene Petrenko
 *
 * 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.eugenePetrenko.idea.dependencies.actions;

import com.eugenePetrenko.idea.dependencies.ModuleDependenciesUpdater;
import com.eugenePetrenko.idea.dependencies.ModulesDependencies;
import com.eugenePetrenko.idea.dependencies.ui.LibrariesSelectionDialog;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

/**
 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
 * Date: 08.04.13 0:22
 */
public class PostAction {
  public static void completeProcess(@NotNull final Project project, @NotNull final ModulesDependencies model) {
    final Application app = ApplicationManager.getApplication();

    if (model.isEmpty()) {
      app.invokeLater(new Runnable() {
        public void run() {
          Messages.showInfoMessage(project, "No unused dependencies were detected", "Jonnyzzz");
        }
      });
      return;
    }

    app.invokeLater(new Runnable() {
      public void run() {
        LibrariesSelectionDialog dialog = new LibrariesSelectionDialog(project, model);
        dialog.show();
        if (!dialog.isOK()) return;

        final ModulesDependencies newModel = dialog.getModel();
        if (newModel.isEmpty()) return;

        doRemoveDependencies(app, project, newModel);
      }
    });
  }

  private static void doRemoveDependencies(@NotNull final Application app,
                                           @NotNull final Project project,
                                           @NotNull final ModulesDependencies newModel) {
    ProgressManager.getInstance().run(new Task.Modal(project, "Removing Dependencies", false) {
      public void run(@NotNull final ProgressIndicator indicator) {
        ModuleDependenciesUpdater.updateModuleDependencies(project, newModel, indicator);

        app.invokeLater(new Runnable() {
          public void run() {
            app.runWriteAction(new Runnable() {
              public void run() {
                project.save();
              }
            });
          }
        });
      }
    });
  }
}

@@ -858,16 +858,162 @@
 Dialog;%0A
+import com.intellij.notification.Notification;%0Aimport com.intellij.notification.NotificationType;%0Aimport com.intellij.notification.Notifications;%0A
 import c
@@ -1049,32 +1049,32 @@
 on.Application;%0A
-
 import com.intel
@@ -1319,49 +1319,8 @@
 ct;%0A
-import com.intellij.openapi.ui.Messages;%0A
 impo
@@ -1760,92 +1760,289 @@
     
-Messages.showInfoMessage(project, %22No unused dependencies were detected%22, %22Jonnyzzz%22
+Notifications.Bus.notify(new Notification(%0A                  %22Unused Dependencies%22,%0A                  %22Remove Unused Dependencies%22,%0A                  %22No unused dependencies were detected%22,%0A                  NotificationType.INFORMATION,%0A                  null%0A          ), project
 );%0A 

fix #12