/*
 * Copyright 2006-2020 The MZmine Development Team
 *
 * This file is part of MZmine.
 *
 * MZmine is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * MZmine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with MZmine; if not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

package io.github.mzmine.parameters.parametertypes.selectors;

import io.github.mzmine.datamodel.features.FeatureList;
import io.github.mzmine.main.MZmineCore;
import io.github.mzmine.parameters.Parameter;
import io.github.mzmine.parameters.impl.SimpleParameterSet;
import io.github.mzmine.parameters.parametertypes.MultiChoiceParameter;
import io.github.mzmine.parameters.parametertypes.StringParameter;
import io.github.mzmine.util.ExitCode;
import javafx.collections.FXCollections;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;

public class FeatureListsComponent extends BorderPane {


  private final ComboBox<FeatureListsSelectionType> typeCombo;
  private final Button detailsButton;
  private final Label numPeakListsLabel;
  private FeatureListsSelection currentValue = new FeatureListsSelection();

  public FeatureListsComponent() {

    // setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));

    numPeakListsLabel = new Label();
    setLeft(numPeakListsLabel);

    detailsButton = new Button("...");
    detailsButton.setDisable(true);
    setRight(detailsButton);

    typeCombo = new ComboBox<>(FXCollections.observableArrayList(FeatureListsSelectionType.values()));
    typeCombo.setOnAction(e -> {
      FeatureListsSelectionType type = typeCombo.getSelectionModel().getSelectedItem();
      currentValue.setSelectionType(type);
      detailsButton.setDisable((type != FeatureListsSelectionType.NAME_PATTERN)
          && (type != FeatureListsSelectionType.SPECIFIC_FEATURELISTS));
      updateNumPeakLists();
    });
    typeCombo.getSelectionModel().selectFirst();
    setCenter(typeCombo);

    detailsButton.setOnAction(e -> {
      FeatureListsSelectionType type = typeCombo.getSelectionModel().getSelectedItem();

      if (type == FeatureListsSelectionType.SPECIFIC_FEATURELISTS) {
        final MultiChoiceParameter<FeatureList> plsParameter =
            new MultiChoiceParameter<FeatureList>("Select feature lists", "Select feature lists",
                MZmineCore.getProjectManager().getCurrentProject().getFeatureLists().toArray(FeatureList[]::new),
                currentValue.getSpecificFeatureLists());
        final SimpleParameterSet paramSet = new SimpleParameterSet(new Parameter[] {plsParameter});
        final ExitCode exitCode = paramSet.showSetupDialog(true);
        if (exitCode == ExitCode.OK) {
          FeatureList pls[] = paramSet.getParameter(plsParameter).getValue();
          currentValue.setSpecificFeatureLists(pls);
        }

      }

      if (type == FeatureListsSelectionType.NAME_PATTERN) {
        final StringParameter nameParameter = new StringParameter("Name pattern",
            "Set name pattern that may include wildcards (*), e.g. *mouse* matches any name that contains mouse",
            currentValue.getNamePattern());
        final SimpleParameterSet paramSet = new SimpleParameterSet(new Parameter[] {nameParameter});
        final ExitCode exitCode = paramSet.showSetupDialog(true);
        if (exitCode == ExitCode.OK) {
          String namePattern = paramSet.getParameter(nameParameter).getValue();
          currentValue.setNamePattern(namePattern);
        }

      }
      updateNumPeakLists();
    });


  }

  void setValue(FeatureListsSelection newValue) {
    currentValue = newValue.clone();
    FeatureListsSelectionType type = newValue.getSelectionType();
    if (type != null)
      typeCombo.getSelectionModel().select(type);
    updateNumPeakLists();
  }

  FeatureListsSelection getValue() {
    return currentValue;
  }



  public void setToolTipText(String toolTip) {
    typeCombo.setTooltip(new Tooltip(toolTip));
  }

  private void updateNumPeakLists() {
    if (currentValue.getSelectionType() == FeatureListsSelectionType.BATCH_LAST_FEATURELISTS) {
      numPeakListsLabel.setText("");
      numPeakListsLabel.setTooltip(null);
    } else {
      FeatureList pls[] = currentValue.getMatchingFeatureLists();
      if (pls.length == 1) {
        String plName = pls[0].getName();
        if (plName.length() > 22)
          plName = plName.substring(0, 20) + "...";
        numPeakListsLabel.setText(plName);
      } else {
        numPeakListsLabel.setText(pls.length + " selected");
      }
      numPeakListsLabel.setTooltip(new Tooltip(currentValue.toString()));
    }
  }
}

@@ -1938,44 +1938,8 @@
 %22);%0A
-    detailsButton.setDisable(true);%0A
     
@@ -2085,65 +2085,47 @@
 mbo.
-setOnA
+getSele
 ction
-(e -%3E %7B%0A      FeatureListsSelectionType type =
+Model().selectFirst();%0A%0A   
  typ
@@ -2143,36 +2143,33 @@
 electionModel().
-getS
+s
 electedItem();%0A 
@@ -2163,19 +2163,73 @@
 ctedItem
-();
+Property().addListener((options, oldValue, newValue) -%3E %7B
 %0A      c
@@ -2257,19 +2257,23 @@
 ionType(
-typ
+newValu
 e);%0A    
@@ -2300,19 +2300,23 @@
 isable((
-typ
+newValu
 e != Fea
@@ -2366,19 +2366,23 @@
     && (
-typ
+newValu
 e != Fea
@@ -2469,56 +2469,8 @@
 %7D);%0A
-    typeCombo.getSelectionModel().selectFirst();
 %0A   

Fix FeatureListsComponent setup button