// Copyright (c) Arlo Godfrey. All Rights Reserved. // Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms. // See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information. using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Diagnostics; using OwlCore.Events; using StrixMusic.Sdk.AdapterModels; using StrixMusic.Sdk.AppModels; using StrixMusic.Sdk.CoreModels; using StrixMusic.Sdk.MediaPlayback; using StrixMusic.Sdk.Plugins.Model; namespace StrixMusic.Sdk.PluginModels; /// /// Wraps an instance of with the provided plugins. /// public class AlbumPluginWrapper : IAlbum, IPluginWrapper { private readonly IAlbum _album; private readonly SdkModelPlugin[] _plugins; /// /// Initializes a new instance of the class. /// /// The instance to wrap around and apply plugins to. /// The plugins that are applied to items returned from or emitted by this collection. internal AlbumPluginWrapper(IAlbum album, params SdkModelPlugin[] plugins) { foreach (var item in plugins) ActivePlugins.Import(item); ActivePlugins = GlobalModelPluginConnector.Create(ActivePlugins); _album = ActivePlugins.Album.Execute(album); _plugins = plugins; if (_album.RelatedItems is not null) RelatedItems = new PlayableCollectionGroupPluginWrapper(_album.RelatedItems, plugins); AttachEvents(_album); } /// public SdkModelPlugin ActivePlugins { get; } = new(PluginModelWrapperInfo.Metadata); private void AttachEvents(IAlbum album) { album.SourcesChanged += OnSourcesChanged; album.ImagesCountChanged += OnImagesCountChanged; album.UrlsCountChanged += OnUrlsCountChanged; album.PlaybackStateChanged += OnPlaybackStateChanged; album.NameChanged += OnNameChanged; album.DescriptionChanged += OnDescriptionChanged; album.DurationChanged += OnDurationChanged; album.LastPlayedChanged += OnLastPlayedChanged; album.IsChangeNameAsyncAvailableChanged += OnIsChangeNameAsyncAvailableChanged; album.IsChangeDescriptionAsyncAvailableChanged += OnIsChangeDescriptionAsyncAvailableChanged; album.IsChangeDurationAsyncAvailableChanged += OnIsChangeDurationAsyncAvailableChanged; album.IsPlayTrackCollectionAsyncAvailableChanged += OnIsPlayTrackCollectionAsyncAvailableChanged; album.IsPauseTrackCollectionAsyncAvailableChanged += OnIsPauseTrackCollectionAsyncAvailableChanged; album.TracksCountChanged += OnTracksCountChanged; album.IsPlayArtistCollectionAsyncAvailableChanged += OnIsPlayArtistCollectionAsyncAvailableChanged; album.IsPauseArtistCollectionAsyncAvailableChanged += OnIsPauseArtistCollectionAsyncAvailableChanged; album.ArtistItemsCountChanged += OnArtistItemsCountChanged; album.ImagesChanged += OnImagesChanged; album.UrlsChanged += OnUrlsChanged; album.DownloadInfoChanged += OnDownloadInfoChanged; album.TracksChanged += OnTracksChanged; album.ArtistItemsChanged += OnArtistItemsChanged; album.GenresChanged += OnGenresChanged; album.GenresCountChanged += OnGenresCountChanged; } private void DetachEvents(IAlbum album) { album.SourcesChanged -= OnSourcesChanged; album.ImagesCountChanged -= OnImagesCountChanged; album.UrlsCountChanged -= OnUrlsCountChanged; album.PlaybackStateChanged -= OnPlaybackStateChanged; album.NameChanged -= OnNameChanged; album.DescriptionChanged -= OnDescriptionChanged; album.DurationChanged -= OnDurationChanged; album.LastPlayedChanged -= OnLastPlayedChanged; album.IsChangeNameAsyncAvailableChanged -= OnIsChangeNameAsyncAvailableChanged; album.IsChangeDescriptionAsyncAvailableChanged -= OnIsChangeDescriptionAsyncAvailableChanged; album.IsChangeDurationAsyncAvailableChanged -= OnIsChangeDurationAsyncAvailableChanged; album.IsPlayTrackCollectionAsyncAvailableChanged -= OnIsPlayTrackCollectionAsyncAvailableChanged; album.IsPauseTrackCollectionAsyncAvailableChanged -= OnIsPauseTrackCollectionAsyncAvailableChanged; album.TracksCountChanged -= OnTracksCountChanged; album.IsPlayArtistCollectionAsyncAvailableChanged -= OnIsPlayArtistCollectionAsyncAvailableChanged; album.IsPauseArtistCollectionAsyncAvailableChanged -= OnIsPauseArtistCollectionAsyncAvailableChanged; album.ArtistItemsCountChanged -= OnArtistItemsCountChanged; album.ImagesChanged -= OnImagesChanged; album.UrlsChanged -= OnUrlsChanged; album.DownloadInfoChanged -= OnDownloadInfoChanged; album.TracksChanged -= OnTracksChanged; album.ArtistItemsChanged -= OnArtistItemsChanged; album.GenresChanged += OnGenresChanged; album.GenresCountChanged += OnGenresCountChanged; } private void OnSourcesChanged(object sender, EventArgs e) => SourcesChanged?.Invoke(sender, e); private void OnGenresCountChanged(object sender, int e) => GenresCountChanged?.Invoke(sender, e); private void OnGenresChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { var wrappedAdded = addedItems.Select(x => new CollectionChangedItem(new GenrePluginWrapper(x.Data, _plugins), x.Index)).ToList(); var wrappedRemoved = removedItems.Select(x => new CollectionChangedItem(new GenrePluginWrapper(x.Data, _plugins), x.Index)).ToList(); GenresChanged?.Invoke(sender, wrappedAdded, wrappedRemoved); } private void OnArtistItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { var wrappedAdded = addedItems.Select(x => new CollectionChangedItem(Transform(x.Data), x.Index)).ToList(); var wrappedRemoved = removedItems.Select(x => new CollectionChangedItem(Transform(x.Data), x.Index)).ToList(); ArtistItemsChanged?.Invoke(sender, wrappedAdded, wrappedRemoved); } private void OnTracksChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { var wrappedAdded = addedItems.Select(x => new CollectionChangedItem(Transform(x.Data), x.Index)).ToList(); var wrappedRemoved = removedItems.Select(x => new CollectionChangedItem(Transform(x.Data), x.Index)).ToList(); TracksChanged?.Invoke(sender, wrappedAdded, wrappedRemoved); } private void OnUrlsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { var wrappedAdded = addedItems.Select(x => new CollectionChangedItem(new UrlPluginWrapper(x.Data, _plugins), x.Index)).ToList(); var wrappedRemoved = removedItems.Select(x => new CollectionChangedItem(new UrlPluginWrapper(x.Data, _plugins), x.Index)).ToList(); UrlsChanged?.Invoke(sender, wrappedAdded, wrappedRemoved); } private void OnImagesChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems) { var wrappedAdded = addedItems.Select(x => new CollectionChangedItem(new ImagePluginWrapper(x.Data, _plugins), x.Index)).ToList(); var wrappedRemoved = removedItems.Select(x => new CollectionChangedItem(new ImagePluginWrapper(x.Data, _plugins), x.Index)).ToList(); ImagesChanged?.Invoke(sender, wrappedAdded, wrappedRemoved); } private void OnDownloadInfoChanged(object sender, DownloadInfo e) => DownloadInfoChanged?.Invoke(sender, e); private void OnArtistItemsCountChanged(object sender, int e) => ArtistItemsCountChanged?.Invoke(sender, e); private void OnIsPauseArtistCollectionAsyncAvailableChanged(object sender, bool e) => IsPauseArtistCollectionAsyncAvailableChanged?.Invoke(sender, e); private void OnIsPlayArtistCollectionAsyncAvailableChanged(object sender, bool e) => IsPlayArtistCollectionAsyncAvailableChanged?.Invoke(sender, e); private void OnTracksCountChanged(object sender, int e) => TracksCountChanged?.Invoke(sender, e); private void OnIsPauseTrackCollectionAsyncAvailableChanged(object sender, bool e) => IsPauseTrackCollectionAsyncAvailableChanged?.Invoke(sender, e); private void OnIsPlayTrackCollectionAsyncAvailableChanged(object sender, bool e) => IsPlayTrackCollectionAsyncAvailableChanged?.Invoke(sender, e); private void OnIsChangeDurationAsyncAvailableChanged(object sender, bool e) => IsChangeDurationAsyncAvailableChanged?.Invoke(sender, e); private void OnIsChangeDescriptionAsyncAvailableChanged(object sender, bool e) => IsChangeDescriptionAsyncAvailableChanged?.Invoke(sender, e); private void OnIsChangeNameAsyncAvailableChanged(object sender, bool e) => IsChangeNameAsyncAvailableChanged?.Invoke(sender, e); private void OnLastPlayedChanged(object sender, DateTime? e) => LastPlayedChanged?.Invoke(sender, e); private void OnDurationChanged(object sender, TimeSpan e) => DurationChanged?.Invoke(sender, e); private void OnDescriptionChanged(object sender, string? e) => DescriptionChanged?.Invoke(sender, e); private void OnNameChanged(object sender, string e) => NameChanged?.Invoke(sender, e); private void OnPlaybackStateChanged(object sender, PlaybackState e) => PlaybackStateChanged?.Invoke(sender, e); private void OnUrlsCountChanged(object sender, int e) => UrlsCountChanged?.Invoke(sender, e); private void OnImagesCountChanged(object sender, int e) => ImagesCountChanged?.Invoke(sender, e); /// public event EventHandler? SourcesChanged; /// public event EventHandler? ImagesCountChanged; /// public event EventHandler? UrlsCountChanged; /// public event EventHandler? PlaybackStateChanged; /// public event EventHandler? NameChanged; /// public event EventHandler? DescriptionChanged; /// public event EventHandler? DurationChanged; /// public event EventHandler? LastPlayedChanged; /// public event EventHandler? IsChangeNameAsyncAvailableChanged; /// public event EventHandler? IsChangeDescriptionAsyncAvailableChanged; /// public event EventHandler? IsChangeDurationAsyncAvailableChanged; /// public event EventHandler? IsPlayTrackCollectionAsyncAvailableChanged; /// public event EventHandler? IsPauseTrackCollectionAsyncAvailableChanged; /// public event EventHandler? TracksCountChanged; /// public event EventHandler? IsPlayArtistCollectionAsyncAvailableChanged; /// public event EventHandler? IsPauseArtistCollectionAsyncAvailableChanged; /// public event EventHandler? ArtistItemsCountChanged; /// public event CollectionChangedEventHandler? ImagesChanged; /// public event CollectionChangedEventHandler? UrlsChanged; /// public event EventHandler? DownloadInfoChanged; /// public event CollectionChangedEventHandler? TracksChanged; /// public event CollectionChangedEventHandler? ArtistItemsChanged; /// public event EventHandler? DatePublishedChanged; /// public event EventHandler? IsChangeDatePublishedAsyncAvailableChanged; /// public event CollectionChangedEventHandler? GenresChanged; /// public event EventHandler? GenresCountChanged; /// public int TotalImageCount => _album.TotalImageCount; /// public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsAddImageAvailableAsync(index, cancellationToken); /// public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsRemoveImageAvailableAsync(index, cancellationToken); /// public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => _album.RemoveImageAsync(index, cancellationToken); /// public int TotalUrlCount => _album.TotalUrlCount; /// public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => _album.RemoveUrlAsync(index, cancellationToken); /// public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsAddUrlAvailableAsync(index, cancellationToken); /// public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsRemoveUrlAvailableAsync(index, cancellationToken); /// public string Id => _album.Id; /// public string Name => _album.Name; /// public string? Description => _album.Description; /// public DateTime? LastPlayed => _album.LastPlayed; /// public PlaybackState PlaybackState => _album.PlaybackState; /// public TimeSpan Duration => _album.Duration; /// public bool IsChangeNameAsyncAvailable => _album.IsChangeNameAsyncAvailable; /// public bool IsChangeDescriptionAsyncAvailable => _album.IsChangeDescriptionAsyncAvailable; /// public bool IsChangeDurationAsyncAvailable => _album.IsChangeDurationAsyncAvailable; /// public Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => _album.ChangeNameAsync(name, cancellationToken); /// public Task ChangeDescriptionAsync(string? description, CancellationToken cancellationToken = default) => _album.ChangeDescriptionAsync(description, cancellationToken); /// public Task ChangeDurationAsync(TimeSpan duration, CancellationToken cancellationToken = default) => _album.ChangeDurationAsync(duration, cancellationToken); /// public DateTime? AddedAt => _album.AddedAt; /// public int TotalTrackCount => _album.TotalTrackCount; /// public bool IsPlayTrackCollectionAsyncAvailable => _album.IsPlayTrackCollectionAsyncAvailable; /// public bool IsPauseTrackCollectionAsyncAvailable => _album.IsPauseTrackCollectionAsyncAvailable; /// public Task PlayTrackCollectionAsync(CancellationToken cancellationToken = default) => _album.PlayTrackCollectionAsync(cancellationToken); /// public Task PauseTrackCollectionAsync(CancellationToken cancellationToken = default) => _album.PauseTrackCollectionAsync(cancellationToken); /// public Task RemoveTrackAsync(int index, CancellationToken cancellationToken = default) => _album.RemoveTrackAsync(index, cancellationToken); /// public Task IsAddTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsAddTrackAvailableAsync(index, cancellationToken); /// public Task IsRemoveTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsRemoveTrackAvailableAsync(index, cancellationToken); /// public int TotalArtistItemsCount => _album.TotalArtistItemsCount; /// public bool IsPlayArtistCollectionAsyncAvailable => _album.IsPlayArtistCollectionAsyncAvailable; /// public bool IsPauseArtistCollectionAsyncAvailable => _album.IsPauseArtistCollectionAsyncAvailable; /// public Task PlayArtistCollectionAsync(CancellationToken cancellationToken = default) => _album.PlayArtistCollectionAsync(cancellationToken); /// public Task PauseArtistCollectionAsync(CancellationToken cancellationToken = default) => _album.PauseArtistCollectionAsync(cancellationToken); /// public Task RemoveArtistItemAsync(int index, CancellationToken cancellationToken = default) => _album.RemoveArtistItemAsync(index, cancellationToken); /// public Task IsAddArtistItemAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsAddArtistItemAvailableAsync(index, cancellationToken); /// public Task IsRemoveArtistItemAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsRemoveArtistItemAvailableAsync(index, cancellationToken); /// public bool Equals(ICoreImageCollection other) => _album.Equals(other); /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)_album).Sources; /// public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => _album.GetImagesAsync(limit, offset, cancellationToken).Select(x => new ImagePluginWrapper(x, _plugins)); /// public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => _album.AddImageAsync(image, index, cancellationToken); /// public bool Equals(ICoreUrlCollection other) => _album.Equals(other); /// public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => _album.GetUrlsAsync(limit, offset, cancellationToken).Select(x => new UrlPluginWrapper(x, _plugins)); /// public Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => _album.AddUrlAsync(url, index, cancellationToken); /// public DownloadInfo DownloadInfo => _album.DownloadInfo; /// public Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => _album.StartDownloadOperationAsync(operation, cancellationToken); /// public bool Equals(ICoreTrackCollection other) => _album.Equals(other); /// public Task PlayTrackCollectionAsync(ITrack track, CancellationToken cancellationToken = default) => _album.PlayTrackCollectionAsync(track, cancellationToken); /// public IAsyncEnumerable GetTracksAsync(int limit, int offset, CancellationToken cancellationToken = default) => _album.GetTracksAsync(limit, offset, cancellationToken).Select(Transform); /// public Task AddTrackAsync(ITrack track, int index, CancellationToken cancellationToken = default) => _album.AddTrackAsync(track, index, cancellationToken); /// public bool Equals(ICoreAlbumCollectionItem other) => _album.Equals(other); /// public bool Equals(ICoreArtistCollectionItem other) => _album.Equals(other); /// public bool Equals(ICoreArtistCollection other) => _album.Equals(other); /// public Task PlayArtistCollectionAsync(IArtistCollectionItem artistItem, CancellationToken cancellationToken = default) => _album.PlayArtistCollectionAsync(artistItem, cancellationToken); /// public IAsyncEnumerable GetArtistItemsAsync(int limit, int offset, CancellationToken cancellationToken = default) => _album.GetArtistItemsAsync(limit, offset, cancellationToken).Select(Transform); /// public Task AddArtistItemAsync(IArtistCollectionItem artistItem, int index, CancellationToken cancellationToken = default) => _album.AddArtistItemAsync(artistItem, index, cancellationToken); /// public bool Equals(ICoreAlbum other) => _album.Equals(other); /// public int TotalGenreCount => _album.TotalGenreCount; /// public Task RemoveGenreAsync(int index, CancellationToken cancellationToken = default) => _album.RemoveGenreAsync(index, cancellationToken); /// public Task IsAddGenreAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsAddGenreAvailableAsync(index, cancellationToken); /// public Task IsRemoveGenreAvailableAsync(int index, CancellationToken cancellationToken = default) => _album.IsRemoveGenreAvailableAsync(index, cancellationToken); /// public bool Equals(ICoreGenreCollection other) => _album.Equals(other); /// public IAsyncEnumerable GetGenresAsync(int limit, int offset, CancellationToken cancellationToken = default) => _album.GetGenresAsync(limit, offset, cancellationToken).Select(x => new GenrePluginWrapper(x, _plugins)); /// public Task AddGenreAsync(IGenre genre, int index, CancellationToken cancellationToken = default) => _album.AddGenreAsync(genre, index, cancellationToken); /// public ValueTask DisposeAsync() { DetachEvents(_album); return _album.DisposeAsync(); } private IArtistCollectionItem Transform(IArtistCollectionItem item) => item switch { IArtist artist => new ArtistPluginWrapper(artist, _plugins), IArtistCollection artistCollection => new ArtistCollectionPluginWrapper(artistCollection, _plugins), _ => ThrowHelper.ThrowArgumentOutOfRangeException() }; private ITrack Transform(ITrack item) => new TrackPluginWrapper(item, _plugins); /// public DateTime? DatePublished => _album.DatePublished; /// public bool IsChangeDatePublishedAsyncAvailable => _album.IsChangeDatePublishedAsyncAvailable; /// public Task ChangeDatePublishedAsync(DateTime datePublished, CancellationToken cancellationToken = default) => _album.ChangeDatePublishedAsync(datePublished, cancellationToken); /// public IPlayableCollectionGroup? RelatedItems { get; } }