package org.jetbrains.plugins.groovy.util;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nls;

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

/**
 * @author peter
 */
public abstract class SdkHomeConfigurable implements Configurable {
  private JPanel myPanel;
  private TextFieldWithBrowseButton myPathField;
  protected final Project myProject;
  protected final String myFrameworkName;

  public SdkHomeConfigurable(Project project, final String frameworkName) {
    myProject = project;
    myFrameworkName = frameworkName;
  }

  @Nls
  public String getDisplayName() {
    return myFrameworkName;
  }

  public JComponent createComponent() {
    if (myPanel != null) {
      return myPanel;
    }

    myPanel = new JPanel(new BorderLayout());
    final JPanel contentPanel = new JPanel(new BorderLayout());
    myPanel.add(contentPanel, BorderLayout.NORTH);
    contentPanel.add(new JLabel(myFrameworkName + " home:"), BorderLayout.WEST);
    myPathField = new TextFieldWithBrowseButton();
    contentPanel.add(myPathField);
    myPathField
      .addBrowseFolderListener("Select " + myFrameworkName + " home", "", myProject, new FileChooserDescriptor(false, true, false, false, false, false) {
        @Override
        public boolean isFileSelectable(VirtualFile file) {
          return isSdkHome(file);
        }
      });
    return myPanel;
  }

  protected abstract boolean isSdkHome(VirtualFile file);

  public boolean isModified() {
    return !myPathField.getText().equals(getStateText());
  }

  public void apply() throws ConfigurationException {
    final SdkHomeSettings state = new SdkHomeSettings();
    state.SDK_HOME = myPathField.getText();
    getFrameworkSettings().loadState(state);
  }

  protected abstract PersistentStateComponent<SdkHomeSettings> getFrameworkSettings();

  public void reset() {
    myPathField.setText(getStateText());
  }

  private String getStateText() {
    final SdkHomeSettings state = getFrameworkSettings().getState();
    final String stateText = state == null ? "" : state.SDK_HOME;
    return StringUtil.notNullize(stateText);
  }

  public void disposeUIResources() {
  }

  public static class SdkHomeSettings {
    public String SDK_HOME;
  }

}

@@ -374,24 +374,70 @@
 owseButton;%0A
+import com.intellij.openapi.util.io.FileUtil;%0A
 import com.i
@@ -2103,16 +2103,49 @@
 _HOME = 
+FileUtil.toSystemIndependentName(
 myPathFi
@@ -2157,16 +2157,17 @@
 etText()
+)
 ;%0A    ge
@@ -2570,16 +2570,47 @@
 Nullize(
+FileUtil.toSystemDependentName(
 stateTex
@@ -2611,16 +2611,17 @@
 ateText)
+)
 ;%0A  %7D%0A%0A 

store gant/gradle path in system-independent format => take advantage of path macros