// 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 .
///
///
/// User profiles are not actually merged (yet).
///
public sealed class UserProfileAdapter : IUserProfile
{
private readonly ICoreUserProfile _userProfile;
private readonly MergedCollectionMap _imageMap;
private readonly MergedCollectionMap _urlMap;
private readonly IReadOnlyList _sources;
///
/// Creates a new instance of .
///
public UserProfileAdapter(ICoreUserProfile userProfile)
{
_userProfile = userProfile ?? ThrowHelper.ThrowArgumentNullException(nameof(userProfile));
_sources = _userProfile.IntoList();
TotalImageCount = _userProfile.TotalImageCount;
TotalUrlCount = _userProfile.TotalUrlCount;
_imageMap = new MergedCollectionMap(this, new MergedCollectionConfig());
_urlMap = new MergedCollectionMap(this, new MergedCollectionConfig());
AttachEvents();
}
///
public event EventHandler? DisplayNameChanged
{
add => _userProfile.DisplayNameChanged += value;
remove => _userProfile.DisplayNameChanged -= value;
}
///
public event EventHandler? BirthDateChanged
{
add => _userProfile.BirthDateChanged += value;
remove => _userProfile.BirthDateChanged -= value;
}
///
public event EventHandler? FullNameChanged
{
add => _userProfile.FullNameChanged += value;
remove => _userProfile.FullNameChanged -= value;
}
///
public event EventHandler? RegionChanged
{
add => _userProfile.RegionChanged += value;
remove => _userProfile.RegionChanged -= value;
}
///
public event EventHandler? EmailChanged
{
add => _userProfile.EmailChanged += value;
remove => _userProfile.EmailChanged -= value;
}
///
public event CollectionChangedEventHandler? ImagesChanged;
///
public event CollectionChangedEventHandler? UrlsChanged;
///
public event EventHandler? ImagesCountChanged;
///
public event EventHandler? UrlsCountChanged;
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 => _userProfile.Id;
///
public string DisplayName => _userProfile.DisplayName;
///
public string? FullName => _userProfile.FullName;
///
public string? Email => _userProfile.Email;
///
public DateTime? Birthdate => _userProfile.Birthdate;
///
public CultureInfo Region => _userProfile.Region;
///
public bool IsChangeDisplayNameAvailable => _userProfile.IsChangeDisplayNameAvailable;
///
public bool IsChangeBirthDateAsyncAvailable => _userProfile.IsChangeBirthDateAsyncAvailable;
///
public bool IsChangeFullNameAsyncAsyncAvailable => _userProfile.IsChangeFullNameAsyncAsyncAvailable;
///
public bool IsChangeRegionAsyncAvailable => _userProfile.IsChangeRegionAsyncAvailable;
///
public bool IsChangeEmailAsyncAvailable => _userProfile.IsChangeEmailAsyncAvailable;
///
public Task IsAddImageAvailableAsync(int index, CancellationToken cancellationToken = default)
{
return _userProfile.IsAddImageAvailableAsync(index, cancellationToken);
}
///
public Task IsRemoveImageAvailableAsync(int index, CancellationToken cancellationToken = default)
{
return _userProfile.IsRemoveImageAvailableAsync(index, cancellationToken);
}
///
public Task IsAddUrlAvailableAsync(int index, CancellationToken cancellationToken = default)
{
return _userProfile.IsAddUrlAvailableAsync(index, cancellationToken);
}
///
public Task IsRemoveUrlAvailableAsync(int index, CancellationToken cancellationToken = default)
{
return _userProfile.IsRemoveUrlAvailableAsync(index, cancellationToken);
}
///
public Task ChangeDisplayNameAsync(string displayName, CancellationToken cancellationToken = default)
{
return _userProfile.ChangeDisplayNameAsync(displayName, cancellationToken);
}
///
public Task ChangeBirthDateAsync(DateTime birthdate, CancellationToken cancellationToken = default)
{
return _userProfile.ChangeBirthDateAsync(birthdate, cancellationToken);
}
///
public Task ChangeFullNameAsync(string fullname, CancellationToken cancellationToken = default)
{
return _userProfile.ChangeFullNameAsync(fullname, cancellationToken);
}
///
public Task ChangeRegionAsync(CultureInfo region, CancellationToken cancellationToken = default)
{
return _userProfile.ChangeRegionAsync(region, cancellationToken);
}
///
public Task ChangeEmailAsync(string? email, CancellationToken cancellationToken = default)
{
return _userProfile.ChangeEmailAsync(email, cancellationToken);
}
///
/// The sources that were combined to form the data in this instance.
///
///
/// Merging of users and user profiles are TODO.
///
public IReadOnlyList Sources => _sources;
///
public event EventHandler? SourcesChanged;
///
IReadOnlyList IMerged.Sources => _sources;
///
IReadOnlyList IMerged.Sources => _sources;
///
public IAsyncEnumerable GetImagesAsync(int limit, int offset, CancellationToken cancellationToken = default)
{
return _imageMap.GetItemsAsync(limit, offset, cancellationToken);
}
///
public IAsyncEnumerable GetUrlsAsync(int limit, int offset, CancellationToken cancellationToken = default)
{
return _urlMap.GetItemsAsync(limit, offset, cancellationToken);
}
///
public Task AddImageAsync(IImage image, int index, CancellationToken cancellationToken = default)
{
return _imageMap.InsertItemAsync(image, index, cancellationToken);
}
///
public Task RemoveImageAsync(int index, CancellationToken cancellationToken = default)
{
return _userProfile.RemoveImageAsync(index, cancellationToken);
}
///
public Task AddUrlAsync(IUrl url, int index, CancellationToken cancellationToken = default)
{
return _urlMap.InsertItemAsync(url, index, cancellationToken);
}
///
public Task RemoveUrlAsync(int index, CancellationToken cancellationToken = default)
{
return _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 _userProfile.DisposeAsync();
}
}
}