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;

import static com.karateca.jstoolbox.config.JsToolboxSettings.Property;

/**
 * @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(
            Property.TestSuffix.getDefaultValue());
        configurationPanel.setViewSuffix(
            Property.ViewSuffix.getDefaultValue());
        configurationPanel.setFileSuffix(
            Property.FileSuffix.getDefaultValue());
        configurationPanel.setSearchUrl(
            Property.SearchUrl.getDefaultValue());
        configurationPanel.setUseFilePath(
            Boolean.parseBoolean(
                Property.UseFilePath.getDefaultValue()));
        configurationPanel.setFromPath(
            Property.FromPath.getDefaultValue());
      }
    });

    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()) ||
        !StringUtils.equals(settings.getFromPath(), configurationPanel.getFromPath()) ||
        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.setFromPath(configurationPanel.getFromPath());
    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());
    configurationPanel.setFromPath(settings.getFromPath());
  }

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

@@ -547,25 +547,16 @@
 private 
-JsToolbox
 Configur
@@ -564,32 +564,17 @@
 tion
-Panel configurationPanel
+View view
 ;%0A  
@@ -982,78 +982,41 @@
 if (
-configurationPanel == null) %7B%0A      configurationPanel = new JsToolbox
+view == null) %7B%0A      view = new 
 Conf
@@ -1024,21 +1024,20 @@
 guration
-Panel
+View
 ();%0A    
@@ -1066,34 +1066,20 @@
 ck.%0A    
-configurationPanel
+view
 .getRese
@@ -1214,34 +1214,20 @@
         
-configurationPanel
+view
 .setTest
@@ -1294,34 +1294,20 @@
         
-configurationPanel
+view
 .setView
@@ -1374,34 +1374,20 @@
         
-configurationPanel
+view
 .setFile
@@ -1454,34 +1454,20 @@
         
-configurationPanel
+view
 .setSear
@@ -1532,34 +1532,20 @@
         
-configurationPanel
+view
 .setUseF
@@ -1653,34 +1653,20 @@
         
-configurationPanel
+view
 .setFrom
@@ -1763,34 +1763,20 @@
  return 
-configurationPanel
+view
 .getMyPa
@@ -1888,34 +1888,20 @@
 ffix(), 
-configurationPanel
+view
 .getTest
@@ -1967,34 +1967,20 @@
 ffix(), 
-configurationPanel
+view
 .getView
@@ -2046,34 +2046,20 @@
 ffix(), 
-configurationPanel
+view
 .getFile
@@ -2124,34 +2124,20 @@
 hUrl(), 
-configurationPanel
+view
 .getSear
@@ -2200,34 +2200,20 @@
 Path(), 
-configurationPanel
+view
 .getFrom
@@ -2260,34 +2260,20 @@
 th() != 
-configurationPanel
+view
 .getUseF
@@ -2381,34 +2381,20 @@
 tSuffix(
-configurationPanel
+view
 .getTest
@@ -2431,34 +2431,20 @@
 eSuffix(
-configurationPanel
+view
 .getFile
@@ -2481,34 +2481,20 @@
 wSuffix(
-configurationPanel
+view
 .getView
@@ -2530,34 +2530,20 @@
 archUrl(
-configurationPanel
+view
 .getSear
@@ -2580,34 +2580,20 @@
 ilePath(
-configurationPanel
+view
 .getUseF
@@ -2629,34 +2629,20 @@
 romPath(
-configurationPanel
+view
 .getFrom
@@ -2737,34 +2737,20 @@
 ();%0A    
-configurationPanel
+view
 .setTest
@@ -2787,34 +2787,20 @@
 ));%0A    
-configurationPanel
+view
 .setView
@@ -2837,34 +2837,20 @@
 ));%0A    
-configurationPanel
+view
 .setFile
@@ -2887,34 +2887,20 @@
 ));%0A    
-configurationPanel
+view
 .setSear
@@ -2935,34 +2935,20 @@
 ));%0A    
-configurationPanel
+view
 .setUseF
@@ -2987,34 +2987,20 @@
 ));%0A    
-configurationPanel
+view
 .setFrom
@@ -3091,26 +3091,12 @@
     
-configurationPanel
+view
  = n

Use configuration view