using Elwig.Helpers.Weighing;

namespace Tests.WeighingTests {
    [TestFixture]
    class ScaleTestGrInzersdorfL246 {

        private MockScale Mock;
        private SysTecITScale Scale;

        [OneTimeSetUp]
        public void SetupScale() {
            Mock = new CommandMockScale(12345, ScaleHandlers.Handle_L246);
            Scale = new("1", "L246", "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 = 1235;
            Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
                GrossWeight = 1235, TareWeight = 0, NetWeight = 1235,
                Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
            }));
            Mock.Weight = 1240;
            Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
                GrossWeight = 1240, TareWeight = 0, NetWeight = 1240,
                Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
            }));
            Mock.Weight = 1245;
            Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
                GrossWeight = 1245, TareWeight = 0, NetWeight = 1245,
                Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
            }));
        }

        [Test]
        public async Task Test_02_Normal() {
            Mock.Weight = 1235;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                GrossWeight = 1235, TareWeight = 0, NetWeight = 1235,
                WeighingId = "1", FullWeighingId = $"2020-10-17/1",
                Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
            }));
            Mock.Weight = 3335;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                GrossWeight = 3335, TareWeight = 0, NetWeight = 3335,
                WeighingId = "2", FullWeighingId = $"2020-10-17/2",
                Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
            }));
            Mock.Weight = 6420;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                GrossWeight = 6420, TareWeight = 0, NetWeight = 6420,
                WeighingId = "3", FullWeighingId = $"2020-10-17/3",
                Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
            }));
        }

        [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());
        }
    }
}