package org.jabref.gui.search;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;

import org.jabref.gui.StateManager;
import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.maintable.MainTableFieldValueFormatter;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.PreferencesService;

import com.tobiasdiez.easybind.EasyBind;

public class SearchResultsTableDataModel {

    private final FilteredList<BibEntryTableViewModel> entriesFiltered;
    private final SortedList<BibEntryTableViewModel> entriesSorted;
    private final ObjectProperty<MainTableFieldValueFormatter> fieldValueFormatter;
    private final PreferencesService preferencesService;
    private final BibDatabaseContext bibDatabaseContext;

    public SearchResultsTableDataModel(BibDatabaseContext bibDatabaseContext, PreferencesService preferencesService, StateManager stateManager) {
        this.preferencesService = preferencesService;
        this.bibDatabaseContext = bibDatabaseContext;
        this.fieldValueFormatter = new SimpleObjectProperty<>(new MainTableFieldValueFormatter(preferencesService, bibDatabaseContext));

        List<BibEntryTableViewModel> models = new ArrayList<>();
        for (BibDatabaseContext context : stateManager.getOpenDatabases()) {
            ObservableList<BibEntry> entriesForDb = context.getDatabase().getEntries();
            List<BibEntryTableViewModel> viewModelForDb = EasyBind.mapBacked(entriesForDb, entry -> new BibEntryTableViewModel(entry, context, fieldValueFormatter));

            models.addAll(viewModelForDb);
        }
        ObservableList<BibEntryTableViewModel> entriesViewModel = FXCollections.observableArrayList(models);

        entriesFiltered = new FilteredList<>(entriesViewModel);
        entriesFiltered.predicateProperty().bind(EasyBind.map(stateManager.activeSearchQueryProperty(), (query) -> entry -> isMatchedBySearch(query, entry)));

        // We need to wrap the list since otherwise sorting in the table does not work
        entriesSorted = new SortedList<>(entriesFiltered);
    }


    private boolean isMatchedBySearch(Optional<SearchQuery> query, BibEntryTableViewModel entry) {
        return query.map(matcher -> matcher.isMatch(entry.getEntry()))
                    .orElse(true);
    }

    public SortedList<BibEntryTableViewModel> getEntriesFilteredAndSorted() {
        return entriesSorted;
    }

    public void refresh() {
        this.fieldValueFormatter.setValue(new MainTableFieldValueFormatter(preferencesService, bibDatabaseContext));
    }
}

@@ -29,36 +29,8 @@
 h;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -1523,32 +1523,42 @@
 ext));%0A%0A        
+Observable
 List%3CBibEntryTab
@@ -1574,21 +1574,51 @@
 el%3E 
-m
+entriesViewM
 odel
-s
  = 
-new 
+FXCollections.observable
 Arra
@@ -1622,18 +1622,16 @@
 rrayList
-%3C%3E
 ();%0A    
@@ -1953,25 +1953,24 @@
 ormatter));%0A
-%0A
             
@@ -1969,22 +1969,32 @@
         
-m
+entriesViewM
 odel
-s
 .addAll(
@@ -2023,117 +2023,8 @@
    %7D
-%0A        ObservableList%3CBibEntryTableViewModel%3E entriesViewModel = FXCollections.observableArrayList(models);
 %0A%0A  

remove extra list