/*
 *     Created by Angel Leon (@gubatron)
 *     Copyright (c) 2011-2026, FrostWire(R). All rights reserved.
 * 
 *     This program 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 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.team_hermes.wireshare.android.core;

import org.team_hermes.wireshare.android.gui.SearchMediator;
import org.team_hermes.wireshare.android.gui.adapters.SearchResultListAdapter;
import org.team_hermes.wireshare.android.gui.fragments.SearchFragment;
import org.team_hermes.wireshare.android.gui.views.AbstractListAdapter;
import org.team_hermes.wireshare.android.util.SystemUtils;
import org.team_hermes.wireshare.search.telluride.TellurideSearchPerformer;
import org.team_hermes.wireshare.search.telluride.TellurideSearchResult;
import org.team_hermes.wireshare.util.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;
import java.util.Objects;

public class TellurideCourierCallback<T extends AbstractListAdapter<? super TellurideSearchResult>> {
    private final T adapter;
    private final String url;
    private final TellurideCourier.SearchPerformer<T> searchPerformer;
    private boolean hasAborted = false;

    private static Logger LOG = Logger.getLogger(TellurideCourierCallback.class);

    public TellurideCourierCallback(TellurideCourier.SearchPerformer<T> searchPerformer, String pageUrl, T adapter) {
        this.searchPerformer = searchPerformer;
        this.url = pageUrl;
        this.adapter = adapter;
    }

    void onSearchResultListAdapterResults(List<TellurideSearchResult> results, boolean errored) {
        SystemUtils.postToUIThread(() -> {
            LOG.info("onSearchResultListAdapterResults: " + adapter.getClass().getName());

            if (results == null || results.isEmpty() || errored) {
                adapter.clear();
                if (SearchMediator.instance().getListener() != null && searchPerformer != null) {
                    SearchMediator.instance().getListener().onStopped(searchPerformer.getToken());
                }
                return;
            }
            if (adapter instanceof org.team_hermes.wireshare.android.gui.dialogs.TellurideSearchResultDownloadDialog.TellurideSearchResultDownloadDialogAdapter) {
                LOG.info("onSearchResultListAdapterResults: TellurideSearchResultDownloadDialogAdapter, aborting.");
                return;
            }
            SearchResultListAdapter srlAdapter = (SearchResultListAdapter) adapter;
            // Screw our listener, make the adapter do what we want.
            adapter.clear();
            if (results != null && !results.isEmpty()) {
                srlAdapter.addResults(results); // adds to full list of results
                srlAdapter.setFileType(Constants.FILE_TYPE_AUDIO, false, null);
                srlAdapter.setFileType(Constants.FILE_TYPE_VIDEOS, true,
                        () -> {
                            SearchResultListAdapter.FilteredSearchResults filteredSearchResults = srlAdapter.getFilteredSearchResults();
                            srlAdapter.updateVisualListWithAllMediaTypeFilteredSearchResults(filteredSearchResults.mediaTypeFiltered, false);
                            SearchFragment.instance().refreshFileTypeCounters(false, filteredSearchResults);
                            SearchMediator.instance().getListener().onStopped(searchPerformer.getToken());
                        });

            }
        });
    }

    private static volatile Gson gson = null;

    void onResults(String result, boolean isPlaylist) {
        if (result == null) {
            onResults((List<TellurideSearchResult>) null, true);
            return;
        }
        if (gson == null) {
            gson = new GsonBuilder().create();
        }
        List<TellurideSearchResult> playlistResults = TellurideSearchPerformer.getValidPlaylistResults(result, gson, null, -1, url);
        onResults(playlistResults, false);
    }

    void onResults(List<TellurideSearchResult> results, boolean errored) {
        // This comes in too fast, gotta let the UI get there
        if (adapter instanceof SearchResultListAdapter || (results == null || results.isEmpty() || errored)) {
            onSearchResultListAdapterResults(results, errored);
        } else {
            if (results != null && !results.isEmpty() && adapter != null) {
                for (TellurideSearchResult result : results) {
                    adapter.addItem(result);
                }
            }
        }
    }

    final void abort() {
        hasAborted = true;
    }

    final boolean aborted() {
        return hasAborted;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TellurideCourierCallback that = (TellurideCourierCallback) o;
        return Objects.equals(url, that.url);
    }

    @Override
    public int hashCode() {
        return Objects.hash(url);
    }
}