#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.DTO.Common;
using Lumina.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Plugins.Contracts.Core.Metadata;
using Lumina.Plugins.CoverArtArchive.Common.Models.Contracts.Responses;
using Lumina.Plugins.CoverArtArchive.Core.Api;
using Lumina.Plugins.CoverArtArchive.Core.Mapping;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Plugins.CoverArtArchive.Core;
///
/// Provides the artwork of an album from the Cover Art Archive, by resolving the MusicBrainz identifiers of the album into artwork DTOs.
/// The artwork of the exact release is preferred, and the artwork of its release group is used when the release itself carries none.
///
internal sealed class CoverArtArchiveAlbumArtworkProvider : IArtworkProvider
{
private readonly CoverArtArchiveHttpClient _coverArtArchiveHttpClient;
///
/// Gets the display name of the artwork provider.
///
public string Name => "Cover Art Archive";
///
/// Gets the media library types this artwork provider supports.
///
public IReadOnlyList SupportedLibraryTypes => [LibraryType.Music];
///
/// Gets a value indicating whether this artwork provider requires access to the web to retrieve artwork.
///
public bool RequiresWebAccess => true;
///
/// Initializes a new instance of the class.
///
/// The HTTP client used to call the Cover Art Archive API.
public CoverArtArchiveAlbumArtworkProvider(CoverArtArchiveHttpClient coverArtArchiveHttpClient)
{
_coverArtArchiveHttpClient = coverArtArchiveHttpClient;
}
///
/// Gets the artworks of the album described by .
///
/// The lookup describing the album to get the artwork for.
/// Cancellation token that can be used to stop the execution.
/// The artworks of the album, or an empty collection when no artwork was found.
public async Task> GetArtworkAsync(AlbumMetadataLookupDto lookup, CancellationToken cancellationToken)
{
CoverArtArchiveArtworkResponse? artwork = null;
// The identifier of the exact release pins the edition the local files match, so its artwork is preferred.
if (lookup.MusicBrainzReleaseId is not null)
artwork = await _coverArtArchiveHttpClient.GetReleaseArtworkAsync(lookup.MusicBrainzReleaseId.Value, cancellationToken).ConfigureAwait(false);
// A release can carry no artwork of its own while its release group does, so the release group is the fallback.
// The collection is guarded here too, because the deserializer can produce a null collection regardless of the initializer.
if ((artwork?.Images is null || artwork.Images.Count == 0) && lookup.MusicBrainzReleaseGroupId is not null)
artwork = await _coverArtArchiveHttpClient.GetReleaseGroupArtworkAsync(lookup.MusicBrainzReleaseGroupId.Value, cancellationToken).ConfigureAwait(false);
return CoverArtArchiveMapper.MapArtwork(artwork);
}
}