package com.karateca.jstoolbox.config;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Andres Dominguez.
 */
public class JsToolboxConfigurable implements Configurable {

  private JsToolboxConfigurationPanel configurationPanel;
  private final JsToolboxSettings settings;

  public JsToolboxConfigurable() {
    settings = new JsToolboxSettings();
  }

  @Nls
  @Override
  public String getDisplayName() {
    return "JS Toolbox";
  }

  @Nullable
  @Override
  public String getHelpTopic() {
    return "Configure the default settings for the JS Toolbox";
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    if (configurationPanel == null) {
      configurationPanel = new JsToolboxConfigurationPanel();
    }

    // Reset on click.
    configurationPanel.getResetButton().addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent actionEvent) {
        configurationPanel.setTestSuffix(JsToolboxSettings.DEFAULT_TEST_SUFFIX);
        configurationPanel.setViewSuffix(JsToolboxSettings.DEFAULT_VIEW_SUFFIX);
        configurationPanel.setFileSuffix(JsToolboxSettings.DEFAULT_FILE_SUFFIX);
        configurationPanel.setSearchUrl(JsToolboxSettings.DEFAULT_SEARCH_URL);
        configurationPanel.setUseFilePath(
            JsToolboxSettings.DEFAULT_USE_FILE_PATH);
      }
    });

    reset();

    return configurationPanel.getMyPanel();
  }

  @Override
  public boolean isModified() {
    return !StringUtils.equals(settings.getTestSuffix(), configurationPanel.getTestSuffix()) ||
        !StringUtils.equals(settings.getViewSuffix(), configurationPanel.getViewSuffix()) ||
        !StringUtils.equals(settings.getFileSuffix(), configurationPanel.getFileSuffix()) ||
        !StringUtils.equals(settings.getSearchUrl(), configurationPanel.getSearchUrl()) ||
        settings.getUseFilePath() != configurationPanel.getUseFilePath();
  }

  @Override
  public void apply() throws ConfigurationException {
    settings.setTestSuffix(configurationPanel.getTestSuffix());
    settings.setFileSuffix(configurationPanel.getFileSuffix());
    settings.setViewSuffix(configurationPanel.getViewSuffix());
    settings.setSearchUrl(configurationPanel.getSearchUrl());
    settings.setUseFilePath(configurationPanel.getUseFilePath());
    settings.save();
  }

  @Override
  public void reset() {
    settings.load();
    configurationPanel.setTestSuffix(settings.getTestSuffix());
    configurationPanel.setViewSuffix(settings.getViewSuffix());
    configurationPanel.setFileSuffix(settings.getFileSuffix());
    configurationPanel.setSearchUrl(settings.getSearchUrl());
    configurationPanel.setUseFilePath(settings.getUseFilePath());
  }

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

@@ -1246,24 +1246,37 @@
 tTestSuffix(
+%0A            
 JsToolboxSet
@@ -1285,27 +1285,45 @@
 ngs.
-DEFAULT_TEST_SUFFIX
+Property.TestSuffix.getDefaultValue()
 );%0A 
@@ -1358,24 +1358,37 @@
 tViewSuffix(
+%0A            
 JsToolboxSet
@@ -1397,27 +1397,45 @@
 ngs.
-DEFAULT_VIEW_SUFFIX
+Property.ViewSuffix.getDefaultValue()
 );%0A 
@@ -1474,16 +1474,29 @@
 eSuffix(
+%0A            
 JsToolbo
@@ -1509,27 +1509,45 @@
 ngs.
-DEFAULT_FILE_SUFFIX
+Property.FileSuffix.getDefaultValue()
 );%0A 
@@ -1585,16 +1585,29 @@
 archUrl(
+%0A            
 JsToolbo
@@ -1620,26 +1620,44 @@
 ngs.
-DEFAULT_SEARCH_URL
+Property.SearchUrl.getDefaultValue()
 );%0A 
@@ -1714,47 +1714,217 @@
     
-JsToolboxSettings.DEFAULT_USE_FILE_PATH
+Boolean.parseBoolean(%0A                JsToolboxSettings.Property.UseFilePath.getDefaultValue()));%0A        configurationPanel.setUsePathFromDir(%0A            JsToolboxSettings.Property.FromPath.getDefaultValue()
 );%0A 
@@ -2415,24 +2415,119 @@
 chUrl()) %7C%7C%0A
+        !StringUtils.equals(settings.getFromPath(), configurationPanel.getUsePathFromDir()) %7C%7C%0A
         sett
@@ -2975,24 +2975,90 @@
 ilePath());%0A
+    settings.setFromPath(configurationPanel.getUsePathFromDir());%0A
     settings
@@ -3448,16 +3448,82 @@
 ath());%0A
+    configurationPanel.setUsePathFromDir(settings.getFromPath());%0A
   %7D%0A%0A  @

use enum. Add path value