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
{
    [CommandOption("address", 'a', Description = "FOCAS address — same format as `read`.", IsRequired = true)]
    public string Address { get; init; } = default!;

    [CommandOption("type", 't', Description =
        "Bit / Byte / Int16 / Int32 / Float32 / Float64 / String (default Int16).")]
    public FocasDataType DataType { get; init; } = FocasDataType.Int16;

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

    public override async ValueTask ExecuteAsync(IConsole console)
    {
        ConfigureLogging();
        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 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
        {
            await driver.ShutdownAsync(CancellationToken.None);
        }
    }

    internal static object ParseValue(string raw, FocasDataType type) => type switch
    {
        FocasDataType.Bit => ParseBool(raw),
        FocasDataType.Byte => sbyte.Parse(raw, CultureInfo.InvariantCulture),
        FocasDataType.Int16 => short.Parse(raw, CultureInfo.InvariantCulture),
        FocasDataType.Int32 => int.Parse(raw, CultureInfo.InvariantCulture),
        FocasDataType.Float32 => float.Parse(raw, CultureInfo.InvariantCulture),
        FocasDataType.Float64 => double.Parse(raw, CultureInfo.InvariantCulture),
        FocasDataType.String => raw,
        _ => throw new CliFx.Exceptions.CommandException($"Unsupported DataType '{type}' for write."),
    };

    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."),
    };
}
