using Elwig.Helpers.Weighing;

namespace Tests.WeighingTests {
    [TestFixture]
    public class ScaleTestHaugsdorfDMA02 {

        private MockScale Mock;
        private GassnerScale Scale;

        [OneTimeSetUp]
        public void SetupScale() {
            Mock = new CommandMockScale(12345, ScaleHandlers.HandleDMA02);
            Scale = new("1", "DMA02", "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.Tare = 0;
            Mock.Error = null;
        }

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

        [Test]
        public async Task Test_02_Normal() {
            Mock.Weight = 1234;
            Mock.Tare = 92;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                GrossWeight = 1326,
                TareWeight = 92,
                NetWeight = 1234,
                WeighingId = "1",
                FullWeighingId = "1",
                Date = new DateOnly(2020, 10, 15),
                Time = new TimeOnly(12, 34),
            }));
            Mock.Weight = 3333;
            Mock.Tare = 41;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                GrossWeight = 3374,
                TareWeight = 41,
                NetWeight = 3333,
                WeighingId = "2",
                FullWeighingId = "2",
                Date = new DateOnly(2020, 10, 15),
                Time = new TimeOnly(12, 34),
            }));
            Mock.Weight = 4321;
            Mock.Tare = 0;
            Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
                GrossWeight = 4321,
                TareWeight = 0,
                NetWeight = 4321,
                WeighingId = "3",
                FullWeighingId = "3",
                Date = new DateOnly(2020, 10, 15),
                Time = new TimeOnly(12, 34),
            }));
        }

        [Test]
        public void Test_03_Moving() {
            Mock.Weight = 1_000;
            Mock.Tare = 41;
            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_InvalidResponse() {
            Mock.Weight = 1_000;
            Mock.Tare = 41;
            Mock.Error = "invalid";
            Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
        }
    }
}