/*
 * @Copyright 2018-2025 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.covers;

import android.content.Context;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import com.hardbacknutter.nevertoomanybooks.core.storage.StorageException;

public interface ImageOwner {

    /**
     * Get the id of the owning entity.
     *
     * @return id
     */
    long getId();

    /**
     * Get the UUID to use as the image file name.
     *
     * @return uuid
     */
    @NonNull
    Optional<String> getImageUuid();

    /**
     * Get the <strong>current</strong> cover file for this book.
     * This method may return a temporary cover, or the persisted cover.
     * <p>
     * Any {@link StorageException} is <strong>IGNORED</strong>
     *
     * @param context Current context
     * @param cIdx    0..n image index
     *
     * @return file
     */
    @NonNull
    Optional<File> getImage(@NonNull Context context,
                            @IntRange(from = 0, to = 3) int cIdx);

    /**
     * Update the ImageOwner with the given {@link File}.
     * This method may set a temporary cover, or persists the cover to storage.
     *
     * @param context Current context
     * @param cIdx    0..n image index
     * @param file    cover file or {@code null} to delete the cover
     *                The file instance passed in MUST be discarded.
     *                If applicable, the caller can/must use the {@link File}
     *                as returned by this method.
     *
     * @throws StorageException The covers directory is not available
     * @throws IOException      on generic/other IO failures
     */
    @WorkerThread
    void setImage(@NonNull Context context,
                  @IntRange(from = 0, to = 3) int cIdx,
                  @Nullable File file)
            throws StorageException, IOException;

    /**
     * Remove the image at the given index.
     *
     * @param context Current context
     * @param cIdx    0..n image index
     */
    @WorkerThread
    void removeImage(@NonNull Context context,
                     @IntRange(from = 0, to = 3) int cIdx);
}
