// 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.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Diagnostics;
using OwlCore.Extensions;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.BaseModels;
using StrixMusic.Sdk.CoreModels;
using StrixMusic.Sdk.MediaPlayback;
namespace StrixMusic.Sdk.AdapterModels
{
///
/// Translates a to a . Does not provide merging.
///
public sealed class DeviceAdapter : IDevice
{
private readonly ICoreDevice _source;
///
/// Creates a new instance of .
///
///
public DeviceAdapter(ICoreDevice source)
{
_source = source;
Name = _source.Name;
IsActive = _source.IsActive;
PlaybackContext = _source.PlaybackContext;
Position = _source.Position;
PlaybackState = _source.PlaybackState;
ShuffleState = _source.ShuffleState;
RepeatState = _source.RepeatState;
Volume = _source.Volume;
PlaybackSpeed = _source.PlaybackSpeed;
Guard.IsNotNull(_source.NowPlaying,nameof(_source.NowPlaying));
var nowPlaying = new MergedTrack(_source.NowPlaying.IntoList(), new MergedCollectionConfig());
NowPlaying = new PlaybackItem()
{
Track = nowPlaying,
};
if (!(_source.PlaybackQueue is null))
PlaybackQueue = new MergedTrackCollection(_source.PlaybackQueue.IntoList(), new MergedCollectionConfig());
AttachEvents();
}
private void AttachEvents()
{
_source.NowPlayingChanged += Source_NowPlayingChanged;
}
private void DetachEvents()
{
_source.NowPlayingChanged -= Source_NowPlayingChanged;
}
private void Source_NowPlayingChanged(object sender, ICoreTrack e)
{
var nowPlaying = new MergedTrack(e.IntoList(), new MergedCollectionConfig());
NowPlaying = new PlaybackItem
{
Track = nowPlaying,
};
NowPlayingChanged?.Invoke(sender, NowPlaying);
}
///
public event EventHandler? IsActiveChanged
{
add => _source.IsActiveChanged += value;
remove => _source.IsActiveChanged -= value;
}
///
public event EventHandler? PlaybackContextChanged
{
add => _source.PlaybackContextChanged += value;
remove => _source.PlaybackContextChanged -= value;
}
///
public event EventHandler? ShuffleStateChanged
{
add => _source.ShuffleStateChanged += value;
remove => _source.ShuffleStateChanged -= value;
}
///
public event EventHandler? RepeatStateChanged
{
add => _source.RepeatStateChanged += value;
remove => _source.RepeatStateChanged -= value;
}
///
public event EventHandler? PositionChanged
{
add => _source.PositionChanged += value;
remove => _source.PositionChanged -= value;
}
///
public event EventHandler? PlaybackStateChanged
{
add => _source.PlaybackStateChanged += value;
remove => _source.PlaybackStateChanged -= value;
}
///
public event EventHandler? VolumeChanged
{
add => _source.VolumeChanged += value;
remove => _source.VolumeChanged -= value;
}
///
public event EventHandler? PlaybackSpeedChanged
{
add => _source.PlaybackSpeedChanged += value;
remove => _source.PlaybackSpeedChanged -= value;
}
///
public event EventHandler? NowPlayingChanged;
///
public string Id => _source.Id;
///
public string Name { get; internal set; }
///
public bool IsActive { get; internal set; }
///
public DeviceType Type { get; internal set; }
///
public double PlaybackSpeed { get; internal set; }
///
public IPlayableBase? PlaybackContext { get; internal set; }
///
public double Volume { get; internal set; }
///
public bool ShuffleState { get; internal set; }
///
public RepeatState RepeatState { get; internal set; }
///
public TimeSpan Position { get; internal set; }
///
public PlaybackState PlaybackState { get; internal set; }
///
public bool IsSeekAsyncAvailable => _source.IsSeekAsyncAvailable;
///
public bool IsResumeAsyncAvailable => _source.IsResumeAsyncAvailable;
///
public bool IsPauseAsyncAvailable => _source.IsPauseAsyncAvailable;
///
public bool IsChangeVolumeAsyncAvailable => _source.IsChangeVolumeAsyncAvailable;
///
public bool IsChangePlaybackSpeedAvailable => _source.IsChangePlaybackSpeedAvailable;
///
public bool IsNextAsyncAvailable => _source.IsNextAsyncAvailable;
///
public bool IsPreviousAsyncAvailable => _source.IsPreviousAsyncAvailable;
///
public bool IsToggleShuffleAsyncAvailable => _source.IsToggleShuffleAsyncAvailable;
///
public bool IsToggleRepeatAsyncAvailable => _source.IsToggleRepeatAsyncAvailable;
///
public Task NextAsync(CancellationToken cancellationToken = default) => _source.NextAsync(cancellationToken);
///
public Task PreviousAsync(CancellationToken cancellationToken = default) => _source.PreviousAsync(cancellationToken);
///
public Task ToggleShuffleAsync(CancellationToken cancellationToken = default) => _source.ToggleShuffleAsync(cancellationToken);
///
public Task ToggleRepeatAsync(CancellationToken cancellationToken = default) => _source.ToggleRepeatAsync(cancellationToken);
///
public Task SeekAsync(TimeSpan position, CancellationToken cancellationToken = default) => _source.SeekAsync(position, cancellationToken);
///
public Task SwitchToAsync(CancellationToken cancellationToken = default) => _source.SwitchToAsync(cancellationToken);
///
public Task ChangePlaybackSpeedAsync(double speed, CancellationToken cancellationToken = default) => _source.ChangePlaybackSpeedAsync(speed, cancellationToken);
///
public Task ResumeAsync(CancellationToken cancellationToken = default) => _source.ResumeAsync(cancellationToken);
///
public Task PauseAsync(CancellationToken cancellationToken = default) => _source.PauseAsync(cancellationToken);
///
public Task ChangeVolumeAsync(double volume, CancellationToken cancellationToken = default) => _source.ChangeVolumeAsync(volume, cancellationToken);
///
public ICore? SourceCore => _source.SourceCore;
///
public ICoreDevice? Source => _source;
///
public ITrackCollection? PlaybackQueue { get; }
///
public PlaybackItem? NowPlaying { get; private set; }
///
public ValueTask DisposeAsync() => _source.DisposeAsync();
}
}