package jetbrains.mps.baseLanguage.unitTest.execution.settings;

/*Generated by MPS */

import javax.swing.JComponent;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBCheckBox;
import jetbrains.mps.execution.lib.ui.FieldWithPathChooseDialog;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Box;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import java.awt.BorderLayout;

/**
 * extends JComponent only of idiotic logic in the editor part of run configurations, where you must pass JComponent with no context
 */
public final class JUnitConfigurationEditorComponent extends JComponent {
  private final JBPanel myPanel = new JBPanel();
  private final ScopeAndPanels myScopeAndPanels;
  private final JBCheckBox myReuseCachesCheckBox = new JBCheckBox("Reuse caches", true);
  private final JBCheckBox myOverrideCachesCheckBox = new JBCheckBox("Override the default settings location:");
  private final FieldWithPathChooseDialog myCachesDir = new FieldWithPathChooseDialog(new FileChooserDescriptor(false, true, false, false, false, false));

  private JBPanel createSaveCachesPanel() {
    JBPanel saveCachesPanel = new JBPanel();
    saveCachesPanel.setLayout(new BoxLayout(saveCachesPanel, BoxLayout.X_AXIS));
    saveCachesPanel.add(myOverrideCachesCheckBox);
    myOverrideCachesCheckBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent p0) {
        syncCachesDirWithCheckBoxes(myScopeAndPanels.getCurrentInProcessCheckBox());
      }
    });
    saveCachesPanel.add(Box.createHorizontalGlue());
    saveCachesPanel.add(myCachesDir);
    return saveCachesPanel;
  }

  public JUnitConfigurationEditorComponent(@NotNull Project project) {
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
    myScopeAndPanels = new ScopeAndPanels(project);
    myPanel.add(myScopeAndPanels.getPanel());
    myScopeAndPanels.addSelectionChangeListener(new ScopeAndPanels.SelectionChangeListener() {
      @Override
      public void selectionChanged() {
        myScopeAndPanels.getCurrentInProcessCheckBox().forceUpdate();
      }
    });
    JBPanel checkboxPanel = new JBPanel(new BorderLayout());
    checkboxPanel.add(myReuseCachesCheckBox, BorderLayout.WEST);
    myPanel.add(checkboxPanel);
    myPanel.add(createSaveCachesPanel());
    init();
  }

  private void syncCachesDirWithCheckBoxes(InProcessJBCheckBox checkbox) {
    myCachesDir.setEnabled(myOverrideCachesCheckBox.isSelected() & !(checkbox.isSelected()));
  }

  private void init() {
    for (final InProcessJBCheckBox checkbox : myScopeAndPanels.getInProcessCheckBoxes()) {
      checkbox.registerDisableIffSelectedUpdater(myReuseCachesCheckBox);
      checkbox.registerDisableIffSelectedUpdater(myOverrideCachesCheckBox);
      InProcessJBCheckBox.Updater cachesDirUpdater = new InProcessJBCheckBox.Updater() {
        @Override
        public void updateMe() {
          syncCachesDirWithCheckBoxes(checkbox);
        }
      };
      checkbox.registerUpdater(cachesDirUpdater);
      checkbox.forceUpdate();
    }
  }

  /**
   * Disable supplied component when 'in-process' setting has been activated.
   * Intended for use from run configurations that use JUnit settings along with other settings that may need to reflect 
   * state of the 'in-process' setting.
   */
  public void disableComponentWhenInProcess(JComponent... component) {
    for (final InProcessJBCheckBox checkbox : myScopeAndPanels.getInProcessCheckBoxes()) {
      for (JComponent c : component) {
        checkbox.registerDisableIffSelectedUpdater(c);
      }
      checkbox.forceUpdate();
    }
  }


  public void apply(JUnitSettings_Configuration configuration) {
    myScopeAndPanels.apply(configuration);
    configuration.setReuseCaches(myReuseCachesCheckBox.isSelected());
    boolean overrideCaches = myOverrideCachesCheckBox.isSelected();
    configuration.setOverrideCachesLocation(overrideCaches);
    configuration.setCachesPath(myCachesDir.getText());
  }

  public void reset(JUnitSettings_Configuration settings) {
    myScopeAndPanels.reset(settings);
    myCachesDir.setText(settings.getSettingsLocation());
    updateCheckBoxes(settings);
  }

  private void updateCheckBoxes(JUnitSettings_Configuration configuration) {
    InProcessJBCheckBox checkBox = myScopeAndPanels.getCurrentInProcessCheckBox();
    checkBox.setSelected(configuration.getInProcessProperty());
    myReuseCachesCheckBox.setSelected(configuration.getReuseCaches());
    myOverrideCachesCheckBox.setSelected(configuration.getOverrideCachesLocation());
    checkBox.forceUpdate();
  }

  public JBPanel getPanel() {
    return myPanel;
  }

  public void dispose() {
    myScopeAndPanels.dispose();
  }
}
