package me.devsaki.hentoid.widget;

import android.content.Context;
import android.os.Bundle;

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

import javax.annotation.Nonnull;

import me.devsaki.hentoid.collection.CollectionAccessor;
import me.devsaki.hentoid.database.ObjectBoxDB;
import me.devsaki.hentoid.database.domains.Attribute;
import me.devsaki.hentoid.database.domains.Content;
import me.devsaki.hentoid.enums.Language;
import me.devsaki.hentoid.enums.Site;
import me.devsaki.hentoid.listener.PagedResultListener;
import me.devsaki.hentoid.util.Preferences;

public class ContentSearchManager {

    // Save state constants
    private static final String KEY_SELECTED_TAGS = "selected_tags";
    private static final String KEY_FILTER_FAVOURITES = "filter_favs";
    private static final String KEY_QUERY = "query";
    private static final String KEY_SORT_ORDER = "sort_order";
    private static final String KEY_CURRENT_PAGE = "current_page";

    private final CollectionAccessor accessor;

    // Current page of collection view (NB : In EndlessFragment, a "page" is a group of loaded books. Last page is reached when scrolling reaches the very end of the book list)
    private int currentPage = 1;
    // Favourite filter active
    private boolean filterFavourites = false;
    // Full-text query
    private String query = "";
    // Current search tags
    private List<Attribute> tags = new ArrayList<>();

    private int contentSortOrder = Preferences.getContentSortOrder();


    public ContentSearchManager(CollectionAccessor accessor) {
        this.accessor = accessor;
    }

    public void setFilterFavourites(boolean filterFavourites) {
        this.filterFavourites = filterFavourites;
    }

    public boolean isFilterFavourites() {
        return filterFavourites;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getQuery() {
        return (query != null) ? query : "";
    }

    public void setTags(List<Attribute> tags) {
        if (tags != null) this.tags = tags;
        else this.tags.clear();
    }

    public List<Attribute> getTags() {
        return tags;
    }

    public void clearSelectedSearchTags() {
        if (tags != null) tags.clear();
    }

    public int getContentSortOrder() {
        return contentSortOrder;
    }

    public void setContentSortOrder(int contentSortOrder) {
        this.contentSortOrder = contentSortOrder;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public void increaseCurrentPage() {
        currentPage++;
    }

    public void decreaseCurrentPage() {
        currentPage--;
    }

    public void saveToBundle(@Nonnull Bundle outState) {
        outState.putBoolean(KEY_FILTER_FAVOURITES, filterFavourites);
        outState.putString(KEY_QUERY, query);
        outState.putInt(KEY_SORT_ORDER, contentSortOrder);
        outState.putInt(KEY_CURRENT_PAGE, currentPage);
        long[] selectedTagIds = new long[tags.size()];
        int index = 0;
        for (Attribute a : tags) {
            selectedTagIds[index++] = a.getId();
        }
        outState.putLongArray(KEY_SELECTED_TAGS, selectedTagIds);
    }

    public void loadFromBundle(@Nonnull Bundle state, Context ctx) {
        filterFavourites = state.getBoolean(KEY_FILTER_FAVOURITES, false);
        query = state.getString(KEY_QUERY, "");
        contentSortOrder = state.getInt(KEY_SORT_ORDER, Preferences.getContentSortOrder());
        currentPage = state.getInt(KEY_CURRENT_PAGE);

        long[] selectedTagIds = state.getLongArray(KEY_SELECTED_TAGS);
        ObjectBoxDB db = ObjectBoxDB.getInstance(ctx);
        if (selectedTagIds != null) {
            for (long i : selectedTagIds) {
                Attribute a = db.selectAttributeById(i);
                if (a != null) {
                    tags.add(a);
                }
            }
        }
    }

    public void searchLibraryForContent(int booksPerPage, PagedResultListener<Content> listener) {
        if (!getQuery().isEmpty())
            accessor.searchBooksUniversalPaged(getQuery(), currentPage, booksPerPage, contentSortOrder, filterFavourites, listener); // Universal search
        else if (!tags.isEmpty())
            accessor.searchBooksPaged("", tags, currentPage, booksPerPage, contentSortOrder, filterFavourites, listener); // Advanced search
        else
            accessor.getRecentBooksPaged(Site.HITOMI, Language.ANY, currentPage, booksPerPage, contentSortOrder, filterFavourites, listener); // Default search (display recent)
        // TODO : do something about these ridiculous default 1st arguments
    }

    public void searchLibraryForId(int booksPerPage, PagedResultListener<Long> listener) {
        if (!getQuery().isEmpty())
            accessor.searchBookIdsUniversalPaged(getQuery(), currentPage, booksPerPage, contentSortOrder, filterFavourites, listener); // Universal search
        else if (!tags.isEmpty())
            accessor.searchBookIdsPaged("", tags, currentPage, booksPerPage, contentSortOrder, filterFavourites, listener); // Advanced search
        else
            accessor.getRecentBookIdsPaged(Site.HITOMI, Language.ANY, currentPage, booksPerPage, contentSortOrder, filterFavourites, listener); // Default search (display recent)
        // TODO : do something about these ridiculous default 1st arguments
    }

    public void dispose() {
        accessor.dispose();
    }
}

@@ -61,16 +61,40 @@
 ontext;%0A
+import android.net.Uri;%0A
 import a
@@ -198,16 +198,83 @@
 nnull;%0A%0A
+import me.devsaki.hentoid.activities.bundles.SearchActivityBundle;%0A
 import m
@@ -3146,172 +3146,89 @@
     
-long%5B%5D selectedTagIds = new long%5Btags.size()%5D;%0A        int index = 0;%0A        for (Attribute a : tags) %7B%0A            selectedTagIds%5Bindex++%5D = a.getId
+String searchUri = SearchActivityBundle.Builder.buildSearchUri(tags).toString
 ();%0A
-        %7D%0A
     
@@ -3243,25 +3243,22 @@
 tate.put
-LongArray
+String
 (KEY_SEL
@@ -3271,28 +3271,23 @@
 TAGS, se
-lectedTagIds
+archUri
 );%0A    %7D
@@ -3639,29 +3639,24 @@
     
-long%5B%5D selectedTagIds
+String searchUri
  = s
@@ -3667,17 +3667,14 @@
 .get
-LongArray
+String
 (KEY
@@ -3702,301 +3702,80 @@
     
-ObjectBoxDB db = ObjectBoxDB.getInstance(ctx);%0A        if (selectedTagIds != null) %7B%0A            for (long i : selectedTagIds) %7B%0A                Attribute a = db.selectAttributeById(i);%0A                if (a != null) %7B%0A                    tags.add(a);%0A                %7D%0A            %7D%0A        %7D
+tags = SearchActivityBundle.Parser.parseSearchUri(Uri.parse(searchUri));
 %0A   

Fix run viewer on book list filtered by source