// 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.Globalization;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Diagnostics;
using OwlCore.Events;
using OwlCore.Extensions;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.CoreModels;
namespace StrixMusic.Sdk.AdapterModels
{
///
/// A class that handles turning a into a .
///
///
/// Users are not actually merged.
///
public sealed class UserAdapter : IUser
{
private readonly ICoreUser _user;
private readonly MergedCollectionMap _imageMap;
private readonly MergedCollectionMap _urlMap;
private readonly IReadOnlyList _sources;
///
/// Creates a new instance of .
///
public UserAdapter(ICoreUser user)
{
_user = user ?? ThrowHelper.ThrowArgumentNullException(nameof(user));
SourceCore = _user.SourceCore;
TotalImageCount = user.TotalImageCount;
TotalUrlCount = user.TotalUrlCount;
Library = new MergedLibrary(_user.Library.IntoList(), new MergedCollectionConfig());
_user.SourceCore.IntoList();
_sources = _user.IntoList();
_imageMap = new MergedCollectionMap(this, new MergedCollectionConfig());
_urlMap = new MergedCollectionMap(this, new MergedCollectionConfig());
AttachEvents();
}
///
public ICore? SourceCore { get; set; }
///
public ICoreUser? Source { get; set; }
///
public event EventHandler? DisplayNameChanged
{
add => _user.DisplayNameChanged += value;
remove => _user.DisplayNameChanged -= value;
}
///
public event EventHandler? BirthDateChanged
{
add => _user.BirthDateChanged += value;
remove => _user.BirthDateChanged -= value;
}
///
public event EventHandler? FullNameChanged
{
add => _user.FullNameChanged += value;
remove => _user.FullNameChanged -= value;
}
///
public event EventHandler? RegionChanged
{
add => _user.RegionChanged += value;
remove => _user.RegionChanged -= value;
}
///
public event EventHandler? EmailChanged
{
add => _user.EmailChanged += value;
remove => _user.EmailChanged -= value;
}
///
public event CollectionChangedEventHandler? ImagesChanged;
///
public event CollectionChangedEventHandler? UrlsChanged;
///
public event EventHandler? ImagesCountChanged;
///
public event EventHandler? UrlsCountChanged;
///
public event EventHandler? SourcesChanged;
private void AttachEvents()
{
_imageMap.ItemsChanged += ImageMap_ItemsChanged;
_imageMap.ItemsCountChanged += ImageMap_ItemsCountChanged;
_urlMap.ItemsChanged += UrlMap_ItemsChanged;
_urlMap.ItemsCountChanged += UrlMap_ItemsCountChanged;
}
private void DetachEvents()
{
_imageMap.ItemsChanged -= ImageMap_ItemsChanged;
_imageMap.ItemsCountChanged -= ImageMap_ItemsCountChanged;
_urlMap.ItemsChanged -= UrlMap_ItemsChanged;
_urlMap.ItemsCountChanged -= UrlMap_ItemsCountChanged;
}
private void ImageMap_ItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems)
=> ImagesChanged?.Invoke(this, addedItems, removedItems);
private void UrlMap_ItemsChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems)
=> UrlsChanged?.Invoke(this, addedItems, removedItems);
private void UrlMap_ItemsCountChanged(object sender, int e)
{
TotalUrlCount = e;
UrlsCountChanged?.Invoke(this, TotalUrlCount);
}
private void ImageMap_ItemsCountChanged(object sender, int e)
{
TotalImageCount = e;
ImagesCountChanged?.Invoke(this, TotalImageCount);
}
///
public int TotalImageCount { get; private set; }
///
public int TotalUrlCount { get; private set; }
///
public string Id => _user.Id;
///
public string DisplayName => _user.DisplayName;
///
public string? FullName => _user.FullName;
///
public string? Email => _user.Email;
///
public DateTime? Birthdate => _user.Birthdate;
///
public CultureInfo Region => _user.Region;
///
public bool IsChangeDisplayNameAvailable => _user.IsChangeDisplayNameAvailable;
///
public bool IsChangeBirthDateAsyncAvailable => _user.IsChangeBirthDateAsyncAvailable;
///
public bool IsChangeFullNameAsyncAsyncAvailable => _user.IsChangeFullNameAsyncAsyncAvailable;
///
public bool IsChangeRegionAsyncAvailable => _user.IsChangeRegionAsyncAvailable;
///
public bool IsChangeEmailAsyncAvailable => _user.IsChangeEmailAsyncAvailable;
///
public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _user.IsAddImageAvailableAsync(index, cancellationToken);
///
public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default) => _user.IsRemoveImageAvailableAsync(index, cancellationToken);
///
public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _user.IsAddUrlAvailableAsync(index, cancellationToken);
///
public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default) => _user.IsRemoveUrlAvailableAsync(index, cancellationToken);
///
public Task ChangeDisplayNameAsync(string displayName, CancellationToken cancellationToken = default) => _user.ChangeDisplayNameAsync(displayName, cancellationToken);
///
public Task ChangeBirthDateAsync(DateTime birthdate, CancellationToken cancellationToken = default) => _user.ChangeBirthDateAsync(birthdate, cancellationToken);
///
public Task ChangeFullNameAsync(string fullname, CancellationToken cancellationToken = default) => _user.ChangeFullNameAsync(fullname, cancellationToken);
///
public Task ChangeRegionAsync(CultureInfo region, CancellationToken cancellationToken = default) => _user.ChangeRegionAsync(region, cancellationToken);
///
public Task ChangeEmailAsync(string? email, CancellationToken cancellationToken = default) => _user.ChangeEmailAsync(email, cancellationToken);
///
IReadOnlyList IMerged.Sources => _sources;
///
IReadOnlyList IMerged.Sources => _sources;
///
public ILibrary Library { get; }
///
public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default) => _imageMap.GetItemsAsync(limit, offset, cancellationToken);
///
public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default) => _urlMap.GetItemsAsync(limit, offset, cancellationToken);
///
public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default) => _imageMap.InsertItemAsync(image, index, cancellationToken);
///
public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default) => _user.RemoveImageAsync(index, cancellationToken);
///
public Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default) => _urlMap.InsertItemAsync(url, index, cancellationToken);
///
public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default) => _urlMap.RemoveAtAsync(index, cancellationToken);
/// Indicates whether the current object is equal to another object of the same type.
/// An object to compare with this object.
/// false. User profiles are never merged.
public bool Equals(ICoreImageCollection other) =>
false;
/// Indicates whether the current object is equal to another object of the same type.
/// An object to compare with this object.
/// false. User profiles are never merged.
public bool Equals(ICoreUrlCollection other) =>
false;
///
public ValueTask DisposeAsync()
{
DetachEvents();
return _user.DisposeAsync();
}
}
}