// 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.Threading.Tasks;
using StrixMusic.Sdk.AdapterModels;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.CoreModels;
using StrixMusic.Sdk.Plugins.Model;
namespace StrixMusic.Sdk.PluginModels;
///
/// Wraps an instance of with the provided plugins.
///
public class GenrePluginWrapper : IGenre, IPluginWrapper
{
private readonly IGenre _genre;
///
/// Initializes a new instance of the class.
///
/// The instance to wrap around and apply plugins to.
/// The plugins that are applied to items returned from or emitted by this collection.
internal GenrePluginWrapper(IGenre genre, params SdkModelPlugin[] plugins)
{
foreach (var item in plugins)
ActivePlugins.Import(item);
ActivePlugins = GlobalModelPluginConnector.Create(ActivePlugins);
_genre = genre;
AttachEvents(_genre);
}
///
public SdkModelPlugin ActivePlugins { get; } = new(PluginModelWrapperInfo.Metadata);
private void AttachEvents(IGenre genre)
{
genre.SourcesChanged += OnSourcesChanged;
}
private void DetachEvents(IGenre genre)
{
genre.SourcesChanged -= OnSourcesChanged;
}
private void OnSourcesChanged(object sender, EventArgs e) => SourcesChanged?.Invoke(sender, e);
///
public event EventHandler? SourcesChanged;
///
public bool Equals(ICoreGenre other) => _genre.Equals(other);
///
public IReadOnlyList Sources => _genre.Sources;
///
public string Name => _genre.Name;
///
public ValueTask DisposeAsync()
{
DetachEvents(_genre);
return _genre.DisposeAsync();
}
}