package io.github.mzmine.modules.visualization.compdb;

import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.datamodel.features.FeatureList;
import io.github.mzmine.datamodel.features.ModularFeatureListRow;
import io.github.mzmine.datamodel.features.compoundannotations.CompoundDBAnnotation;
import io.github.mzmine.gui.mainwindow.MZmineTab;
import io.github.mzmine.main.MZmineCore;
import io.github.mzmine.modules.visualization.featurelisttable_modular.FeatureTableFX;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javafx.collections.ListChangeListener;
import javafx.geometry.Orientation;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.Separator;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.GridPane;
import org.jetbrains.annotations.NotNull;

public class CompoundDatabaseMatchTab extends MZmineTab {
  private final FeatureTableFX table;
  private final ScrollPane scrollPane;

  public static void addNewTab(final FeatureTableFX table) {
    MZmineCore.runLater(() -> {
      final CompoundDatabaseMatchTab tab = new CompoundDatabaseMatchTab(table);
      tab.selectionChanged();
      MZmineCore.getDesktop().addTab(tab);
    });
  }

  public CompoundDatabaseMatchTab(FeatureTableFX table) {
    super("Compound database matches", true, true);
    this.table = table;
    scrollPane = new ScrollPane();
    scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
    scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
    setContent(scrollPane);

    final ListChangeListener<TreeItem<ModularFeatureListRow>> listener = c -> selectionChanged();
    table.getSelectionModel().getSelectedItems().addListener(listener);
    setOnClosed(e -> table.getSelectionModel().getSelectedItems().removeListener(listener));
  }

  private void selectionChanged() {
    final ModularFeatureListRow selectedRow = table.getSelectedRow();
    GridPane pane = new GridPane();
    final List<CompoundDBAnnotation> compoundAnnotations = selectedRow.getCompoundAnnotations();
    int i = 0;
    for (CompoundDBAnnotation annotation : compoundAnnotations) {
      final CompoundDatabaseMatchPane matchPane = new CompoundDatabaseMatchPane(annotation,
          selectedRow);
      pane.add(matchPane, 0, i++);
      pane.add(new Separator(Orientation.HORIZONTAL), 0, i++);
    }
    scrollPane.setContent(pane);
  }

  @Override
  public @NotNull Collection<? extends RawDataFile> getRawDataFiles() {
    return Collections.emptyList();
  }

  @Override
  public @NotNull Collection<? extends FeatureList> getFeatureLists() {
    return Collections.emptyList();
  }

  @Override
  public @NotNull Collection<? extends FeatureList> getAlignedFeatureLists() {
    return Collections.emptyList();
  }

  @Override
  public void onRawDataFileSelectionChanged(Collection<? extends RawDataFile> rawDataFiles) {

  }

  @Override
  public void onFeatureListSelectionChanged(Collection<? extends FeatureList> featureLists) {

  }

  @Override
  public void onAlignedFeatureListSelectionChanged(Collection<? extends FeatureList> featureLists) {

  }
}

@@ -299,24 +299,225 @@
 Annotation;%0A
+import io.github.mzmine.datamodel.features.types.DataType;%0Aimport io.github.mzmine.datamodel.features.types.ListWithSubsType;%0Aimport io.github.mzmine.datamodel.features.types.modifiers.AnnotationType;%0A
 import io.gi
@@ -682,16 +682,44 @@
 ableFX;%0A
+import java.util.ArrayList;%0A
 import j
@@ -2240,24 +2240,25 @@
 GridPane();%0A
+%0A
     final Li
@@ -2308,70 +2308,596 @@
 s = 
-selectedRow.getCompoundAnnotations();%0A    int i = 0;%0A    for (
+new ArrayList%3C%3E();%0A    final Collection%3CDataType%3E dataTypes = selectedRow.getTypes().values();%0A    for (DataType dataType : dataTypes) %7B%0A      if (dataType instanceof ListWithSubsType%3C?%3E listType && dataType instanceof AnnotationType) %7B%0A        final List%3C?%3E list = selectedRow.get(listType);%0A        if (list != null && !list.isEmpty()) %7B%0A          list.stream().filter(c -%3E c instanceof CompoundDBAnnotation)%0A              .forEach(c -%3E compoundAnnotations.add((CompoundDBAnnotation) c));%0A        %7D%0A      %7D%0A    %7D%0A%0A    for (int i = 0, j = 0; i %3C compoundAnnotations.size(); i++) %7B%0A      
 Comp
@@ -2924,17 +2924,17 @@
 otation 
-:
+=
  compoun
@@ -2945,19 +2945,24 @@
 otations
-) %7B
+.get(i);
 %0A      f
@@ -3095,25 +3095,25 @@
 tchPane, 0, 
-i
+j
 ++);%0A      p
@@ -3162,17 +3162,17 @@
 AL), 0, 
-i
+j
 ++);%0A   

fix wrong number of compounds displayed in CompoundDatabaseMatchTab