using Shouldly;
using Xunit;

namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.IntegrationTests.DL205;

/// <summary>
///     Verifies the DL205/DL260 V-memory octal addressing quirk end-to-end: use
///     <see cref="DirectLogicAddress.UserVMemoryToPdu"/> to translate <c>V2000</c> octal into
///     the Modbus PDU address actually dispatched, then read the marker the dl205.json profile
///     placed at that address. HR[0x0400] = 0x2000 proves the translation was performed
///     correctly — a naïve caller treating "V2000" as decimal 2000 would read HR[2000] (which
///     the profile leaves at 0) and miss the marker entirely.
/// </summary>
[Collection(ModbusSimulatorCollection.Name)]
[Trait("Category", "Integration")]
[Trait("Device", "DL205")]
public sealed class DL205VMemoryQuirkTests(ModbusSimulatorFixture sim)
{
    /// <summary>Verifies that DL205 V2000 user memory octal address resolves to PDU address 0x0400.</summary>
    [Fact]
    public async Task DL205_V2000_user_memory_resolves_to_PDU_0x0400_marker()
    {
        if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
        if (!string.Equals(Environment.GetEnvironmentVariable("MODBUS_SIM_PROFILE"), "dl205",
                StringComparison.OrdinalIgnoreCase))
        {
            Assert.Skip("MODBUS_SIM_PROFILE != dl205 — skipping (standard profile does not seed V-memory markers).");
        }

        var pdu = DirectLogicAddress.UserVMemoryToPdu("V2000");
        pdu.ShouldBe((ushort)0x0400);

        var options = new ModbusDriverOptions
        {
            Host = sim.Host,
            Port = sim.Port,
            UnitId = 1,
            Timeout = TimeSpan.FromSeconds(2),
            RawTags = ModbusRawTags.Entries([
                new ModbusTagDefinition("DL205_V2000",
                    ModbusRegion.HoldingRegisters, Address: pdu,
                    DataType: ModbusDataType.UInt16, Writable: false),
            ]),
            Probe = new ModbusProbeOptions { Enabled = false },
        };
        await using var driver = new ModbusDriver(options, driverInstanceId: "dl205-vmem");
        await driver.InitializeAsync("{}", TestContext.Current.CancellationToken);

        var results = await driver.ReadAsync(["DL205_V2000"], TestContext.Current.CancellationToken);
        results[0].StatusCode.ShouldBe(0u);
        results[0].Value.ShouldBe((ushort)0x2000, "dl205.json seeds HR[0x0400] with marker 0x2000 (= V2000 value)");
    }

    /// <summary>Verifies that DL205 V40400 system memory address resolves to PDU address 0x2100.</summary>
    [Fact]
    public async Task DL205_V40400_system_memory_resolves_to_PDU_0x2100_marker()
    {
        if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);
        if (!string.Equals(Environment.GetEnvironmentVariable("MODBUS_SIM_PROFILE"), "dl205",
                StringComparison.OrdinalIgnoreCase))
        {
            Assert.Skip("MODBUS_SIM_PROFILE != dl205 — skipping.");
        }

        // V40400 is system memory on DL260 / H2-ECOM100 absolute mode; it does NOT follow the
        // simple octal-to-decimal formula (40400 octal = 16640 decimal, which would read HR[0x4100]).
        // The CPU places the system bank at PDU 0x2100 instead. Proving the helper routes there.
        var pdu = DirectLogicAddress.SystemVMemoryToPdu(0);
        pdu.ShouldBe((ushort)0x2100);

        var options = new ModbusDriverOptions
        {
            Host = sim.Host,
            Port = sim.Port,
            UnitId = 1,
            Timeout = TimeSpan.FromSeconds(2),
            RawTags = ModbusRawTags.Entries([
                new ModbusTagDefinition("DL205_V40400",
                    ModbusRegion.HoldingRegisters, Address: pdu,
                    DataType: ModbusDataType.UInt16, Writable: false),
            ]),
            Probe = new ModbusProbeOptions { Enabled = false },
        };
        await using var driver = new ModbusDriver(options, driverInstanceId: "dl205-sysv");
        await driver.InitializeAsync("{}", TestContext.Current.CancellationToken);

        var results = await driver.ReadAsync(["DL205_V40400"], TestContext.Current.CancellationToken);
        results[0].StatusCode.ShouldBe(0u);
        results[0].Value.ShouldBe((ushort)0x4040, "dl205.json seeds HR[0x2100] with marker 0x4040 (= V40400 value)");
    }
}
