/*
 * Copyright 2000-2013 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.application.options.emmet;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator;
import com.intellij.codeInsight.template.impl.TemplateSettings;
import com.intellij.openapi.options.CompositeConfigurable;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.UnnamedConfigurable;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.xml.XmlBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

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

/**
 * User: zolotov
 * Date: 9/24/13
 */
public class EmmetCompositeConfigurable extends CompositeConfigurable<UnnamedConfigurable> implements Configurable.NoScroll {
  private JPanel myRootPanel;
  private JPanel myGeneratorSettingsPanel;
  private JComboBox myEmmetExpandShortcutCombo;

  private static final String SPACE = CodeInsightBundle.message("template.shortcut.space");
  private static final String TAB = CodeInsightBundle.message("template.shortcut.tab");
  private static final String ENTER = CodeInsightBundle.message("template.shortcut.enter");

  public EmmetCompositeConfigurable() {
    myEmmetExpandShortcutCombo.addItem(SPACE);
    myEmmetExpandShortcutCombo.addItem(TAB);
    myEmmetExpandShortcutCombo.addItem(ENTER);
  }

  @Nls
  @Override
  public String getDisplayName() {
    return XmlBundle.message("emmet.configuration.title");
  }

  @Nullable
  @Override
  public String getHelpTopic() {
    return "reference.idesettings.emmet";
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    final List<UnnamedConfigurable> configurables = getConfigurables();
    myGeneratorSettingsPanel.setLayout(new GridLayoutManager(configurables.size(), 1, new Insets(0, 0, 10, 0), -1, -1));
    for (int i = 0; i < configurables.size(); i++) {
      UnnamedConfigurable configurable = configurables.get(i);
      final JComponent component = configurable.createComponent();
      myGeneratorSettingsPanel.add(component,
                                   new GridConstraints(i, 0, 1, 1, 0, GridConstraints.FILL_HORIZONTAL, 
                                                       GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK,
                                                       GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK, 
                                                       new Dimension(-1, -1),  new Dimension(-1, -1), new Dimension(-1, -1)));
    }
    myGeneratorSettingsPanel.revalidate();
    myRootPanel.revalidate();
    return myRootPanel;
  }

  @Override
  public void reset() {
    final EmmetOptions emmetOptions = EmmetOptions.getInstance();
    char shortcut = (char)emmetOptions.getEmmetExpandShortcut();
    if (shortcut == TemplateSettings.TAB_CHAR) {
      myEmmetExpandShortcutCombo.setSelectedItem(TAB);
    }
    else if (shortcut == TemplateSettings.ENTER_CHAR) {
      myEmmetExpandShortcutCombo.setSelectedItem(ENTER);
    }
    else {
      myEmmetExpandShortcutCombo.setSelectedItem(SPACE);
    }
    super.reset();
  }

  @Override
  public void apply() throws ConfigurationException {
    final EmmetOptions emmetOptions = EmmetOptions.getInstance();
    emmetOptions.setEmmetExpandShortcut(getSelectedEmmetExpandShortcut());
    super.apply();
  }

  @Override
  public boolean isModified() {
    return EmmetOptions.getInstance().getEmmetExpandShortcut() != getSelectedEmmetExpandShortcut() || super.isModified();
  }

  @Override
  public void disposeUIResources() {
    myGeneratorSettingsPanel.removeAll();
    super.disposeUIResources();
  }

  @Override
  protected List<UnnamedConfigurable> createConfigurables() {
    List<UnnamedConfigurable> configurables = ContainerUtil.newArrayList();
    for (ZenCodingGenerator zenCodingGenerator : ZenCodingGenerator.getInstances()) {
      ContainerUtil.addIfNotNull(configurables, zenCodingGenerator.createConfigurable());
    }
    return configurables;
  }

  private char getSelectedEmmetExpandShortcut() {
    Object selectedItem = myEmmetExpandShortcutCombo.getSelectedItem();
    if (TAB.equals(selectedItem)) {
      return TemplateSettings.TAB_CHAR;
    }
    else if (ENTER.equals(selectedItem)) {
      return TemplateSettings.ENTER_CHAR;
    }
    return TemplateSettings.SPACE_CHAR;
  }
}

@@ -876,196 +876,9 @@
 ons.
-CompositeConfigurable;%0Aimport com.intellij.openapi.options.Configurable;%0Aimport com.intellij.openapi.options.ConfigurationException;%0Aimport com.intellij.openapi.options.UnnamedConfigurable
+*
 ;%0Aim
@@ -1107,16 +1107,58 @@
 ns.Nls;%0A
+import org.jetbrains.annotations.NotNull;%0A
 import o
@@ -1300,16 +1300,16 @@
 /13%0A */%0A
-
 public c
@@ -1423,16 +1423,142 @@
 NoScroll
+,%0A                                                                                                      SearchableConfigurable
  %7B%0A  pri
@@ -2321,37 +2321,15 @@
 urn 
-%22reference.idesettings.emmet%22
+getId()
 ;%0A  
@@ -5185,11 +5185,201 @@
 HAR;%0A  %7D
+%0A%0A  @NotNull%0A  @Override%0A  public String getId() %7B%0A    return %22reference.idesettings.emmet%22;%0A  %7D%0A%0A  @Nullable%0A  @Override%0A  public Runnable enableSearch(String option) %7B%0A    return null;%0A  %7D
 %0A%7D%0A

Fix ConfigurablesModifiedTest.testSearchableConfigurables