/*
 * Copyright 2006-2022 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.io.export_msn_tree_json;

import io.github.mzmine.datamodel.PrecursorIonTree;
import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.parameters.parametertypes.tolerances.MZTolerance;
import io.github.mzmine.taskcontrol.AbstractTask;
import io.github.mzmine.util.scans.ScanUtils;
import java.io.File;
import java.time.Instant;
import java.util.List;

public class MSnTreeJsonExportTask extends AbstractTask {


  private final File outFile;
  private final RawDataFile[] raws;
  private final MZTolerance mzTol;
  private final int total = 0;
  private final int done = 0;
  private String description = "Exporting raw files as MSn trees to jsonlines file";

  public MSnTreeJsonExportTask(ParameterSet parameters, Instant moduleCallDate) {
    super(null, moduleCallDate);
    outFile = parameters.getValue(MSnTreeJsonExportParameters.FILENAME);
    raws = parameters.getValue(MSnTreeJsonExportParameters.RAW_FILES).getMatchingRawDataFiles();
    mzTol = parameters.getValue(MSnTreeJsonExportParameters.MZ_TOL);
    description = String.format("Exporting %d raw files as MSn trees to jsonlines file %s",
        raws.length, outFile.getAbsolutePath());
  }

  @Override
  public String getTaskDescription() {
    return description;
  }

  @Override
  public double getFinishedPercentage() {
    return total == 0 ? 0 : done / (double) total;
  }

  @Override
  public void run() {
    for (RawDataFile raw : raws) {
      final List<PrecursorIonTree> trees = ScanUtils.getMSnFragmentTrees(raw);
      for (PrecursorIonTree tree : trees) {
        tree.getRoot().getFragmentScans()
      }
    }
  }
}

@@ -923,27 +923,123 @@
 tamodel.
-RawDataFile
+PrecursorIonTreeNode;%0Aimport io.github.mzmine.datamodel.RawDataFile;%0Aimport io.github.mzmine.datamodel.Scan
 ;%0Aimport
@@ -1205,16 +1205,64 @@
 ctTask;%0A
+import io.github.mzmine.taskcontrol.TaskStatus;%0A
 import i
@@ -1346,16 +1346,44 @@
 nstant;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1565,30 +1565,24 @@
 ;%0A  private 
-final 
 int total = 
@@ -1598,22 +1598,16 @@
 ate 
-final 
 int done
  = 0
@@ -1602,20 +1602,16 @@
 int done
- = 0
 ;%0A  priv
@@ -2410,24 +2410,88 @@
 oid run() %7B%0A
+    setStatus(TaskStatus.PROCESSING);%0A    total = raws.length;%0A%0A
     for (Raw
@@ -2648,22 +2648,322 @@
     
-tree.getRoot()
+List%3CString%3E lines = treeNodeToCSV(raw, tree.getRoot(), new ArrayList%3C%3E());%0A      %7D%0A%0A      done++;%0A    %7D%0A%0A    setStatus(TaskStatus.FINISHED);%0A  %7D%0A%0A  private List%3CString%3E treeNodeToCSV(RawDataFile raw, PrecursorIonTreeNode treeNode,%0A      List%3CString%3E lines) %7B%0A    // export all spectra%0A    for (var spec : treeNode
 .get
@@ -2981,22 +2981,280 @@
 ns()
+) %7B
 %0A      
-%7D%0A    %7D
+spectrumToCSV(raw, spec, lines);%0A    %7D%0A%0A    // add children%0A    for (var child : treeNode.getChildPrecursors()) %7B%0A      treeNodeToCSV(raw, child, lines);%0A    %7D%0A    return lines;%0A  %7D%0A%0A  private void spectrumToCSV(RawDataFile raw, Scan spec, List%3CString%3E lines) %7B%0A
 %0A  %7D

change export