/*
 * @Copyright 2018-2026 HardBackNutter
 * @License GNU General Public License
 *
 * This file is part of NeverTooManyBooks.
 *
 * NeverTooManyBooks 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.
 *
 * NeverTooManyBooks 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 NeverTooManyBooks. If not, see <http://www.gnu.org/licenses/>.
 */

package com.hardbacknutter.nevertoomanybooks.dialogs.entities.tocentry;

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

import androidx.annotation.NonNull;
import androidx.annotation.UiContext;

import com.hardbacknutter.nevertoomanybooks.database.DBKey;
import com.hardbacknutter.nevertoomanybooks.dialogs.DialogLauncher;
import com.hardbacknutter.nevertoomanybooks.entities.Book;
import com.hardbacknutter.nevertoomanybooks.entities.TocEntry;

public class EditTocEntryLauncher
        extends DialogLauncher {

    @NonNull
    private final ResultListener resultListener;

    /**
     * Constructor.
     *
     * @param resultListener to use
     */
    public EditTocEntryLauncher(@NonNull final ResultListener resultListener) {
        super(DBKey.FK_TOC_ENTRY,
              EditTocEntryDialogFragment::new,
              EditTocEntryBottomSheet::new);
        this.resultListener = resultListener;
    }

    /**
     * Constructor.
     *
     * @param context     preferably the {@code Activity}
     *                    but another UI {@code Context} will also do.
     * @param book        the entry belongs to
     * @param position    of the tocEntry in the list
     * @param tocEntry    to edit.
     * @param isAnthology Flag that will enable/disable the author edit field
     */
    public void launch(@NonNull @UiContext final Context context,
                       @NonNull final Book book,
                       final int position,
                       @NonNull final TocEntry tocEntry,
                       final boolean isAnthology) {

        final EditTocEntryInput input = new EditTocEntryInput(
                getRequestKey(), book.getTitle(), position, tocEntry, isAnthology);
        showDialog(context, input.toBundle());
    }

    @Override
    public void onFragmentResult(@NonNull final String requestKey,
                                 @NonNull final Bundle result) {
        final EditTocEntryOutput output = EditTocEntryOutput.fromBundle(result);
        resultListener.onResult(output.getTocEntry(), output.getPosition());
    }

    @FunctionalInterface
    public interface ResultListener {
        /**
         * Callback handler.
         *
         * @param tocEntry the modified entry
         * @param position the position in the list we were editing
         */
        void onResult(@NonNull TocEntry tocEntry,
                      int position);
    }
}
