using System.Globalization;
using CliFx.Attributes;
using CliFx.Infrastructure;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;

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

/// <summary>
///     Write one value to a FOCAS address. PMC G/R writes are real — be careful
///     which file you hit on a running machine. Parameter writes may require the
///     CNC to be in MDI mode + the parameter-write switch enabled.
/// </summary>
[Command("write", Description = "Write a single FOCAS address.")]
public sealed class WriteCommand : FocasCommandBase
{
    /// <summary>Gets the FOCAS address to write to.</summary>
    [CommandOption("address", 'a', Description = "FOCAS address — same format as `read`.", IsRequired = true)]
    public string Address { get; init; } = default!;

    /// <summary>Gets the data type of the value to write.</summary>
    [CommandOption("type", 't', Description =
        "Bit / Byte / Int16 / Int32 / Float32 / Float64 / String (default Int16).")]
    public FocasDataType DataType { get; init; } = FocasDataType.Int16;

    /// <summary>Gets the value to write.</summary>
    [CommandOption("value", 'v', Description =
        "Value to write. Parsed per --type (booleans accept true/false/1/0).",
        IsRequired = true)]
    public string Value { get; init; } = default!;

    /// <inheritdoc />
    public override async ValueTask ExecuteAsync(IConsole console)
    {
        ConfigureLogging();
        // Validate numeric option ranges before any driver work so a zero/negative
        // port/timeout surfaces as a clean CommandException rather than an opaque
        // downstream exception.
        ValidateOptions();
        var ct = console.RegisterCancellationHandler();

        var tagName = ReadCommand.SynthesiseTagName(Address, DataType);
        var tag = new FocasTagDefinition(
            Name: tagName,
            DeviceHostAddress: HostAddress,
            Address: Address,
            DataType: DataType,
            Writable: true);
        var options = BuildOptions([tag]);

        var parsed = ParseValue(Value, DataType);

        // `await using` is the sole disposal mechanism — FocasDriver.DisposeAsync already
        // invokes ShutdownAsync, so a redundant explicit ShutdownAsync(CancellationToken.None)
        // in a finally block ran shutdown twice. The await-using on the next line is enough.
        await using var driver = new FocasDriver(options, DriverInstanceId);
        try
        {
            await driver.InitializeAsync("{}", ct);
            var results = await driver.WriteAsync([new WriteRequest(tagName, parsed)], ct);
            await console.Output.WriteLineAsync(SnapshotFormatter.FormatWrite(Address, results[0]));
        }
        finally
        {
            // Flush Serilog before process exit so buffered log output emitted during driver
            // shutdown is not silently dropped (matching DriverCommandBase docs and every
            // sibling CLI — Modbus / AbCip / AbLegacy / TwinCAT).
            FlushLogging();
        }
    }

    /// <summary>Parse <c>--value</c> per <see cref="FocasDataType"/>, invariant culture throughout.</summary>
    /// <remarks>
    ///     Numeric parses are wrapped so that malformed input
    ///     (<see cref="FormatException"/> / <see cref="OverflowException"/>) surfaces
    ///     as a clean <see cref="CliFx.Exceptions.CommandException"/> rather than a raw
    ///     .NET stack trace — matching the friendly message the Bit path already produces.
    /// </remarks>
    /// <param name="raw">The raw string value to parse.</param>
    /// <param name="type">The data type to parse the value as.</param>
    /// <returns>The parsed value as an object.</returns>
    internal static object ParseValue(string raw, FocasDataType type)
    {
        if (type == FocasDataType.Bit) return ParseBool(raw);
        if (type == FocasDataType.String) return raw;
        try
        {
            return type switch
            {
                FocasDataType.Byte    => (object)sbyte.Parse(raw, CultureInfo.InvariantCulture),
                FocasDataType.Int16   => (object)short.Parse(raw, CultureInfo.InvariantCulture),
                FocasDataType.Int32   => (object)int.Parse(raw, CultureInfo.InvariantCulture),
                FocasDataType.Float32 => (object)float.Parse(raw, CultureInfo.InvariantCulture),
                FocasDataType.Float64 => (object)double.Parse(raw, CultureInfo.InvariantCulture),
                _ => throw new CliFx.Exceptions.CommandException($"Unsupported DataType '{type}' for write."),
            };
        }
        catch (FormatException ex)
        {
            throw new CliFx.Exceptions.CommandException(
                $"Value '{raw}' is not a valid {type}: {ex.Message}");
        }
        catch (OverflowException ex)
        {
            throw new CliFx.Exceptions.CommandException(
                $"Value '{raw}' is out of range for {type}: {ex.Message}");
        }
    }

    private static bool ParseBool(string raw) => raw.Trim().ToLowerInvariant() switch
    {
        "1" or "true" or "on" or "yes" => true,
        "0" or "false" or "off" or "no" => false,
        _ => throw new CliFx.Exceptions.CommandException(
            $"Boolean value '{raw}' is not recognised. Use true/false, 1/0, on/off, or yes/no."),
    };
}
