namespace Tests.UnitTests.WeighingTests {
    public static class ScaleHandlers {

        public static (string, bool) Handle_IT3000A(string req, int weight, int tare, 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");

            Thread.Sleep(100);

            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 + tare,8}{tare,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);
        }

        public static (string, bool) Handle_IT6000E(string req, int weight, int tare, string? error, int identNr, string terminalNr) {
            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);

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

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

        public static (string, bool) Handle_L246(string req, int weight, int tare, 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");

            Thread.Sleep(100);

            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);

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

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

        public static (string, bool) HandleDMA02(string req, int weight, int tare, string? error, int identNr) {
            var modes = error?.Split(';') ?? [];
            var overloaded = modes.Contains("overloaded");
            var moving = modes.Contains("moving");
            var invalid = modes.Contains("invalid");

            Thread.Sleep(100);

            if (invalid) {
                return ("abcd\x03", false);
            } else if (req != "\x05" && req != "?") {
                return ("\x0002ES\x03", false);
            }

            bool incr;
            if (req == "?") {
                incr = false;
            } else {
                incr = true;
            }

            if (overloaded) {
                return ("\x0002ES\x03", false);
            } else if (weight == 0) {
                incr = false;
            }

            if (moving && incr)
                return ("\x0002EM\x03", false);

            string data = $" {(moving ? "M" : "S")}{weight + tare,7}{tare,7}{weight,7} 1{(incr ? identNr : 0),6}{new DateTime(2020, 10, 15, 12, 34, 0):yyyyMMddHHmmss}";
            return ($"\x0002{data}\x03", incr);
        }

        public static (string, bool) HandleDMA03Baby(string req, int weight, int tare, string? error, int identNr) {
            return HandleDMA02(req, weight, tare, error, identNr);
        }

        public static (string, bool) Handle_L320(int weight, int tare, string? error, int identNr) {
            var modes = error?.Split(';') ?? [];
            var invalid = modes.Contains("invalid");
            var unit = modes.Contains("unit");

            Thread.Sleep(100);

            if (invalid) {
                return ("abcd", false);
            }

            bool incr = true;
            return ($" {new DateTime(2020, 9, 28, 9, 8, 0):dd.MM.yy HH:mm} {identNr,4} {weight,9}{(unit ? "lb" : "kg")} ", incr);
        }
    }
}
