using libplctag;

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

/// <summary>
///     Maps libplctag status codes + PCCC STS/EXT_STS bytes to OPC UA StatusCodes. Mirrors the
///     AbCip mapper — PCCC errors roughly align with CIP general-status in shape but with a
///     different byte vocabulary (PCCC STS nibble-low + EXT_STS on code 0x0F).
/// </summary>
public static class AbLegacyStatusMapper
{
    public const uint Good = 0u;
    public const uint GoodMoreData = 0x00A60000u;
    public const uint BadInternalError = 0x80020000u;
    public const uint BadNodeIdUnknown = 0x80340000u;
    public const uint BadNotWritable = 0x803B0000u;
    public const uint BadOutOfRange = 0x803C0000u;
    public const uint BadNotSupported = 0x803D0000u;
    public const uint BadDeviceFailure = 0x808B0000u;
    public const uint BadCommunicationError = 0x80050000u;
    public const uint BadTimeout = 0x800A0000u;
    public const uint BadTypeMismatch = 0x80740000u;

    /// <summary>
    ///     Map a libplctag return/status code to an OPC UA StatusCode. The integer passed here
    ///     is <c>(int)Tag.GetStatus()</c> — the underlying value of the libplctag.NET
    ///     <see cref="Status"/> enum. Delegates to the strongly-typed overload so the mapping
    ///     stays correct regardless of how the wrapper renumbers native PLCTAG_ERR_* constants
    ///     in future releases.
    /// </summary>
    /// <param name="status">The integer status code from libplctag.</param>
    /// <returns>The corresponding OPC UA status code.</returns>
    public static uint MapLibplctagStatus(int status) => MapLibplctagStatus((Status)status);

    /// <summary>
    ///     Map a libplctag.NET <see cref="Status"/> enum value to an OPC UA StatusCode. This is
    ///     the canonical core; the <c>int</c> overload exists only for the
    ///     <see cref="IAbLegacyTagRuntime.GetStatus"/> seam which boxes the enum as an int.
    /// </summary>
    /// <param name="status">The libplctag Status enum value.</param>
    /// <returns>The corresponding OPC UA status code.</returns>
    public static uint MapLibplctagStatus(Status status) => status switch
    {
        Status.Ok => Good,
        Status.Pending => GoodMoreData,
        Status.ErrorTimeout => BadTimeout,
        Status.ErrorNotFound or Status.ErrorNoMatch or Status.ErrorBadDevice => BadNodeIdUnknown,
        Status.ErrorNotAllowed => BadNotWritable,
        Status.ErrorOutOfBounds or Status.ErrorTooLarge or Status.ErrorTooSmall => BadOutOfRange,
        Status.ErrorUnsupported or Status.ErrorNotImplemented => BadNotSupported,
        Status.ErrorBadConnection or Status.ErrorBadGateway or Status.ErrorBadReply
            or Status.ErrorWinsock or Status.ErrorOpen or Status.ErrorClose
            or Status.ErrorRead or Status.ErrorWrite or Status.ErrorRemoteErr
            or Status.ErrorPartial or Status.ErrorAbort => BadCommunicationError,
        _ => BadCommunicationError,
    };

    /// <summary>
    ///     Map a PCCC STS (status) byte. Common codes per AB PCCC reference:
    ///     0x00 = success, 0x10 = illegal command, 0x20 = bad address, 0x30 = protected,
    ///     0x40 = programmer busy, 0x50 = file locked, 0xF0 = extended status follows.
    ///     libplctag surfaces only its own <see cref="Status"/> enum rather than exposing
    ///     the raw STS byte, so this method is not wired into the current read/write path.
    ///     It is retained as the reference mapping for future PCCC-STS inspection.
    /// </summary>
    /// <param name="sts">The PCCC STS byte.</param>
    /// <returns>The corresponding OPC UA status code.</returns>
    public static uint MapPcccStatus(byte sts) => sts switch
    {
        0x00 => Good,
        0x10 => BadNotSupported,
        0x20 => BadNodeIdUnknown,
        0x30 => BadNotWritable,
        0x40 => BadDeviceFailure,
        0x50 => BadDeviceFailure,
        0xF0 => BadInternalError, // extended status not inspected at this layer
        _ => BadCommunicationError,
    };
}
