/*
 *     Created by Angel Leon (@gubatron), Alden Torres (aldenml)
 *     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 com.andrew.apollo.cache;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.andrew.apollo.Config;
import com.andrew.apollo.MusicPlaybackService;
import com.andrew.apollo.utils.MusicUtils;

/**
 * A subclass of {@link ImageWorker} that fetches images from a URL.
 */
public class ImageFetcher extends ImageWorker {

    private static ImageFetcher sInstance = null;

    /**
     * Creates a new instance of {@link ImageFetcher}.
     *
     * @param context The {@link Context} to use.
     */
    private ImageFetcher(final Context context) {
        super(context);
    }

    /**
     * Used to create a singleton of the image fetcher
     *
     * @param context The {@link Context} to use
     * @return A new instance of this class.
     */
    public static ImageFetcher getInstance(final Context context) {
        if (sInstance == null) {
            sInstance = new ImageFetcher(context.getApplicationContext());
        }
        return sInstance;
    }

    /**
     * Used to fetch album images.
     */
    public void loadAlbumImage(final String artistName,
                               final String albumName,
                               final long albumId,
                               final ImageView imageView) {
        loadAlbumImage(artistName, albumName, albumId, imageView, null);
    }

    /**
     * Used to fetch album images with an optional file path fallback.
     */
    public void loadAlbumImage(final String artistName,
                               final String albumName,
                               final long albumId,
                               final ImageView imageView,
                               final String filePath) {
        loadImage(generateAlbumCacheKey(albumName, artistName),
                artistName,
                albumId,
                imageView,
                ImageType.ALBUM,
                filePath);
    }

    /**
     * Used to fetch the current artwork.
     */
    public void loadCurrentArtwork(final ImageView imageView) {
        String albumName = MusicUtils.getAlbumName();
        String artistName = MusicUtils.getArtistName();
        long currentAlbumId = MusicUtils.getCurrentAlbumId();
        String filePath = MusicUtils.getFilePath();

        if (currentAlbumId <= 0) {
            // No MediaStore album ID — try loading art directly from the file path
            // (covers YouTube downloads and other files not yet indexed by MediaStore)
            if (filePath != null) {
                loadImage(generateAlbumCacheKey(albumName, artistName), artistName,
                        -1L, imageView, ImageType.ALBUM, filePath);
            } else {
                Context context = imageView.getContext();
                Bitmap defaultArtwork = ImageFetcher.getInstance(context).getDefaultArtwork();
                if (defaultArtwork != null) {
                    imageView.setImageBitmap(defaultArtwork);
                }
            }
            return;
        }

        String albumCacheKey = generateAlbumCacheKey(albumName, artistName);
        loadImage(albumCacheKey, artistName, currentAlbumId, imageView, ImageType.ALBUM, filePath);
    }

    /**
     * Used to fetch artist images.
     */
    public void loadArtistImage(final String key, final ImageView imageView) {
        loadImage(key, key, -1, imageView, ImageType.ARTIST);
    }

    /**
     * @param pause True to temporarily pause the disk cache, false otherwise.
     */
    public void setPauseDiskCache(final boolean pause) {
        if (mImageCache != null) {
            mImageCache.setPauseDiskCache(pause);
        }
    }

    /**
     * @param key The key used to find the image to remove
     */
    public void removeFromCache(final String key) {
        if (mImageCache != null) {
            mImageCache.removeFromCache(key);
        }
    }

    /**
     * @param key The key used to find the image to return
     */
    public Bitmap getCachedBitmap(final String key) {
        if (mImageCache != null) {
            return mImageCache.getCachedBitmap(key);
        }
        return getDefaultArtwork();
    }

    /**
     * Finds cached or downloads album art. Used in {@link MusicPlaybackService}
     * to set the current album art in the notification and lock screen
     *
     * @param albumName  The name of the current album
     * @param albumId    The ID of the current album
     * @param artistName The album artist in case we should have to download
     *                   missing artwork
     * @return The album art as an {@link Bitmap}
     */
    public Bitmap getArtwork(final String albumName, final long albumId, final String artistName) {
        // Check the disk cache
        Bitmap artwork = null;

        if (albumName != null && mImageCache != null) {
            artwork = mImageCache.getBitmapFromDiskCache(
                    generateAlbumCacheKey(albumName, artistName));
        }
        if (artwork == null && albumId >= 0 && mImageCache != null) {
            // Check for local artwork
            artwork = mImageCache.getArtworkFromFile(mContext, albumId);
        }
        if (artwork != null) {
            return artwork;
        }
        return getDefaultArtwork();
    }

    /**
     * Generates key used by album art cache. It needs both album name and artist name
     * to let to select correct image for the case when there are two albums with the
     * same artist.
     *
     * @param albumName  The album name the cache key needs to be generated.
     * @param artistName The artist name the cache key needs to be generated.
     */
    public static String generateAlbumCacheKey(final String albumName, final String artistName) {
        if (albumName == null && artistName == null) {
            return "unknown_" + Config.ALBUM_ART_SUFFIX;
        }
        if (albumName == null) {
            return artistName + "_" + Config.ALBUM_ART_SUFFIX;
        }
        if (artistName == null) {
            return albumName + "_" + Config.ALBUM_ART_SUFFIX;
        }
        return albumName +
                "_" +
                artistName +
                "_" +
                Config.ALBUM_ART_SUFFIX;
    }
}
