using Elwig.Helpers.Weighing;

namespace Tests.WeighingTests {
    [TestFixture]
    class ScaleTestMatzen {

        private MockScale? Mock;
        private SysTecITScale? Scale;

        private static (string, bool) ScaleHandler(string req, int weight, string? error, int identNr) {
            var modes = error?.Split(';') ?? [];
            var overloaded = modes.Contains("overloaded");
            var moving = modes.Contains("moving");
            var invalid = modes.Contains("invalid");
            var crc = modes.Contains("crc");
            var unit = modes.Contains("unit");

            if (invalid) {
                return ("abcd\r\n", false);
            } else if (!req.StartsWith('<') || !req.EndsWith('>')) {
                return ("<31>\r\n", false);
            }
            req = req[1..^1];

            bool incr;
            if (req.Length > 3) {
                return ("<32>\r\n", false);
            } else if (req.StartsWith("RN")) {
                incr = true;
            } else if (req.StartsWith("RM")) {
                incr = false;
            } else {
                return ("<32>\r\n", false);
            }

            if (overloaded) {
                return ("<12>\r\n", false);
            } else if (weight == 0) {
                incr = false;
            }

            if (moving && incr)
                return ("<13>\r\n", false);

            string data = $"00{(moving ? 1 : 0)}0{new DateTime(2020, 10, 15, 12, 34, 0):dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1" +
                $"{weight,8}{0,8}{weight,8}{(unit ? "lb" : "kg")}   {1,3}";
            ushort checksum = Elwig.Helpers.Utils.CalcCrc16Modbus(data);
            if (crc) checksum += 10;
            return ($"<{data}{checksum,8}>\r\n", incr);
        }

        [OneTimeSetUp]
        public void SetupScale() {
            Mock = new CommandMockScale(12345, ScaleHandler);
            Scale = new("1", "IT3000A", "tcp://127.0.0.1:12345");
        }

        [OneTimeTearDown]
        public void TeardownScale() {
            Mock?.Dispose();
            Scale?.Dispose();
        }

        [SetUp]
        public void ResetScale() {
            Mock!.IdentNr = 0;
            Mock!.Weight = 0;
            Mock!.Error = null;
        }

        [Test]
        public async Task Test_01_CurrentWeight() {
            Mock!.Weight = 1234;
            Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
                Weight = 1234, Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
            }));
            Mock!.Weight = 1235;
            Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
                Weight = 1235, Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
            }));
            Mock!.Weight = 1236;
            Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
                Weight = 1236, Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
            }));
        }

        [Test]
        public async Task Test_02_Normal() {
            Mock!.Weight = 1234;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                Weight = 1234, WeighingId = "1",
                FullWeighingId = $"2020-10-15/1",
                Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
            }));
            Mock!.Weight = 3333;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                Weight = 3333, WeighingId = "2",
                FullWeighingId = $"2020-10-15/2",
                Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
            }));
            Mock!.Weight = 4321;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                Weight = 4321, WeighingId = "3",
                FullWeighingId = $"2020-10-15/3",
                Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
            }));
        }

        [Test]
        public void Test_03_Moving() {
            Mock!.Weight = 1_000;
            Mock!.Error = "moving";
            IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
            Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
        }

        [Test]
        public void Test_04_Overloaded() {
            Mock!.Weight = 10_000;
            Mock!.Error = "overloaded";
            IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
            Assert.That(ex.Message, Contains.Substring("Waage in Überlast"));
        }

        [Test]
        public void Test_05_InvalidResponse() {
            Mock!.Weight = 1_000;
            Mock!.Error = "invalid";
            Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
        }

        [Test]
        public void Test_06_InvalidCrc() {
            Mock!.Weight = 1_000;
            Mock!.Error = "crc";
            IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
            Assert.That(ex.Message, Contains.Substring("Invalid CRC16 checksum"));
        }

        [Test]
        public void Test_07_InvalidUnit() {
            Mock!.Weight = 1_000;
            Mock!.Error = "unit";
            IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
        }
    }
}