// 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.ide.plugins;

import com.intellij.ide.ui.UINumericRange;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * @author Alexander Lobas
 */
public class PluginManagerConfigurableProxy
  implements SearchableConfigurable, Configurable.NoScroll, Configurable.NoMargin, Configurable.TopComponentProvider {
  private final SearchableConfigurable myConfigurable;

  public PluginManagerConfigurableProxy() {
    if (Registry.is("show.new.layout.plugin.page", false)) {
      myConfigurable = new PluginManagerConfigurableNewLayout();
    }
    else if (Registry.is("show.new.plugin.page", false)) {
      myConfigurable = new PluginManagerConfigurableNew();
    }
    else {
      myConfigurable = new PluginManagerConfigurable(PluginManagerUISettings.getInstance());
    }
  }

  @NotNull
  @Override
  public String getId() {
    return myConfigurable.getId();
  }

  @Nullable
  @Override
  public Runnable enableSearch(String option) {
    return myConfigurable.enableSearch(option);
  }

  @Nls(capitalization = Nls.Capitalization.Title)
  @Override
  public String getDisplayName() {
    return myConfigurable.getDisplayName();
  }

  @Nullable
  @Override
  public String getHelpTopic() {
    return myConfigurable.getHelpTopic();
  }

  @Override
  public boolean isModified(@NotNull JTextField textField, @NotNull String value) {
    return myConfigurable.isModified(textField, value);
  }

  @Override
  public boolean isModified(@NotNull JTextField textField, int value, @NotNull UINumericRange range) {
    return myConfigurable.isModified(textField, value, range);
  }

  @Override
  public boolean isModified(@NotNull JToggleButton toggleButton, boolean value) {
    return myConfigurable.isModified(toggleButton, value);
  }

  @Override
  public <T> boolean isModified(@NotNull ComboBox<T> comboBox, T value) {
    return myConfigurable.isModified(comboBox, value);
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myConfigurable.getPreferredFocusedComponent();
  }

  @Override
  public boolean isAvailable() {
    return myConfigurable instanceof Configurable.TopComponentProvider;
  }

  @NotNull
  @Override
  public Component getCenterComponent(@NotNull TopComponentController controller) {
    return ((Configurable.TopComponentProvider)myConfigurable).getCenterComponent(controller);
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    JComponent component = myConfigurable.createComponent();
    if (component != null && myConfigurable instanceof PluginManagerConfigurable) {
      if (!component.getClass().equals(JPanel.class)) {
        // some custom components do not support borders
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(BorderLayout.CENTER, component);
        component = panel;
      }
      component.setBorder(JBUI.Borders.empty(5, 10, 10, 10));
    }
    return component;
  }

  @Override
  public boolean isModified() {
    return myConfigurable.isModified();
  }

  @Override
  public void apply() throws ConfigurationException {
    myConfigurable.apply();
  }

  @Override
  public void reset() {
    myConfigurable.reset();
  }

  @Override
  public void disposeUIResources() {
    myConfigurable.disposeUIResources();
  }

  public void select(@NotNull IdeaPluginDescriptor... descriptors) {
    if (myConfigurable instanceof PluginManagerConfigurableInfo) {
      ((PluginManagerConfigurableInfo)myConfigurable).select(descriptors);
    }
    else {
      ((PluginManagerConfigurable)myConfigurable).select(descriptors);
    }
  }

  public void enable(@NotNull IdeaPluginDescriptor... descriptors) {
    if (myConfigurable instanceof PluginManagerConfigurableInfo) {
      ((PluginManagerConfigurableInfo)myConfigurable).getPluginModel().changeEnableDisable(descriptors, true);
    }
    else {
      ((InstalledPluginsTableModel)((PluginManagerConfigurable)myConfigurable).getOrCreatePanel().getPluginsModel())
        .enableRows(descriptors, Boolean.TRUE);
    }
  }

  public static void showPluginConfigurableAndEnable(@Nullable Project project, @NotNull IdeaPluginDescriptor... descriptors) {
    PluginManagerConfigurableProxy configurable = new PluginManagerConfigurableProxy();
    ShowSettingsUtil.getInstance().editConfigurable(project, configurable, () -> {
      configurable.enable(descriptors);
      configurable.select(descriptors);
    });
  }

  public static void showPluginConfigurable(@Nullable Component parent,
                                            @Nullable Project project,
                                            @NotNull IdeaPluginDescriptor... descriptors) {
    PluginManagerConfigurableProxy configurable = new PluginManagerConfigurableProxy();
    Runnable init = () -> configurable.select(descriptors);
    ShowSettingsUtil util = ShowSettingsUtil.getInstance();

    if (parent != null) {
      util.editConfigurable(parent, configurable, init);
    }
    else {
      util.editConfigurable(project, configurable, init);
    }
  }
}
@@ -526,94 +526,8 @@
 ox;%0A
-import com.intellij.openapi.util.registry.Registry;%0Aimport com.intellij.util.ui.JBUI;%0A
 impo
@@ -906,158 +906,42 @@
 nal 
-SearchableConfigurable myConfigurable;%0A%0A  public PluginManagerConfigurableProxy() %7B%0A    if (Registry.is(%22show.new.layout.plugin.page%22, false)) %7B%0A     
+PluginManagerConfigurableNewLayout
  myC
@@ -943,38 +943,43 @@
 t myConfigurable
- = new
+;%0A%0A  public
  PluginManagerCo
@@ -992,88 +992,18 @@
 able
-NewLayout();%0A    %7D%0A    else if (Registry.is(%22show.new.plugin.page%22, false)
+Proxy(
 ) %7B%0A
-  
     
@@ -1055,127 +1055,17 @@
 eNew
-();%0A    %7D%0A    else %7B%0A      myConfigurable = new PluginManagerConfigurable(PluginManagerUISettings.getInstance());%0A    %7D
+Layout();
 %0A  %7D
@@ -2362,67 +2362,12 @@
 urn 
-myConfigurable instanceof Configurable.TopComponentProvider
+true
 ;%0A  
@@ -2705,414 +2705,8 @@
 ();%0A
-    if (component != null && myConfigurable instanceof PluginManagerConfigurable) %7B%0A      if (!component.getClass().equals(JPanel.class)) %7B%0A        // some custom components do not support borders%0A        JPanel panel = new JPanel(new BorderLayout());%0A        panel.add(BorderLayout.CENTER, component);%0A        component = panel;%0A      %7D%0A      component.setBorder(JBUI.Borders.empty(5, 10, 10, 10));%0A    %7D%0A
     
@@ -3145,36 +3145,32 @@
 criptors) %7B%0A    
-if (
 myConfigurable i
@@ -3171,221 +3171,29 @@
 able
- instanceof PluginManagerConfigurableInfo) %7B%0A      ((PluginManagerConfigurableInfo)myConfigurable).select(descriptors);%0A    %7D%0A    else %7B%0A      ((PluginManagerConfigurable)myConfigurable).select(descriptors);%0A    %7D
+.select(descriptors);
 %0A  %7D
@@ -3267,20 +3267,16 @@
 ) %7B%0A    
-if (
 myConfig
@@ -3285,351 +3285,65 @@
 able
- instanceof PluginManagerConfigurableInfo) %7B%0A      ((PluginManagerConfigurableInfo)myConfigurable).getPluginModel().changeEnableDisable(descriptors, true);%0A    %7D%0A    else %7B%0A      ((InstalledPluginsTableModel)((PluginManagerConfigurable)myConfigurable).getOrCreatePanel().getPluginsModel())%0A        .enableRows(descriptors, Boolean.TRUE);%0A    %7D
+.getPluginModel().changeEnableDisable(descriptors, true);
 %0A  %7D

Remove support for old configurable UIs