using Prism.Events;

namespace SHH.MjpegPlayer;

/// <summary>Prism 消息框架</summary>
public class PrismMsg<T>
{
    #region Defines

    public IEventAggregator _ea;

    private static PrismMsg<T>? _instance = null;

    #endregion

    #region Constructor

    /// <summary>构造函数</summary>
    private PrismMsg()
    {
        _ea = new EventAggregator();
    }

    #endregion

    #region Instance

    /// <summary>获取实例信息</summary>
    public static PrismMsg<T> Instance
    {
        get
        {
            if (_instance == null)
                _instance = new PrismMsg<T>();

            return _instance;
        }
    }

    #endregion

    #region Publish

    /// <summary>发送消息</summary>
    public static void Publish(T msg)
    {
        if (Instance == null)
            return;

        dynamic? data = msg;
        Instance._ea.GetEvent<PubSubEvent<T>>().Publish(data);
    }

    #endregion

    #region Subscribe

    /// <summary>订阅消息</summary>
    public static void Subscribe(Action<T> method)
    {
        if (Instance == null || Instance._ea == null)
            return;

        Instance._ea.GetEvent<PubSubEvent<T>>().Subscribe(method);
    }

    #endregion
}