using SharedInterfaces;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using WpfMediaPlayer.ViewModels;

namespace WpfMediaPlayer
{
    public class PluginManager
    {
        public ObservableCollection<IPluginBase> IPlugins { get; private set; } = new ObservableCollection<IPluginBase>();
        public ObservableCollection<IPluginWithUI> IPluginsWithUI { get; private set; } = new ObservableCollection<IPluginWithUI>();
        public ObservableCollection<PluginViewModel> PluginViewModels { get; private set; } = new ObservableCollection<PluginViewModel>();

        /// <summary>
        /// 加载指定路径中的所有插件DLL
        /// </summary>
        /// <param name="pluginsPath">插件文件夹路径</param>
        public void LoadPlugins(string pluginsPath)
        {
            if (!Directory.Exists(pluginsPath))
                return;

            var dllFiles = Directory.GetFiles(pluginsPath, "*.dll");
            foreach (var dll in dllFiles)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFrom(dll);
                    var types = assembly.GetTypes();

                    foreach (var type in types)
                    {
                        if (typeof(IPluginWithUI).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
                        {
                            // 插件实现了IPluginWithUI
                            IPluginWithUI pluginWithUI = (IPluginWithUI)Activator.CreateInstance(type);
                            pluginWithUI.Initialize();
                            IPlugins.Add(pluginWithUI);
                            IPluginsWithUI.Add(pluginWithUI);
                            PluginViewModels.Add(new PluginViewModel(pluginWithUI));
                            Console.WriteLine($"Loaded IPluginWithUI: {pluginWithUI.PluginName}");
                        }
                        else if (typeof(IPluginBase).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
                        {
                            // 插件仅实现了IPluginBase
                            IPluginBase pluginBase = (IPluginBase)Activator.CreateInstance(type);
                            pluginBase.Initialize();
                            IPlugins.Add(pluginBase);
                            PluginViewModels.Add(new PluginViewModel(pluginBase));
                            Console.WriteLine($"Loaded IPluginBase: {pluginBase.PluginName}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to load plugin from {dll}: {ex.Message}");
                }
            }
        }

        /// <summary>
        /// 卸载所有插件
        /// </summary>
        public void UnloadPlugins()
        {
            // 卸载IPluginWithUI插件
            foreach (var pluginWithUI in IPluginsWithUI)
            {
                pluginWithUI.GetControl()?.Dispatcher.Invoke(() => pluginWithUI.GetControl().ClearValue(UserControl.ContentProperty));
            }
            IPluginsWithUI.Clear();

            // 卸载IPluginBase插件
            IPlugins.Clear();
            PluginViewModels.Clear();
        }

        /// <summary>
        /// 添加一个IPluginBase插件
        /// </summary>
        /// <param name="plugin">插件实例</param>
        public void AddIPlugin(IPluginBase plugin)
        {
            if (plugin != null)
            {
                plugin.Initialize();
                IPlugins.Add(plugin);
                PluginViewModels.Add(new PluginViewModel(plugin));
                Console.WriteLine($"Loaded IPluginBase: {plugin.PluginName}");
            }
        }

        /// <summary>
        /// 添加一个IPluginWithUI插件
        /// </summary>
        /// <param name="plugin">插件实例</param>
        public void AddIPluginWithUI(IPluginWithUI plugin)
        {
            if (plugin != null)
            {
                plugin.Initialize();
                IPlugins.Add(plugin);
                IPluginsWithUI.Add(plugin);
                PluginViewModels.Add(new PluginViewModel(plugin));
                Console.WriteLine($"Loaded IPluginWithUI: {plugin.PluginName}");
            }
        }
    }
}
using System.ComponentModel;
using SharedInterfaces;

namespace WpfMediaPlayer.ViewModels
{
    public class PluginParameterViewModel : INotifyPropertyChanged
    {
        private double _value;

        public string Name { get; set; }
        public string DisplayName { get; set; }
        public double Value
        {
            get => _value;
            set
            {
                if (_value != value)
                {
                    _value = value;
                    OnPropertyChanged(nameof(Value));
                    // 更新插件的参数
                    if (Plugin != null)
                    {
                        Plugin.Parameters[Name] = value;
                    }
                }
            }
        }
        public double Minimum { get; set; }
        public double Maximum { get; set; }

        public IPluginWithUI Plugin { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using SharedInterfaces;
using System.Windows.Controls;

namespace WpfMediaPlayer.ViewModels
{
    public class PluginViewModel : INotifyPropertyChanged
    {
        private bool _isEnabled;

        public string PluginName => Plugin.PluginName;

        public bool IsEnabled
        {
            get => _isEnabled;
            set
            {
                if (_isEnabled != value)
                {
                    _isEnabled = value;
                    Plugin.IsEnabled = value;
                    OnPropertyChanged(nameof(IsEnabled));
                }
            }
        }

        public ObservableCollection<PluginParameterViewModel> Parameters { get; set; } = new ObservableCollection<PluginParameterViewModel>();

        public IPluginBase Plugin { get; set; }

        public UserControl Control
        {
            get
            {
                if (Plugin is IPluginWithUI pluginWithUI)
                {
                    return pluginWithUI.GetControl();
                }
                return null;
            }
        }

        public PluginViewModel(IPluginBase plugin)
        {
            Plugin = plugin;
            IsEnabled = plugin.IsEnabled;

            if (plugin is IPluginWithUI pluginWithUI)
            {
                foreach (var param in pluginWithUI.Parameters)
                {
                    Parameters.Add(new PluginParameterViewModel
                    {
                        Name = param.Key,
                        DisplayName = param.Key, // 可以根据需要更改显示名称
                        Value = Convert.ToDouble(param.Value),
                        Minimum = 0, // 根据插件定义设置
                        Maximum = 2, // 根据插件定义设置
                        Plugin = pluginWithUI
                    });
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }