#region << 版 本 注 释 >>

/*--------------------------------------------------------------------
* 版权所有 (c) 2025 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：Mr.Wen's MacBook Pro
* 命名空间：Sln.Iot.CFX
* 唯一标识：B906AE83-E238-4BF5-A269-1A48D38158CC
*
* 创建者：WenJY
* 电子邮箱：
* 创建时间：2025-10-24 15:26:56
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System.Diagnostics;
using CFX;
using CFX.Production;
using CFX.ResourcePerformance;
using CFX.Structures;
using CFX.Transport;
using Sln.Iot.CFX.RequestReceived;

namespace Sln.Iot.CFX;

/// <summary>
/// 
/// </summary>
public class CFXHelper
{
    private readonly AmqpCFXEndpoint? _endpoint;

    public CFXHelper(AmqpCFXEndpoint endpoint)
    {
        _endpoint = endpoint;

    }

    public void Init(string cfxHandle,Uri uri,string address)
    {
        try
        {
            if (_endpoint == null)
            {
                throw new ArgumentNullException($"AmqpCFXEndpoint is null");
            }
            
            _endpoint.Open(cfxHandle, uri);

            _endpoint.AddPublishChannel(uri, address);

            _endpoint.OnRequestReceived -= Endpoint_OnRequestReceived;
            _endpoint.OnRequestReceived += Endpoint_OnRequestReceived;
        }
        catch (Exception e)
        {
            throw new InvalidOperationException($"CFX 接口初始化异常:{ e.Message}");
        }
    }

    /// <summary>
    /// 请求接收处理
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    private CFXEnvelope Endpoint_OnRequestReceived(CFXEnvelope request)
    {
        CFXMessage response = null;

        if (request.MessageBody is AreYouThereRequest)
        {
            AreYouThereRequestReceived.Handle(request.MessageBody as AreYouThereRequest,out response);
        }else if (request.MessageBody is GetEndpointInformationRequest)
        {
            GetEndpointInformationRequestReceived.Handle(request.MessageBody as GetEndpointInformationRequest,out response);
        }else if (request.MessageBody is WhoIsThereRequest)
        {
            WhoIsThereRequestReceived.Handle(request.MessageBody as WhoIsThereRequest,out response);
        }else if (request.MessageBody is GetActiveRecipeRequest)
        {
            GetActiveRecipeRequestReceived.Handle(request.MessageBody as GetActiveRecipeRequest,out response);
        }else if (request.MessageBody is GetActiveFaultsRequest)
        {
            GetActiveFaultsRequestReceived.Handle(request.MessageBody as GetActiveFaultsRequest,out response);
        }else if (request.MessageBody is HandleFaultRequest)
        {
            HandleFaultRequestReceived.Handle(request.MessageBody as HandleFaultRequest,out response);
        }

        CFXEnvelope env = new CFXEnvelope(response);
        return env;
    }

    /// <summary>
    /// 主动推送事件
    /// </summary>
    /// <param name="env"></param>
    public void PublishEvent(CFXEnvelope env)
    {
        try
        {
            if (_endpoint == null)
            {
                throw new ArgumentNullException($"AmqpCFXEndpoint is null");
            }
            
            _endpoint.Publish(env);
        }
        catch (Exception e)
        {
            throw new InvalidOperationException($"推送事件异常:{ e.Message}");
        }
    }
}