namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip;

/// <summary>
///     Thin wire-layer abstraction over a single CIP tag. The driver holds one instance per
///     <c>(device, tag path)</c> pair; the default implementation delegates to
///     <see cref="LibplctagTagRuntime"/>. Tests swap in a fake via
///     <see cref="IAbCipTagFactory"/> so the driver's read / write / status-mapping logic can
///     be exercised without a running PLC or the native libplctag binary.
/// </summary>
public interface IAbCipTagRuntime : IDisposable
{
    /// <summary>Create the underlying native tag (equivalent to libplctag's <c>plc_tag_create</c>).</summary>
    Task InitializeAsync(CancellationToken cancellationToken);

    /// <summary>Issue a read; on completion the local buffer holds the current PLC value.</summary>
    Task ReadAsync(CancellationToken cancellationToken);

    /// <summary>Flush the local buffer to the PLC.</summary>
    Task WriteAsync(CancellationToken cancellationToken);

    /// <summary>
    ///     Raw libplctag status code — mapped to an OPC UA StatusCode via
    ///     <see cref="AbCipStatusMapper.MapLibplctagStatus"/>. Zero on success, negative on error.
    /// </summary>
    int GetStatus();

    /// <summary>
    ///     Decode the local buffer into a boxed .NET value per the tag's configured type.
    ///     <paramref name="bitIndex"/> is non-null only for BOOL-within-DINT tags captured in
    ///     the <c>.N</c> syntax at parse time.
    /// </summary>
    object? DecodeValue(AbCipDataType type, int? bitIndex);

    /// <summary>
    ///     Decode a value at an arbitrary byte offset in the local buffer. Task #194 —
    ///     whole-UDT reads perform one <see cref="ReadAsync"/> on the parent UDT tag then
    ///     call this per declared member with its computed offset, avoiding one libplctag
    ///     round-trip per member. Implementations that do not support offset-aware decoding
    ///     may fall back to <see cref="DecodeValue"/> when <paramref name="offset"/> is zero;
    ///     offsets greater than zero against an unsupporting runtime should return <c>null</c>
    ///     so the planner can skip grouping.
    /// </summary>
    object? DecodeValueAt(AbCipDataType type, int offset, int? bitIndex);

    /// <summary>
    ///     Encode <paramref name="value"/> into the local buffer per the tag's type. Callers
    ///     pair this with <see cref="WriteAsync"/>.
    /// </summary>
    void EncodeValue(AbCipDataType type, int? bitIndex, object? value);
}

/// <summary>
///     Factory for per-tag runtime handles. Instantiated once per driver, consumed per
///     <c>(device, tag path)</c> pair at the first read/write.
/// </summary>
public interface IAbCipTagFactory
{
    IAbCipTagRuntime Create(AbCipTagCreateParams createParams);
}

/// <summary>Everything libplctag needs to materialise a tag handle.</summary>
/// <param name="Gateway">Gateway IP / hostname parsed from <see cref="AbCipHostAddress.Gateway"/>.</param>
/// <param name="Port">EtherNet/IP TCP port — default 44818.</param>
/// <param name="CipPath">CIP route path, e.g. <c>1,0</c>. Empty for Micro800.</param>
/// <param name="LibplctagPlcAttribute">libplctag <c>plc=...</c> attribute, per family profile.</param>
/// <param name="TagName">Logix symbolic tag name as emitted by <see cref="AbCipTagPath.ToLibplctagName"/>.</param>
/// <param name="Timeout">libplctag operation timeout (applies to Initialize / Read / Write).</param>
public sealed record AbCipTagCreateParams(
    string Gateway,
    int Port,
    string CipPath,
    string LibplctagPlcAttribute,
    string TagName,
    TimeSpan Timeout);
