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.S7.Cli.Commands;

/// <summary>
///     Write one value to an S7 address. Mirrors <see cref="ReadCommand"/>'s flag shape.
///     Writes to M (merker) bits or Q (output) coils that drive edge-triggered routines
///     are real — be careful what you hit on a running PLC.
/// </summary>
[Command("write", Description = "Write a single S7 address.")]
public sealed class WriteCommand : S7CommandBase
{
    [CommandOption("address", 'a', Description =
        "S7 address — same format as `read`.", 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("value", 'v', Description =
        "Value to write. Parsed per --type (booleans accept true/false/1/0).",
        IsRequired = true)]
    public string Value { get; init; } = default!;

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

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

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

        var parsed = ParseValue(Value, DataType);

        await using var driver = new S7Driver(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);
        }
    }

    /// <summary>Parse <c>--value</c> per <see cref="S7DataType"/>, invariant culture throughout.</summary>
    internal static object ParseValue(string raw, S7DataType type) => type switch
    {
        S7DataType.Bool => ParseBool(raw),
        S7DataType.Byte => byte.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.Int16 => short.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.UInt16 => ushort.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.Int32 => int.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.UInt32 => uint.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.Int64 => long.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.UInt64 => ulong.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.Float32 => float.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.Float64 => double.Parse(raw, CultureInfo.InvariantCulture),
        S7DataType.String => raw,
        S7DataType.DateTime => DateTime.Parse(raw, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind),
        _ => 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."),
    };
}
