// 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 OwlCore.Extensions;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.BaseModels;
using StrixMusic.Sdk.CoreModels;
using StrixMusic.Sdk.Extensions;
using StrixMusic.Sdk.MediaPlayback;
namespace StrixMusic.Sdk.AdapterModels
{
///
/// Merged multiple into a single
///
public class MergedPlaylist : IPlaylist, IMergedMutable, IMergedMutable
{
private readonly List _sources;
private readonly ICorePlaylist _preferredSource;
private readonly MergedCollectionMap _trackCollectionMap;
private readonly MergedCollectionMap _imageCollectionMap;
private readonly MergedCollectionMap _urlCollectionMap;
///
/// Creates a new instance of .
///
public MergedPlaylist(IEnumerable sources, MergedCollectionConfig config)
{
_sources = sources.ToList();
_trackCollectionMap = new MergedCollectionMap(this, config);
_imageCollectionMap = new MergedCollectionMap(this, config);
_urlCollectionMap = new MergedCollectionMap(this, config);
// TODO: Get the actual preferred source.
_preferredSource = _sources[0];
Name = _preferredSource.Name;
Description = _preferredSource.Description;
PlaybackState = _preferredSource.PlaybackState;
LastPlayed = _preferredSource.LastPlayed;
AddedAt = _preferredSource.AddedAt;
foreach (var source in _sources)
{
TotalImageCount += source.TotalImageCount;
TotalTrackCount += source.TotalTrackCount;
TotalUrlCount += source.TotalUrlCount;
}
if (_preferredSource.RelatedItems != null)
{
RelatedItems = new MergedPlayableCollectionGroup(_preferredSource.RelatedItems.IntoList(), config);
}
if (_preferredSource.Owner != null)
{
Owner = new UserProfileAdapter(_preferredSource.Owner);
}
AttachEvents(_preferredSource);
}
///
public event CollectionChangedEventHandler? TracksChanged;
///
public event EventHandler? TracksCountChanged;
///
public event EventHandler? PlaybackStateChanged;
///
public event EventHandler? NameChanged;
///
public event EventHandler? DescriptionChanged;
///
public event EventHandler? DurationChanged;
///
public event EventHandler? LastPlayedChanged;
///
public event EventHandler? IsPlayTrackCollectionAsyncAvailableChanged;
///
public event EventHandler? IsPauseTrackCollectionAsyncAvailableChanged;
///
public event EventHandler? IsChangeNameAsyncAvailableChanged;
///
public event EventHandler? IsChangeDescriptionAsyncAvailableChanged;
///
public event EventHandler? IsChangeDurationAsyncAvailableChanged;
///
public event CollectionChangedEventHandler? ImagesChanged;
///
public event EventHandler? ImagesCountChanged;
///
public event CollectionChangedEventHandler? UrlsChanged;
///
public event EventHandler? UrlsCountChanged;
///
public event EventHandler? DownloadInfoChanged;
///
public event EventHandler? SourcesChanged;
private void AttachEvents(ICorePlaylist source)
{
AttachPlayableEvents(source);
source.IsPlayTrackCollectionAsyncAvailableChanged += IsPlayTrackCollectionAsyncAvailableChanged;
source.IsPauseTrackCollectionAsyncAvailableChanged += IsPauseTrackCollectionAsyncAvailableChanged;
_trackCollectionMap.ItemsCountChanged += TrackCollectionMap_ItemsCountChanged;
_trackCollectionMap.ItemsChanged += TrackCollectionMap_ItemsChanged;
_imageCollectionMap.ItemsChanged += ImageCollectionMap_ItemsChanged;
_imageCollectionMap.ItemsCountChanged += ImageCollectionMap_ItemsCountChanged;
_urlCollectionMap.ItemsChanged += UrlCollectionMap_ItemsChanged;
_urlCollectionMap.ItemsCountChanged += UrlCollectionMap_ItemsCountChanged;
}
private void DetachEvents(ICorePlaylist source)
{
DetachPlayableEvents(source);
source.IsPlayTrackCollectionAsyncAvailableChanged += IsPlayTrackCollectionAsyncAvailableChanged;
source.IsPauseTrackCollectionAsyncAvailableChanged += IsPauseTrackCollectionAsyncAvailableChanged;
_trackCollectionMap.ItemsChanged -= TrackCollectionMap_ItemsChanged;
_trackCollectionMap.ItemsCountChanged -= TrackCollectionMap_ItemsCountChanged;
_imageCollectionMap.ItemsChanged -= ImageCollectionMap_ItemsChanged;
_imageCollectionMap.ItemsCountChanged -= ImageCollectionMap_ItemsCountChanged;
_urlCollectionMap.ItemsChanged -= UrlCollectionMap_ItemsChanged;
_urlCollectionMap.ItemsCountChanged -= UrlCollectionMap_ItemsCountChanged;
}
private void AttachPlayableEvents(IPlayableBase source)
{
source.PlaybackStateChanged += PlaybackStateChanged;
source.NameChanged += NameChanged;
source.DescriptionChanged += DescriptionChanged;
source.DurationChanged += DurationChanged;
source.LastPlayedChanged += LastPlayedChanged;
source.IsChangeNameAsyncAvailableChanged += IsChangeNameAsyncAvailableChanged;
source.IsChangeDurationAsyncAvailableChanged += IsChangeDurationAsyncAvailableChanged;
source.IsChangeDescriptionAsyncAvailableChanged += IsChangeDescriptionAsyncAvailableChanged;
}
private void DetachPlayableEvents(IPlayableBase source)
{
source.PlaybackStateChanged -= PlaybackStateChanged;
source.NameChanged -= NameChanged;
source.DescriptionChanged -= DescriptionChanged;
source.DurationChanged -= DurationChanged;
source.LastPlayedChanged -= LastPlayedChanged;
source.IsChangeNameAsyncAvailableChanged -= IsChangeNameAsyncAvailableChanged;
source.IsChangeDurationAsyncAvailableChanged -= IsChangeDurationAsyncAvailableChanged;
source.IsChangeDescriptionAsyncAvailableChanged -= IsChangeDescriptionAsyncAvailableChanged;
}
private void TrackCollectionMap_ItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems)
{
TracksChanged?.Invoke(this, addedItems, removedItems);
}
private void TrackCollectionMap_ItemsCountChanged(object sender, int e)
{
TotalTrackCount = e;
TracksCountChanged?.Invoke(this, e);
}
private void ImageCollectionMap_ItemsCountChanged(object sender, int e)
{
TotalImageCount = e;
ImagesCountChanged?.Invoke(this, e);
}
private void ImageCollectionMap_ItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems)
{
ImagesChanged?.Invoke(this, addedItems, removedItems);
}
private void UrlCollectionMap_ItemsCountChanged(object sender, int e)
{
TotalUrlCount = e;
UrlsCountChanged?.Invoke(this, e);
}
private void UrlCollectionMap_ItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems)
{
UrlsChanged?.Invoke(this, addedItems, removedItems);
}
///
public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => _imageCollectionMap.InsertItemAsync(image, index, cancellationToken);
///
public Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => _urlCollectionMap.InsertItemAsync(url, index, cancellationToken);
///
public void AddSource(ICorePlaylist itemToMerge)
{
Guard.IsNotNull(itemToMerge, nameof(itemToMerge));
if (!Equals(itemToMerge))
ThrowHelper.ThrowArgumentException(nameof(itemToMerge), "Tried to merge an artist that doesn't match. Verify that the item matches before merging the source.");
_sources.Add(itemToMerge);
_trackCollectionMap.AddSource(itemToMerge);
_imageCollectionMap.AddSource(itemToMerge);
SourcesChanged?.Invoke(this, EventArgs.Empty);
}
///
public void RemoveSource(ICorePlaylist itemToRemove)
{
Guard.IsNotNull(itemToRemove, nameof(itemToRemove));
_sources.Remove(itemToRemove);
_trackCollectionMap.RemoveSource(itemToRemove);
_imageCollectionMap.RemoveSource(itemToRemove);
SourcesChanged?.Invoke(this, EventArgs.Empty);
}
///
public void AddSource(ICorePlaylistCollectionItem itemToMerge) => AddSource((ICorePlaylist)itemToMerge);
///
public void RemoveSource(ICorePlaylistCollectionItem itemToRemove) => RemoveSource((ICorePlaylist)itemToRemove);
///
public Task AddTrackAsync(ITrack track, int index, CancellationToken cancellationToken = default) => _trackCollectionMap.InsertItemAsync(track, index, cancellationToken);
///
public Task ChangeDescriptionAsync(string? description, CancellationToken cancellationToken = default) => _preferredSource.ChangeDescriptionAsync(description, cancellationToken);
///
public Task ChangeDurationAsync(TimeSpan duration, CancellationToken cancellationToken = default) => _preferredSource.ChangeDurationAsync(duration, cancellationToken);
///
public Task ChangeNameAsync(string name, CancellationToken cancellationToken = default) => _preferredSource.ChangeNameAsync(name, cancellationToken);
///
public IAsyncEnumerable GetTracksAsync(int limit, int offset, CancellationToken cancellationToken = default) => _trackCollectionMap.GetItemsAsync(limit, offset, cancellationToken);
///
public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => _imageCollectionMap.GetItemsAsync(limit, offset, cancellationToken);
///
public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => _urlCollectionMap.GetItemsAsync(limit, offset, cancellationToken);
///
public Task IsAddTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => _trackCollectionMap.IsAddItemAvailableAsync(index, cancellationToken);
///
public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _imageCollectionMap.IsAddItemAvailableAsync(index, cancellationToken);
///
public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _urlCollectionMap.IsAddItemAvailableAsync(index, cancellationToken);
///
public Task IsRemoveTrackAvailableAsync(int index, CancellationToken cancellationToken = default) => _trackCollectionMap.IsRemoveItemAvailableAsync(index, cancellationToken);
///
public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _imageCollectionMap.IsRemoveItemAvailableAsync(index, cancellationToken);
///
public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _urlCollectionMap.IsRemoveItemAvailableAsync(index, cancellationToken);
///
public Task PauseTrackCollectionAsync(CancellationToken cancellationToken) => _preferredSource.PauseTrackCollectionAsync(cancellationToken);
///
public Task PlayTrackCollectionAsync(CancellationToken cancellationToken) => _preferredSource.PlayTrackCollectionAsync(cancellationToken);
///
public Task PlayTrackCollectionAsync(ITrack track, CancellationToken cancellationToken = default)
{
var targetCore = _preferredSource.SourceCore;
var source = track.GetSources().FirstOrDefault(x => x.SourceCore.InstanceId == targetCore.InstanceId);
Guard.IsNotNull(source, nameof(source));
return _preferredSource.PlayTrackCollectionAsync(source, cancellationToken);
}
///
public Task RemoveTrackAsync(int index, CancellationToken cancellationToken = default) => _trackCollectionMap.RemoveAtAsync(index, cancellationToken);
///
public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => _imageCollectionMap.RemoveAtAsync(index, cancellationToken);
///
public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => _imageCollectionMap.RemoveAtAsync(index, cancellationToken);
///
public Task StartDownloadOperationAsync(DownloadOperation operation, CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
///
public IUserProfile? Owner { get; }
///
public IPlayableCollectionGroup? RelatedItems { get; }
///
public int TotalTrackCount { get; internal set; }
///
public int TotalImageCount { get; internal set; }
///
public int TotalUrlCount { get; internal set; }
///
public string Id => _preferredSource.Id;
///
public string Name { get; internal set; }
///
public string? Description { get; internal set; }
///
public PlaybackState PlaybackState { get; internal set; }
///
public DownloadInfo DownloadInfo => default;
///
public TimeSpan Duration { get; internal set; }
///
public DateTime? LastPlayed { get; internal set; }
///
public DateTime? AddedAt { get; internal set; }
///
public bool IsPlayTrackCollectionAsyncAvailable => _preferredSource.IsPlayTrackCollectionAsyncAvailable;
///
public bool IsPauseTrackCollectionAsyncAvailable => _preferredSource.IsPauseTrackCollectionAsyncAvailable;
///
public bool IsChangeNameAsyncAvailable => _preferredSource.IsChangeDescriptionAsyncAvailable;
///
public bool IsChangeDescriptionAsyncAvailable => _preferredSource.IsChangeDescriptionAsyncAvailable;
///
public bool IsChangeDurationAsyncAvailable => _preferredSource.IsChangeDescriptionAsyncAvailable;
///
IReadOnlyList IMerged.Sources => Sources;
///
IReadOnlyList IMerged.Sources => Sources;
///
IReadOnlyList IMerged.Sources => Sources;
///
IReadOnlyList IMerged.Sources => Sources;
///
public IReadOnlyList Sources => _sources;
///
public bool Equals(ICoreImageCollection other) => Equals(other as ICorePlaylistCollectionItem);
///
public bool Equals(ICoreUrlCollection other) => Equals(other as ICorePlaylistCollectionItem);
///
public bool Equals(ICoreTrackCollection other) => Equals(other as ICorePlaylistCollectionItem);
///
public bool Equals(ICorePlaylist? other)
{
return other?.Name == Name;
}
///
public bool Equals(ICorePlaylistCollectionItem? other)
{
if (other is ICorePlaylist corePlaylist)
return Equals(corePlaylist);
return false;
}
///
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || (obj is ICorePlaylistCollectionItem other && Equals(other));
}
///
public override int GetHashCode()
{
return _preferredSource.GetHashCode();
}
///
public async ValueTask DisposeAsync()
{
DetachEvents(_preferredSource);
await _sources.InParallel(x => x.DisposeAsync().AsTask());
await _imageCollectionMap.DisposeAsync();
await _trackCollectionMap.DisposeAsync();
}
}
}