package org.jetbrains.postfixCompletion.settings;

import com.intellij.application.options.editor.EditorOptionsProvider;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.postfixCompletion.templates.PostfixTemplate;

import javax.swing.*;
import java.util.Arrays;
import java.util.Map;

public class PostfixCompletionConfigurable implements SearchableConfigurable, EditorOptionsProvider, Configurable.NoScroll {
  private static final Logger LOG = Logger.getInstance(PostfixCompletionConfigurable.class);

  @Nullable 
  private PostfixTemplatesListPanel myPanel;

  @NotNull
  @Override
  public String getId() {
    return "reference.settingsdialog.IDE.editor.postfix.completion";
  }

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

  @Nls
  @Override
  public String getDisplayName() {
    return "Postfix Completion";
  }

  @Nullable
  public PostfixTemplatesListPanel getTemplatesListPanel() {
    if (myPanel == null) {
      createComponent();
    }
    return myPanel;
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    if (myPanel == null) {
      final PostfixTemplate[] templates = PostfixTemplate.EP_NAME.getExtensions();

      PostfixCompletionSettings templatesSettings = PostfixCompletionSettings.getInstance();
      if (templatesSettings == null) {
        LOG.error("Can't retrieve postfix template settings");
        return null;
      }

      myPanel = new PostfixTemplatesListPanel(Arrays.asList(templates));
    }

    return myPanel.getComponent();
  }

  @Override
  public void apply() throws ConfigurationException {
    if (myPanel != null) {
      PostfixCompletionSettings templatesSettings = PostfixCompletionSettings.getInstance();
      if (templatesSettings != null) {
        Map<String, Boolean> newTemplatesState = ContainerUtil.newHashMap();
        for (Map.Entry<String, Boolean> entry : myPanel.getState().entrySet()) {
          Boolean value = entry.getValue();
          if (value != null && !value) {
            newTemplatesState.put(entry.getKey(), entry.getValue());
          }
        }

        templatesSettings.setTemplatesState(newTemplatesState);
      }
    }
  }

  @Override
  public void reset() {
    if (myPanel != null) {
      PostfixCompletionSettings templatesSettings = PostfixCompletionSettings.getInstance();
      if (templatesSettings != null) {
        myPanel.setState(templatesSettings.getTemplatesState());
      }
    }
  }

  @Override
  public void disposeUIResources() {
    myPanel = null;
  }

  @Override
  public boolean isModified() {
    PostfixCompletionSettings templatesSettings = PostfixCompletionSettings.getInstance();
    if (templatesSettings == null) return false;

    return myPanel != null && !myPanel.getState().equals(templatesSettings.getTemplatesState());
  }

  @Nullable
  @Override
  public Runnable enableSearch(String s) {
    return null;
  }
}
@@ -912,16 +912,29 @@
 Panel my
+TemplatesList
 Panel;%0A%0A
@@ -1302,32 +1302,45 @@
 l() %7B%0A    if (my
+TemplatesList
 Panel == null) %7B
@@ -1380,24 +1380,37 @@
    return my
+TemplatesList
 Panel;%0A  %7D%0A%0A
@@ -1479,24 +1479,37 @@
 %7B%0A    if (my
+TemplatesList
 Panel == nul
@@ -1826,24 +1826,37 @@
  %7D%0A%0A      my
+TemplatesList
 Panel = new 
@@ -1924,24 +1924,37 @@
    return my
+TemplatesList
 Panel.getCom
@@ -2036,32 +2036,45 @@
 ion %7B%0A    if (my
+TemplatesList
 Panel != null) %7B
@@ -2333,16 +2333,29 @@
 try : my
+TemplatesList
 Panel.ge
@@ -2683,16 +2683,29 @@
   if (my
+TemplatesList
 Panel !=
@@ -2851,24 +2851,37 @@
 %7B%0A        my
+TemplatesList
 Panel.setSta
@@ -2997,16 +2997,29 @@
 %7B%0A    my
+TemplatesList
 Panel = 
@@ -3227,16 +3227,29 @@
 eturn my
+TemplatesList
 Panel !=
@@ -3260,16 +3260,29 @@
 l && !my
+TemplatesList
 Panel.ge

Rename panel