/*
 * Copyright 2000-2015 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.intellij.openapi.updateSettings.impl;

import com.intellij.ide.IdeBundle;
import com.intellij.ide.plugins.PluginManagerMain;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.progress.PerformInBackgroundOption;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.util.Condition;
import com.intellij.ui.TableUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;
import java.util.Set;

/**
 * @author pti
 */
class PluginUpdateInfoDialog extends AbstractUpdateDialog {
  private final Collection<PluginDownloader> myUploadedPlugins;
  private final boolean myPlatformUpdate;

  public PluginUpdateInfoDialog(Collection<PluginDownloader> uploadedPlugins, boolean enableLink) {
    super(enableLink);
    myUploadedPlugins = uploadedPlugins;
    myPlatformUpdate = false;
    init();
  }

  /**
   * Used from {@link UpdateInfoDialog} when both platform and plugin updates are available.
   */
  PluginUpdateInfoDialog(Component parent, @NotNull Collection<PluginDownloader> updatePlugins) {
    super(parent, false);
    myUploadedPlugins = updatePlugins;
    myPlatformUpdate = true;
    init();
  }

  @Override
  protected JComponent createCenterPanel() {
    return new PluginUpdateInfoPanel().myPanel;
  }

  @Override
  protected String getOkButtonText() {
    if (!myPlatformUpdate) {
      return IdeBundle.message("update.plugins.update.action");
    }
    else {
      boolean canRestart = ApplicationManager.getApplication().isRestartCapable();
      return IdeBundle.message(canRestart ? "update.restart.plugins.update.action" : "update.shutdown.plugins.update.action");
    }
  }

  @Override
  protected void doOKAction() {
    super.doOKAction();

    if (myPlatformUpdate) {
      ProgressManager.getInstance().run(new Task.Modal(null, IdeBundle.message("progress.downloading.plugins"), true) {
        @Override
        public void run(@NotNull ProgressIndicator indicator) {
          UpdateChecker.saveDisabledToUpdatePlugins();
          UpdateChecker.installPluginUpdates(myUploadedPlugins, indicator);
        }
      });
    }
    else {
      ProgressManager.getInstance().run(new Task.Backgroundable(null, IdeBundle.message("progress.downloading.plugins"), true, PerformInBackgroundOption.DEAF) {
        @Override
        public void run(@NotNull ProgressIndicator indicator) {
          UpdateChecker.saveDisabledToUpdatePlugins();
          boolean updated = UpdateChecker.installPluginUpdates(myUploadedPlugins, indicator);
          if (updated) {
            ApplicationManager.getApplication().invokeLater(new Runnable() {
              @Override
              public void run() {
                PluginManagerMain.notifyPluginsUpdated(null);
              }
            }, ModalityState.NON_MODAL);
          }
        }
      });
    }
  }

  private class PluginUpdateInfoPanel {
    private JPanel myPanel;
    private JLabel myPluginsToUpdateLabel;
    private JPanel myPluginsPanel;
    private JEditorPane myMessageArea;

    public PluginUpdateInfoPanel() {
      myPluginsToUpdateLabel.setVisible(true);
      myPluginsPanel.setVisible(true);

      final DetectedPluginsPanel foundPluginsPanel = new DetectedPluginsPanel();
      foundPluginsPanel.addAll(myUploadedPlugins);
      TableUtil.ensureSelectionExists(foundPluginsPanel.getEntryTable());
      foundPluginsPanel.addStateListener(new DetectedPluginsPanel.Listener() {
        @Override
        public void stateChanged() {
          final Set<String> skipped = foundPluginsPanel.getSkippedPlugins();
          final PluginDownloader any = ContainerUtil.find(myUploadedPlugins, new Condition<PluginDownloader>() {
            @Override
            public boolean value(PluginDownloader plugin) {
              return !skipped.contains(plugin.getPluginId());
            }
          });
          getOKAction().setEnabled(any != null);
        }
      });
      myPluginsPanel.add(foundPluginsPanel, BorderLayout.CENTER);

      configureMessageArea(myMessageArea);
    }
  }
}

@@ -21,9 +21,9 @@
 -201
-5
+6
  Jet
@@ -1087,111 +1087,16 @@
 lij.
-openapi.util.Condition;%0Aimport com.intellij.ui.TableUtil;%0Aimport com.intellij.util.containers.Container
+ui.Table
 Util
@@ -3388,98 +3388,13 @@
 ter(
-new Runnable() %7B%0A              @Override%0A              public void run() %7B%0A               
+() -%3E
  Plu
@@ -3438,39 +3438,8 @@
 ull)
-;%0A              %7D%0A            %7D
 , Mo
@@ -3821,22 +3821,16 @@
 %0A%0A      
-final 
 Detected
@@ -4060,116 +4060,267 @@
 ner(
-new DetectedPluginsPanel.Listener() %7B%0A        @Override
+() -%3E updateState(foundPluginsPanel));%0A      myPluginsPanel.add(foundPluginsPanel, BorderLayout.CENTER);%0A%0A      configureMessageArea(myMessageArea);%0A%0A      updateState(foundPluginsPanel);
 %0A    
+%7D%0A%0A
     p
-ublic void stateChanged() %7B%0A          final
+rivate void updateState(DetectedPluginsPanel panel) %7B%0A     
  Set
@@ -4338,29 +4338,17 @@
 ipped = 
-foundPluginsP
+p
 anel.get
@@ -4375,463 +4375,168 @@
     
-   
- final PluginDownloader any = ContainerUtil.find(myUploadedPlugins, new Condition%3CPluginDownloader%3E() %7B%0A            @Override%0A            public boolean value(PluginDownloader plugin) %7B%0A              return !skipped.contains(plugin.getPluginId());%0A            %7D%0A          %7D);%0A          getOKAction().setEnabled(any != null);%0A        %7D%0A      %7D);%0A      myPluginsPanel.add(foundPluginsPanel, BorderLayout.CENTER);%0A%0A      configureMessageArea(myMessageArea
+boolean nothingSelected = myUploadedPlugins.stream().allMatch(plugin -%3E skipped.contains(plugin.getPluginId()));%0A      getOKAction().setEnabled(!nothingSelected
 );%0A 
@@ -4545,9 +4545,8 @@
  %7D%0A  %7D%0A%7D
-%0A

correct initial state of plugin update info dialog