using DragonISO.Engine.Interop;

namespace DragonISO.Engine.NdiInterop;

internal sealed class NdiPInvokeFindHandle : NdiFindHandle
{
    public IntPtr Native { get; private set; }

    public NdiPInvokeFindHandle(IntPtr native) => Native = native;

    public override void Dispose()
    {
        if (Native != IntPtr.Zero)
        {
            NdiNative.FindDestroy(Native);
            Native = IntPtr.Zero;
        }
    }
}

internal sealed class NdiPInvokeReceiverHandle : NdiReceiverHandle
{
    public IntPtr Native { get; private set; }
    public string SourceName { get; }

    public NdiPInvokeReceiverHandle(IntPtr native, string sourceName)
    {
        Native = native;
        SourceName = sourceName;
    }

    public override void Dispose()
    {
        if (Native != IntPtr.Zero)
        {
            NdiNative.RecvDestroy(Native);
            Native = IntPtr.Zero;
        }
    }
}

internal sealed class NdiPInvokeSenderHandle : NdiSenderHandle
{
    public IntPtr Native { get; private set; }
    public string OutputName { get; }

    // The NDI SDK carries frame rate per-frame (VideoFrameV2.frame_rate_N/D),
    // not on send_create, so the handle remembers the configured rational and
    // SendFrame stamps it onto every outgoing frame.
    public int FrameRateN { get; }
    public int FrameRateD { get; }

    public NdiPInvokeSenderHandle(IntPtr native, string outputName, int frameRateN = 60000, int frameRateD = 1001)
    {
        Native = native;
        OutputName = outputName;
        FrameRateN = frameRateN;
        FrameRateD = frameRateD;
    }

    public override void Dispose()
    {
        if (Native != IntPtr.Zero)
        {
            NdiNative.SendDestroy(Native);
            Native = IntPtr.Zero;
        }
    }
}
