package org.jabref.logic.importer;

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

import org.jabref.model.entry.BibEntry;
import org.jabref.model.paging.Page;
import org.jabref.model.search.query.BaseQueryNode;

public interface PagedSearchBasedFetcher extends SearchBasedFetcher {

    /// @param queryNode  first search node
    /// @param pageNumber requested site number indexed from 0
    /// @return Page with search results
    Page<BibEntry> performSearchPaged(BaseQueryNode queryNode, int pageNumber) throws FetcherException;

    /// @param searchQuery query string that can be parsed into a lucene query
    /// @param pageNumber  requested site number indexed from 0
    /// @return Page with search results
    default Page<BibEntry> performSearchPaged(String searchQuery, int pageNumber) throws FetcherException {
        if (searchQuery.isBlank()) {
            return new Page<>(searchQuery, pageNumber, List.of());
        }

        return this.performSearchPaged(SearchBasedFetcher.getQueryNode(searchQuery), pageNumber);
    }

    /// @param rawQuery   catalog-native query string sent verbatim to the catalog
    /// @param pageNumber requested site number indexed from 0
    /// @return Page with search results
    default Page<BibEntry> performRawSearchQueryPaged(String rawQuery, int pageNumber) throws FetcherException {
        throw new UnsupportedOperationException(
                getName() + " has not yet been migrated to performRawSearchQueryPaged");
    }

    /// @return default pageSize
    default int getPageSize() {
        return 20;
    }

    /// This method is used to send complex queries using fielded search.
    ///
    /// @param queryNode the first search node
    /// @return a list of {@link BibEntry}, which are matched by the query (may be empty)
    @Override
    default List<BibEntry> performSearch(BaseQueryNode queryNode) throws FetcherException {
        return new ArrayList<>(performSearchPaged(queryNode, 0).getContent());
    }

    @Override
    default List<BibEntry> performRawSearchQuery(String rawQuery) throws FetcherException {
        return new ArrayList<>(performRawSearchQueryPaged(rawQuery, 0).getContent());
    }
}
