using CliFx.Attributes;
using CliFx.Infrastructure;
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;

namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Commands;

/// <summary>
///     Read one S7 address (DB / M / I / Q area). Addresses use S7.Net grammar — the driver
///     parses them via <c>S7AddressParser</c> so whatever the server accepts the CLI accepts
///     too.
/// </summary>
[Command("read", Description = "Read a single S7 address.")]
public sealed class ReadCommand : S7CommandBase
{
    [CommandOption("address", 'a', Description =
        "S7 address. Examples: DB1.DBW0 (DB1, word 0); M0.0 (merker bit); IW4 (input word 4); " +
        "QD8 (output dword 8); DB2.DBD20 (DB2, dword 20); DB5.DBX4.3 (DB5, byte 4, bit 3); " +
        "DB10.STRING[0] (DB10 string). Bit addresses use dot notation.",
        IsRequired = true)]
    public string Address { get; init; } = default!;

    [CommandOption("type", 't', Description =
        "Bool / Byte / Int16 / UInt16 / Int32 / UInt32 / Int64 / UInt64 / Float32 / Float64 / " +
        "String / DateTime (default Int16).")]
    public S7DataType DataType { get; init; } = S7DataType.Int16;

    [CommandOption("string-length", Description =
        "For type=String: S7-string max length (default 254, S7 max).")]
    public int StringLength { get; init; } = 254;

    public override async ValueTask ExecuteAsync(IConsole console)
    {
        ConfigureLogging();
        var ct = console.RegisterCancellationHandler();

        var tagName = SynthesiseTagName(Address, DataType);
        var tag = new S7TagDefinition(
            Name: tagName,
            Address: Address,
            DataType: DataType,
            Writable: false,
            StringLength: StringLength);
        var options = BuildOptions([tag]);

        await using var driver = new S7Driver(options, DriverInstanceId);
        try
        {
            await driver.InitializeAsync("{}", ct);
            var snapshot = await driver.ReadAsync([tagName], ct);
            await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0]));
        }
        finally
        {
            await driver.ShutdownAsync(CancellationToken.None);
        }
    }

    /// <summary>Tag-name key used internally. Address + type is already unique.</summary>
    internal static string SynthesiseTagName(string address, S7DataType type)
        => $"{address}:{type}";
}
