using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using CommunityToolkit.Diagnostics;
using OwlCore.Extensions;
using StrixMusic.Sdk.CoreModels;
using StrixMusic.Sdk.Extensions;
using StrixMusic.Sdk.FileMetadata;
using StrixMusic.Sdk.FileMetadata.Models;
namespace StrixMusic.Cores.Files.Models
{
///
/// A LocalFileCore implementation of .
///
public sealed class FilesCoreSearchResults : FilesCorePlayableCollectionGroupBase, ICoreSearchResults
{
private readonly string _query;
private readonly IFileMetadataManager? _fileMetadataManager;
private IEnumerable _trackMetadata;
private IEnumerable _albumMetadata;
private IEnumerable _artistMetadata;
///
/// Initializes a new instance of the class.
///
/// The core that created this object.
/// The query that was given to produce these results.
public FilesCoreSearchResults(ICore sourceCore, string query)
: base(sourceCore)
{
_albumMetadata = new List();
_trackMetadata = new List();
_artistMetadata = new List();
_query = query;
_fileMetadataManager = SourceCore.Cast().FileMetadataManager;
}
///
public sealed override string Id { get; protected set; } = string.Empty;
///
public override string Name { get; protected set; } = "Search Results";
///
public override string? Description { get; protected set; } = null;
///
public override int TotalChildrenCount { get; internal set; }
///
public override int TotalPlaylistItemsCount { get; internal set; }
///
public override int TotalArtistItemsCount { get; internal set; }
///
public override int TotalAlbumItemsCount { get; internal set; }
///
public override int TotalTrackCount { get; internal set; }
///
public override IAsyncEnumerable GetChildrenAsync(int limit, int offset, CancellationToken cancellationToken = default)
{
return AsyncEnumerable.Empty();
}
///
public override IAsyncEnumerable GetPlaylistItemsAsync(int limit, int offset, CancellationToken cancellationToken = default)
{
return AsyncEnumerable.Empty();
}
///
public override async IAsyncEnumerable GetAlbumItemsAsync(int limit, int offset, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Guard.IsNotNull(_fileMetadataManager, nameof(_fileMetadataManager));
_albumMetadata = await _fileMetadataManager.Albums.GetItemsAsync(0, int.MaxValue);
_albumMetadata = _albumMetadata.Where(c => c.Title?.Equals(_query, StringComparison.OrdinalIgnoreCase) ?? false);
foreach (var album in _albumMetadata)
{
Guard.IsNotNull(album.Id, nameof(album.Id));
yield return new FilesCoreAlbum(SourceCore, album);
}
}
///
public override async IAsyncEnumerable GetArtistItemsAsync(int limit, int offset, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Guard.IsNotNull(_fileMetadataManager, nameof(_fileMetadataManager));
_artistMetadata = await _fileMetadataManager.Artists.GetItemsAsync(0, int.MaxValue);
_artistMetadata = _artistMetadata.Where(c => c.Name?.Equals(_query, StringComparison.OrdinalIgnoreCase) ?? false);
foreach (var artist in _artistMetadata)
{
yield return new FilesCoreArtist(SourceCore, artist);
}
}
///
public override async IAsyncEnumerable GetTracksAsync(int limit, int offset, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Guard.IsNotNull(_fileMetadataManager, nameof(_fileMetadataManager));
_trackMetadata = await _fileMetadataManager.Tracks.GetItemsAsync(0, int.MaxValue);
_trackMetadata = _trackMetadata.Where(c => c.Title?.Equals(_query, StringComparison.OrdinalIgnoreCase) ?? false);
foreach (var track in _trackMetadata)
{
yield return new FilesCoreTrack(SourceCore, track);
}
}
///
public override IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default)
{
return AsyncEnumerable.Empty();
}
}
}