// 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 OwlCore.ComponentModel; using OwlCore.Events; using StrixMusic.Sdk.AdapterModels; using StrixMusic.Sdk.AppModels; using StrixMusic.Sdk.CoreModels; using StrixMusic.Sdk.MediaPlayback; namespace StrixMusic.Sdk.Plugins.Model { /// /// An implementation of which delegates all member access to the implementation, /// unless the member is overridden in a derived class which changes the behavior. /// public class ArtistCollectionPluginBase : IModelPlugin, IArtistCollection, IDelegatable { /// /// Creates a new instance of . /// /// Metadata about the plugin which was provided during registration. /// The implementation which all member access is delegated to, unless the member is overridden in a derived class which changes the behavior. internal protected ArtistCollectionPluginBase(ModelPluginMetadata registration, IArtistCollection inner) { Metadata = registration; Inner = inner; InnerUrlCollection = inner; InnerImageCollection = inner; InnerPlayable = inner; InnerDownloadable = inner; } /// /// Metadata about the plugin which was provided during registration. /// public ModelPluginMetadata Metadata { get; } /// public IArtistCollection Inner { get; set; } /// /// A wrapped implementation which member access can be delegated to. Defaults to . /// public IPlayable InnerPlayable { get; set; } /// /// A wrapped implementation which member access can be delegated to. Defaults to . /// public IDownloadable InnerDownloadable { get; set; } /// /// A wrapped implementation which member access can be delegated to. Defaults to . /// public IUrlCollection InnerUrlCollection { get; set; } /// /// A wrapped implementation which member access can be delegated to. Defaults to . /// public IImageCollection InnerImageCollection { get; set; } /// public virtual int TotalArtistItemsCount => Inner.TotalArtistItemsCount; /// public virtual bool IsPlayArtistCollectionAsyncAvailable => Inner.IsPlayArtistCollectionAsyncAvailable; /// public virtual bool IsPauseArtistCollectionAsyncAvailable => Inner.IsPauseArtistCollectionAsyncAvailable; /// public virtual DateTime? AddedAt => Inner.AddedAt; /// public virtual string Id => InnerPlayable.Id; /// public virtual string Name => InnerPlayable.Name; /// public virtual string? Description => InnerPlayable.Description; /// public virtual DateTime? LastPlayed => InnerPlayable.LastPlayed; /// public virtual PlaybackState PlaybackState => InnerPlayable.PlaybackState; /// public virtual TimeSpan Duration => InnerPlayable.Duration; /// public virtual bool IsChangeNameAsyncAvailable => InnerPlayable.IsChangeNameAsyncAvailable; /// public virtual bool IsChangeDescriptionAsyncAvailable => InnerPlayable.IsChangeDescriptionAsyncAvailable; /// public virtual bool IsChangeDurationAsyncAvailable => InnerPlayable.IsChangeDurationAsyncAvailable; /// public virtual DownloadInfo DownloadInfo => InnerDownloadable.DownloadInfo; /// public virtual int TotalImageCount => InnerImageCollection.TotalImageCount; /// public virtual int TotalUrlCount => InnerUrlCollection.TotalUrlCount; /// IReadOnlyList IMerged.Sources => ((IMerged)Inner).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)Inner).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)InnerImageCollection).Sources; /// IReadOnlyList IMerged.Sources => ((IMerged)InnerUrlCollection).Sources; /// public event EventHandler? SourcesChanged { add => Inner.SourcesChanged += value; remove => Inner.SourcesChanged -= value; } /// public virtual event CollectionChangedEventHandler? ArtistItemsChanged { add => Inner.ArtistItemsChanged += value; remove => Inner.ArtistItemsChanged -= value; } /// public virtual event EventHandler? ArtistItemsCountChanged { add => Inner.ArtistItemsCountChanged += value; remove => Inner.ArtistItemsCountChanged -= value; } /// public virtual event EventHandler? IsPlayArtistCollectionAsyncAvailableChanged { add => Inner.IsPlayArtistCollectionAsyncAvailableChanged += value; remove => Inner.IsPlayArtistCollectionAsyncAvailableChanged -= value; } /// public virtual event EventHandler? IsPauseArtistCollectionAsyncAvailableChanged { add => Inner.IsPauseArtistCollectionAsyncAvailableChanged += value; remove => Inner.IsPauseArtistCollectionAsyncAvailableChanged -= value; } /// public virtual event EventHandler? PlaybackStateChanged { add => InnerPlayable.PlaybackStateChanged += value; remove => InnerPlayable.PlaybackStateChanged -= value; } /// public virtual event EventHandler? NameChanged { add => InnerPlayable.NameChanged += value; remove => InnerPlayable.NameChanged -= value; } /// public virtual event EventHandler? DescriptionChanged { add => InnerPlayable.DescriptionChanged += value; remove => InnerPlayable.DescriptionChanged -= value; } /// public virtual event EventHandler? DurationChanged { add => InnerPlayable.DurationChanged += value; remove => InnerPlayable.DurationChanged -= value; } /// public virtual event EventHandler? LastPlayedChanged { add => InnerPlayable.LastPlayedChanged += value; remove => InnerPlayable.LastPlayedChanged -= value; } /// public virtual event EventHandler? IsChangeNameAsyncAvailableChanged { add => InnerPlayable.IsChangeNameAsyncAvailableChanged += value; remove => InnerPlayable.IsChangeNameAsyncAvailableChanged -= value; } /// public virtual event EventHandler? IsChangeDescriptionAsyncAvailableChanged { add => InnerPlayable.IsChangeDescriptionAsyncAvailableChanged += value; remove => InnerPlayable.IsChangeDescriptionAsyncAvailableChanged -= value; } /// public virtual event EventHandler? IsChangeDurationAsyncAvailableChanged { add => InnerPlayable.IsChangeDurationAsyncAvailableChanged += value; remove => InnerPlayable.IsChangeDurationAsyncAvailableChanged -= value; } /// public virtual event EventHandler? DownloadInfoChanged { add => InnerDownloadable.DownloadInfoChanged += value; remove => InnerDownloadable.DownloadInfoChanged -= value; } /// public virtual event CollectionChangedEventHandler? ImagesChanged { add => InnerImageCollection.ImagesChanged += value; remove => InnerImageCollection.ImagesChanged -= value; } /// public virtual event EventHandler? ImagesCountChanged { add => InnerImageCollection.ImagesCountChanged += value; remove => InnerImageCollection.ImagesCountChanged -= value; } /// public virtual event CollectionChangedEventHandler? UrlsChanged { add => InnerUrlCollection.UrlsChanged += value; remove => InnerUrlCollection.UrlsChanged -= value; } /// public virtual event EventHandler? UrlsCountChanged { add => InnerUrlCollection.UrlsCountChanged += value; remove => InnerUrlCollection.UrlsCountChanged -= value; } /// public virtual Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => InnerImageCollection.AddImageAsync(image, index, cancellationToken); /// public virtual Task AddArtistItemAsync(IArtistCollectionItem artistItem, int index, CancellationToken cancellationToken = default) => Inner.AddArtistItemAsync(artistItem, index, cancellationToken); /// public virtual Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => InnerUrlCollection.AddUrlAsync(url, index, cancellationToken); /// public virtual Task ChangeDescriptionAsync(string? description, CancellationToken cancellationToken = default) => InnerPlayable.ChangeDescriptionAsync(description, cancellationToken); /// public virtual Task ChangeDurationAsync(TimeSpan duration, CancellationToken cancellationToken = default) => InnerPlayable.ChangeDurationAsync(duration, cancellationToken); /// public virtual Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => InnerPlayable.ChangeNameAsync(name, cancellationToken); /// public virtual bool Equals(ICoreArtistCollectionItem other) => Inner.Equals(other); /// public virtual bool Equals(ICoreArtistCollection other) => Inner.Equals(other); /// public virtual bool Equals(ICoreImageCollection other) => InnerImageCollection.Equals(other); /// public virtual bool Equals(ICoreUrlCollection other) => InnerUrlCollection.Equals(other); /// public virtual IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => InnerImageCollection.GetImagesAsync(limit, offset, cancellationToken); /// public virtual IAsyncEnumerable GetArtistItemsAsync(int limit, int offset, CancellationToken cancellationToken = default) => Inner.GetArtistItemsAsync(limit, offset, cancellationToken); /// public virtual IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => InnerUrlCollection.GetUrlsAsync(limit, offset, cancellationToken); /// public virtual Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => InnerImageCollection.IsAddImageAvailableAsync(index, cancellationToken); /// public virtual Task IsAddArtistItemAvailableAsync(int index, CancellationToken cancellationToken = default) => Inner.IsAddArtistItemAvailableAsync(index, cancellationToken); /// public virtual Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => InnerUrlCollection.IsAddUrlAvailableAsync(index, cancellationToken); /// public virtual Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => InnerImageCollection.IsRemoveImageAvailableAsync(index, cancellationToken); /// public virtual Task IsRemoveArtistItemAvailableAsync(int index, CancellationToken cancellationToken = default) => Inner.IsRemoveArtistItemAvailableAsync(index, cancellationToken); /// public virtual Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => InnerUrlCollection.IsRemoveUrlAvailableAsync(index, cancellationToken); /// public virtual Task PauseArtistCollectionAsync(CancellationToken cancellationToken = default) => Inner.PauseArtistCollectionAsync(cancellationToken); /// public virtual Task PlayArtistCollectionAsync(IArtistCollectionItem artistItem, CancellationToken cancellationToken = default) => Inner.PlayArtistCollectionAsync(artistItem, cancellationToken); /// public virtual Task PlayArtistCollectionAsync(CancellationToken cancellationToken = default) => Inner.PlayArtistCollectionAsync(cancellationToken); /// public virtual Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => InnerImageCollection.RemoveImageAsync(index, cancellationToken); /// public virtual Task RemoveArtistItemAsync(int index, CancellationToken cancellationToken = default) => Inner.RemoveArtistItemAsync(index, cancellationToken); /// public virtual Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => InnerUrlCollection.RemoveUrlAsync(index, cancellationToken); /// public virtual Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default) => InnerDownloadable.StartDownloadOperationAsync(operation, cancellationToken); /// public virtual ValueTask DisposeAsync() { var uniqueInstances = new HashSet() { Inner, InnerDownloadable, InnerPlayable, InnerImageCollection, InnerUrlCollection, }; return new ValueTask(uniqueInstances.AsParallel() .Select(x => x.DisposeAsync().AsTask()) .Aggregate((x, y) => Task.WhenAll(x, y))); } } }