using System;
using System.Threading;
using System.Threading.Tasks;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Shared;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Shared.Contracts;

namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Host.Ipc;

/// <summary>
///     Dispatches a single IPC frame to the backend. Implementations own the FOCAS session
///     state and translate request DTOs into Fwlib32 calls.
/// </summary>
public interface IFrameHandler
{
    Task HandleAsync(FocasMessageKind kind, byte[] body, FrameWriter writer, CancellationToken ct);

    /// <summary>
    ///     Called once per accepted connection after the Hello handshake. Lets the handler
    ///     attach server-pushed event sinks (data-change notifications, runtime-status
    ///     changes) to the connection's <paramref name="writer"/>. Returns an
    ///     <see cref="IDisposable"/> the pipe server disposes when the connection closes —
    ///     backends use it to unsubscribe from their push sources.
    /// </summary>
    IDisposable AttachConnection(FrameWriter writer);

    public sealed class NoopAttachment : IDisposable
    {
        public static readonly NoopAttachment Instance = new();
        public void Dispose() { }
    }
}
