/*
 * 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.modules.visualization.spectra.simplespectra;

import java.util.Collection;
import org.jetbrains.annotations.NotNull;
import io.github.mzmine.datamodel.IsotopePattern;
import io.github.mzmine.datamodel.MZmineProject;
import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.datamodel.Scan;
import io.github.mzmine.datamodel.features.Feature;
import io.github.mzmine.main.MZmineCore;
import io.github.mzmine.modules.MZmineModuleCategory;
import io.github.mzmine.modules.MZmineRunnableModule;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.taskcontrol.Task;
import io.github.mzmine.util.ExitCode;
import javafx.application.Platform;

/**
 * Spectrum visualizer
 */
public class SpectraVisualizerModule implements MZmineRunnableModule {

  private static final String MODULE_NAME = "Spectra visualizer";
  private static final String MODULE_DESCRIPTION = "Spectra visualizer."; // TODO

  @Override
  public @NotNull String getName() {
    return MODULE_NAME;
  }

  @Override
  public @NotNull String getDescription() {
    return MODULE_DESCRIPTION;
  }

  @Override
  @NotNull
  public ExitCode runModule(@NotNull MZmineProject project, @NotNull ParameterSet parameters,
      @NotNull Collection<Task> tasks) {
    RawDataFile dataFiles[] = parameters.getParameter(SpectraVisualizerParameters.dataFiles)
        .getValue().getMatchingRawDataFiles();

    int scanNumber = parameters.getParameter(SpectraVisualizerParameters.scanNumber).getValue();
    addNewSpectrumTab(dataFiles[0], dataFiles[0].getScanAtNumber(scanNumber));
    return ExitCode.OK;
  }

  public static SpectraVisualizerTab addNewSpectrumTab(Scan scan) {
    return addNewSpectrumTab(scan.getDataFile(), scan);
  }

  public static SpectraVisualizerTab addNewSpectrumTab(RawDataFile dataFile, Scan scan) {
    return addNewSpectrumTab(dataFile, scan, null, null, null, null);
  }

  public static SpectraVisualizerTab addNewSpectrumTab(RawDataFile dataFile, Scan scan,
      Feature peak) {
    return addNewSpectrumTab(dataFile, scan, peak, null, null, null);
  }

  public static SpectraVisualizerTab addNewSpectrumTab(RawDataFile dataFile, Scan scan,
      IsotopePattern detectedPattern) {
    return addNewSpectrumTab(dataFile, scan, null, detectedPattern, null, null);
  }

  public static SpectraVisualizerTab addNewSpectrumTab(RawDataFile dataFile, Scan scan,
      Feature peak, IsotopePattern detectedPattern, IsotopePattern predictedPattern) {
    return addNewSpectrumTab(dataFile, scan, peak, detectedPattern, predictedPattern, null);
  }

  public static SpectraVisualizerTab addNewSpectrumTab(RawDataFile dataFile, Scan scan,
      Feature peak, IsotopePattern detectedPattern, IsotopePattern predictedPattern,
      IsotopePattern spectrum) {

    assert Platform.isFxApplicationThread();

    if (scan == null) {
      MZmineCore.getDesktop()
          .displayErrorMessage("Raw data file " + dataFile + " does not contain scan #" + scan);
      return null;
    }

    SpectraVisualizerTab newTab = new SpectraVisualizerTab(dataFile, scan, true);
    newTab.loadRawData(scan);

    if (peak != null) {
      newTab.loadSinglePeak(peak);
    }

    if (detectedPattern != null) {
      newTab.loadIsotopes(detectedPattern);
    }

    if (predictedPattern != null) {
      newTab.loadIsotopes(predictedPattern);
    }

    if (spectrum != null) {
      newTab.loadSpectrum(spectrum);
    }

    // newWindow.show();
    MZmineCore.getDesktop().addTab(newTab);

    return newTab;
  }

  @Override
  public @NotNull MZmineModuleCategory getModuleCategory() {
    return MZmineModuleCategory.VISUALIZATIONRAWDATA;
  }

  @Override
  public @NotNull Class<? extends ParameterSet> getParameterSetClass() {
    return SpectraVisualizerParameters.class;
  }

}

@@ -2081,11 +2081,8 @@
 File
-s%5B%5D
  = p
@@ -2188,16 +2188,19 @@
 aFiles()
+%5B0%5D
 ;%0A%0A    i
@@ -2299,39 +2299,196 @@
     
-addNewSpectrumTab(
+Scan scan = dataFile.getScanAtNumber(scanNumber);%0A    if (scan == null) %7B%0A      assert MZmineCore.getDesktop() != null;%0A      MZmineCore.getDesktop().displayErrorMessage(%22Raw 
 data
-Files%5B0%5D,
+ file %22 +
  dat
@@ -2496,40 +2496,141 @@
 File
-s%5B0%5D.getScanAtNumber(scanNumber)
+ +%0A          %22 does not contain scan #%22 + scanNumber + %22.%22);%0A      return ExitCode.ERROR;%0A    %7D%0A%0A    addNewSpectrumTab(dataFile, scan
 );%0A 
@@ -3896,24 +3896,70 @@
  == null) %7B%0A
+      assert MZmineCore.getDesktop() != null;%0A
       MZmine
@@ -4056,30 +4056,32 @@
 contain 
-scan #%22 +
+the given
  scan
+.%22
 );%0A     

Fix SpectraVisualizerModule error messages