// 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 ArtistCollectionPluginWrapper : IArtistCollection, IPluginWrapper
{
private readonly IArtistCollection _artistCollection;
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 ArtistCollectionPluginWrapper(IArtistCollection artistCollection, params SdkModelPlugin[] plugins)
{
foreach (var item in plugins)
ActivePlugins.Import(item);
ActivePlugins = GlobalModelPluginConnector.Create(ActivePlugins);
_artistCollection = ActivePlugins.ArtistCollection.Execute(artistCollection);
_plugins = plugins;
AttachEvents(_artistCollection);
}
///
public SdkModelPlugin ActivePlugins { get; } = new(PluginModelWrapperInfo.Metadata);
private void AttachEvents(IArtistCollection artistCollection)
{
artistCollection.SourcesChanged += OnSourcesChanged;
artistCollection.ImagesCountChanged += OnImagesCountChanged;
artistCollection.UrlsCountChanged += OnUrlsCountChanged;
artistCollection.PlaybackStateChanged += OnPlaybackStateChanged;
artistCollection.NameChanged += OnNameChanged;
artistCollection.DescriptionChanged += OnDescriptionChanged;
artistCollection.DurationChanged += OnDurationChanged;
artistCollection.LastPlayedChanged += OnLastPlayedChanged;
artistCollection.IsChangeNameAsyncAvailableChanged += OnIsChangeNameAsyncAvailableChanged;
artistCollection.IsChangeDescriptionAsyncAvailableChanged += OnIsChangeDescriptionAsyncAvailableChanged;
artistCollection.IsChangeDurationAsyncAvailableChanged += OnIsChangeDurationAsyncAvailableChanged;
artistCollection.IsPlayArtistCollectionAsyncAvailableChanged += OnIsPlayArtistCollectionAsyncAvailableChanged;
artistCollection.IsPauseArtistCollectionAsyncAvailableChanged += OnIsPauseArtistCollectionAsyncAvailableChanged;
artistCollection.ArtistItemsCountChanged += OnArtistItemsCountChanged;
artistCollection.ImagesChanged += OnImagesChanged;
artistCollection.UrlsChanged += OnUrlsChanged;
artistCollection.DownloadInfoChanged += OnDownloadInfoChanged;
artistCollection.ArtistItemsChanged += OnArtistItemsChanged;
}
private void DetachEvents(IArtistCollection artistCollection)
{
artistCollection.SourcesChanged -= OnSourcesChanged;
artistCollection.ImagesCountChanged -= OnImagesCountChanged;
artistCollection.UrlsCountChanged -= OnUrlsCountChanged;
artistCollection.PlaybackStateChanged -= OnPlaybackStateChanged;
artistCollection.NameChanged -= OnNameChanged;
artistCollection.DescriptionChanged -= OnDescriptionChanged;
artistCollection.DurationChanged -= OnDurationChanged;
artistCollection.LastPlayedChanged -= OnLastPlayedChanged;
artistCollection.IsChangeNameAsyncAvailableChanged -= OnIsChangeNameAsyncAvailableChanged;
artistCollection.IsChangeDescriptionAsyncAvailableChanged -= OnIsChangeDescriptionAsyncAvailableChanged;
artistCollection.IsChangeDurationAsyncAvailableChanged -= OnIsChangeDurationAsyncAvailableChanged;
artistCollection.IsPlayArtistCollectionAsyncAvailableChanged -= OnIsPlayArtistCollectionAsyncAvailableChanged;
artistCollection.IsPauseArtistCollectionAsyncAvailableChanged -= OnIsPauseArtistCollectionAsyncAvailableChanged;
artistCollection.ArtistItemsCountChanged -= OnArtistItemsCountChanged;
artistCollection.ImagesChanged -= OnImagesChanged;
artistCollection.UrlsChanged -= OnUrlsChanged;
artistCollection.DownloadInfoChanged -= OnDownloadInfoChanged;
artistCollection.ArtistItemsChanged -= OnArtistItemsChanged;
}
private void OnSourcesChanged(object sender, EventArgs e) => SourcesChanged?.Invoke(sender, e);
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 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 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? IsPlayArtistCollectionAsyncAvailableChanged;
///
public event EventHandler? IsPauseArtistCollectionAsyncAvailableChanged;
///
public event EventHandler? ArtistItemsCountChanged;
///
public event CollectionChangedEventHandler? ImagesChanged;
///
public event CollectionChangedEventHandler? UrlsChanged;
///
public event EventHandler? DownloadInfoChanged;
///
public event CollectionChangedEventHandler? ArtistItemsChanged;
///
public int TotalImageCount => _artistCollection.TotalImageCount;
///
public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.IsAddImageAvailableAsync(index, cancellationToken);
///
public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.IsRemoveImageAvailableAsync(index, cancellationToken);
///
public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.RemoveImageAsync(index, cancellationToken);
///
public int TotalUrlCount => _artistCollection.TotalUrlCount;
///
public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.RemoveUrlAsync(index, cancellationToken);
///
public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.IsAddUrlAvailableAsync(index, cancellationToken);
///
public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.IsRemoveUrlAvailableAsync(index, cancellationToken);
///
public string Id => _artistCollection.Id;
///
public string Name => _artistCollection.Name;
///
public string? Description => _artistCollection.Description;
///
public DateTime? LastPlayed => _artistCollection.LastPlayed;
///
public PlaybackState PlaybackState => _artistCollection.PlaybackState;
///
public TimeSpan Duration => _artistCollection.Duration;
///
public bool IsChangeNameAsyncAvailable => _artistCollection.IsChangeNameAsyncAvailable;
///
public bool IsChangeDescriptionAsyncAvailable => _artistCollection.IsChangeDescriptionAsyncAvailable;
///
public bool IsChangeDurationAsyncAvailable => _artistCollection.IsChangeDurationAsyncAvailable;
///
public Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => _artistCollection.ChangeNameAsync(name, cancellationToken);
///
public Task ChangeDescriptionAsync(string? description, CancellationToken cancellationToken = default) => _artistCollection.ChangeDescriptionAsync(description, cancellationToken);
///
public Task ChangeDurationAsync(TimeSpan duration, CancellationToken cancellationToken = default) => _artistCollection.ChangeDurationAsync(duration, cancellationToken);
///
public DateTime? AddedAt => _artistCollection.AddedAt;
///
public int TotalArtistItemsCount => _artistCollection.TotalArtistItemsCount;
///
public bool IsPlayArtistCollectionAsyncAvailable => _artistCollection.IsPlayArtistCollectionAsyncAvailable;
///
public bool IsPauseArtistCollectionAsyncAvailable => _artistCollection.IsPauseArtistCollectionAsyncAvailable;
///
public Task PlayArtistCollectionAsync(CancellationToken cancellationToken = default) => _artistCollection.PlayArtistCollectionAsync(cancellationToken);
///
public Task PauseArtistCollectionAsync(CancellationToken cancellationToken = default) => _artistCollection.PauseArtistCollectionAsync(cancellationToken);
///
public Task RemoveArtistItemAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.RemoveArtistItemAsync(index, cancellationToken);
///
public Task IsAddArtistItemAvailableAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.IsAddArtistItemAvailableAsync(index, cancellationToken);
///
public Task IsRemoveArtistItemAvailableAsync(int index, CancellationToken cancellationToken = default) => _artistCollection.IsRemoveArtistItemAvailableAsync(index, cancellationToken);
///
public bool Equals(ICoreImageCollection other) => _artistCollection.Equals(other);
///
IReadOnlyList IMerged.Sources => ((IMerged)_artistCollection).Sources;
///
IReadOnlyList IMerged.Sources => ((IMerged)_artistCollection).Sources;
///
IReadOnlyList IMerged.Sources => ((IMerged)_artistCollection).Sources;
///
IReadOnlyList IMerged.Sources => ((IMerged)_artistCollection).Sources;
///
public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => _artistCollection.GetImagesAsync(limit, offset, cancellationToken).Select(x => new ImagePluginWrapper(x, _plugins));
///
public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => _artistCollection.AddImageAsync(image, index, cancellationToken);
///
public bool Equals(ICoreUrlCollection other) => _artistCollection.Equals(other);
///
public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => _artistCollection.GetUrlsAsync(limit, offset, cancellationToken).Select(x => new UrlPluginWrapper(x, _plugins));
///
public Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => _artistCollection.AddUrlAsync(url, index, cancellationToken);
///
public DownloadInfo DownloadInfo => _artistCollection.DownloadInfo;
///
public Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => _artistCollection.StartDownloadOperationAsync(operation, cancellationToken);
///
public bool Equals(ICoreArtistCollectionItem other) => _artistCollection.Equals(other);
///
public Task PlayArtistCollectionAsync(IArtistCollectionItem artistItem, CancellationToken cancellationToken = default) => _artistCollection.PlayArtistCollectionAsync(artistItem, cancellationToken);
///
public IAsyncEnumerable GetArtistItemsAsync(int limit, int offset, CancellationToken cancellationToken = default) => _artistCollection.GetArtistItemsAsync(limit, offset, cancellationToken).Select(Transform);
///
public Task AddArtistItemAsync(IArtistCollectionItem artistItem, int index, CancellationToken cancellationToken = default) => _artistCollection.AddArtistItemAsync(artistItem, index, cancellationToken);
///
public bool Equals(ICoreArtistCollection other) => _artistCollection.Equals(other);
///
public ValueTask DisposeAsync()
{
DetachEvents(_artistCollection);
return _artistCollection.DisposeAsync();
}
private IArtistCollectionItem Transform(IArtistCollectionItem item) => item switch
{
IArtist artist => new ArtistPluginWrapper(artist, _plugins),
IArtistCollection artistCollection => new ArtistCollectionPluginWrapper(artistCollection, _plugins),
_ => ThrowHelper.ThrowArgumentOutOfRangeException()
};
}