1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
#![allow(clippy::too_many_arguments)]

use crate::error::MessageParseError;
use crate::protocol::Message;
use std::fmt::{Debug, Display, Formatter};

/// Represents a trains address of 14 byte length.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct AddressArg(u16);

impl AddressArg {
    /// Creates a new address.
    ///
    /// Please consider keeping in range between 0 and 16383.
    /// Higher values may not be supported by this address implementation.
    pub fn new(adr: u16) -> Self {
        Self(adr)
    }

    /// Parses the message bytes from a model railroads message into an `AddressArg`
    ///
    /// # Parameters
    ///
    /// - `adr`: seven least significant loco address bits
    /// - `adr2`: seven most significant loco address bits
    pub(crate) fn parse(adr2: u8, adr: u8) -> Self {
        let mut address = adr as u16;
        address |= (adr2 as u16) << 7;
        Self(address)
    }

    /// # Returns
    ///
    /// The address hold by this arg
    pub fn address(&self) -> u16 {
        self.0
    }

    /// Sets the address hold by this [`AddressArg`]
    ///
    /// Please consider keeping in range between 0 and 16383.
    /// Higher values may not be supported by this address implementation.
    pub fn set_address(&mut self, address: u16) {
        self.0 = address;
    }

    /// # Returns
    ///
    /// seven least significant loco address bits
    pub(crate) fn adr1(&self) -> u8 {
        (self.0 & 0x007F) as u8
    }

    /// # Returns
    ///
    /// seven most significant loco address bits
    pub(crate) fn adr2(&self) -> u8 {
        ((self.0 >> 7) & 0x007F) as u8
    }
}

/// Which direction state a switch is orientated to
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum SwitchDirection {
    Straight,
    Curved,
}

impl std::ops::Not for SwitchDirection {
    type Output = SwitchDirection;

    fn not(self) -> Self::Output {
        match self {
            SwitchDirection::Straight => SwitchDirection::Curved,
            SwitchDirection::Curved => SwitchDirection::Straight,
        }
    }
}

/// Holds switch state information to be read or write
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct SwitchArg {
    /// The address of the switch (0 - 2047)
    address: u16,
    /// The switches direction state, which direction the switch points
    direction: SwitchDirection,
    /// If the switch is not in the requested direction.
    /// Use true if you want the switch to go to the direction.
    state: bool,
}

impl SwitchArg {
    /// Creates a new switch information block that can be send to update a switch in a
    /// model railroad system using the corresponding [`crate::protocol::Message::SwReq`] message.
    ///
    /// # Parameters
    ///
    /// - `address`: The address of the switch you want to change state (0 to 2047)
    /// - `direction`: The direction the switch should switch to
    /// - `state`: The activation state of the switch (If the switch is in the requested state)
    pub fn new(address: u16, direction: SwitchDirection, state: bool) -> Self {
        Self {
            address,
            direction,
            state,
        }
    }

    /// Parses the arguments of an incoming model railroads message to a [`SwitchArg`].
    ///
    /// # Parameters
    ///
    /// - `sw1`: Seven least significant switch address bits
    /// - `sw2`: four most significant switch address bits,
    ///          1 bit for direction and
    ///          1 bit for activation state
    pub(crate) fn parse(sw1: u8, sw2: u8) -> Self {
        let mut address = sw1 as u16;
        address |= (sw2 as u16 & 0x0F) << 7;

        let direction = if sw2 & 0x20 == 0 {
            SwitchDirection::Curved
        } else {
            SwitchDirection::Straight
        };

        let state = (sw2 & 0x10) != 0;
        Self {
            address,
            direction,
            state,
        }
    }

    /// # Returns
    ///
    /// The address of the switch
    pub fn address(&self) -> u16 {
        self.address
    }
    /// # Returns
    ///
    /// The switches direction state
    pub fn direction(&self) -> SwitchDirection {
        self.direction
    }
    /// # Returns
    ///
    /// The switches activation status. False if the switch has switched to the requested state.
    pub fn state(&self) -> bool {
        self.state
    }

    /// Sets the address of the switch to use.
    ///
    /// # Parameters
    ///
    /// - `address`: The switches address (0 - 2047)
    pub fn set_address(&mut self, address: u16) {
        self.address = address;
    }
    /// Sets the direction to switch to.
    ///
    /// # Parameters
    ///
    /// - `direction`: The switches direction
    pub fn set_direction(&mut self, direction: SwitchDirection) {
        self.direction = direction;
    }
    /// Sets the activation state of the switch.
    ///
    /// # Parameters
    ///
    /// - `state`: The switches activation state to set (`true = ON, false = OFF`)
    pub fn set_state(&mut self, state: bool) {
        self.state = state;
    }

    /// # Returns
    ///
    /// The seven least significant address bits.
    pub(crate) fn sw1(&self) -> u8 {
        (self.address & 0x007F) as u8
    }

    /// # Returns
    ///
    /// The four most significant address bits combined with a direction state and activation state.
    pub(crate) fn sw2(&self) -> u8 {
        let mut sw2 = ((self.address >> 7) & 0x000F) as u8;

        sw2 |= match self.direction {
            SwitchDirection::Curved => 0x00,
            SwitchDirection::Straight => 0x20,
        };

        if self.state {
            sw2 |= 0x10;
        }

        sw2
    }
}

/// Represents one slots address between 0 to 127.
///
/// Note that some slots are special handled slots and therefore can not be used (read/write) as normal slots.
///
/// # Slots
///
/// | Nr.     | Function                           |
/// |---------|------------------------------------|
/// | 0       | dispatch                           |
/// | 1-119   | active locs (normal slots)         |
/// | 120-127 | reserved (system / master control) |
/// | - 123   | fast clock                         |
/// | - 124   | programming track                  |
/// | - 127   | command station options            |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct SlotArg(u8);

impl SlotArg {
    /// Creates a new slots address in range of 0 to 127.
    ///
    /// Please consider that the special slots (0, 120 - 127) may not work
    /// as you expect other slots to do.
    ///
    /// # Parameter
    ///
    /// - `slot`: The slots address to set
    pub fn new(slot: u8) -> Self {
        Self(slot & 0x7F)
    }

    /// Parses an incoming slot message from a model railroads message.
    ///
    /// # Parameter
    ///
    /// - `slot`: The slots address to set
    pub(crate) fn parse(slot: u8) -> Self {
        Self(slot & 0x7F)
    }

    /// # Returns
    ///
    /// The slot hold by the struct
    pub fn slot(&self) -> u8 {
        self.0
    }
}

/// Represents the speed set to a [`SlotArg`].
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum SpeedArg {
    /// Performs a normal stop. Trains may stop smoothly when they receive a message force them to stop.
    Stop,
    /// Performs an immediate stop action. Trains do stop immediately.
    EmergencyStop,
    /// Sets the slots speed to a given value. If you want a slot speed to set to 0
    /// use [`SpeedArg::Stop`] or create your [`SpeedArg`] using [`SpeedArg::new()`].
    ///
    /// The maximum speed is 126. Higher values may create unexpected behaviour.
    Drive(u8),
}

impl SpeedArg {
    /// Creates a new [`SpeedArg`] from the given value.
    /// This means returning [`SpeedArg::Stop`] if the given `spd` is set to 0 and
    /// returning [`SpeedArg::Drive`] with the given `spd` set as speed otherwise.
    ///
    /// # Parameters
    ///
    /// - `spd`: The speed to create the `SpeedArg` for.
    ///          The maximum speed is 126. Higher values may create unexpected behaviour.
    pub fn new(spd: u8) -> Self {
        match spd {
            0x00 => Self::Stop,
            _ => Self::Drive(spd),
        }
    }

    /// Parses the speed from a model railroads send speed.
    ///
    /// # Parameters
    ///
    /// - `spd`: The model railroad messages speed
    pub(crate) fn parse(spd: u8) -> Self {
        match spd {
            0x00 => Self::Stop,
            0x01 => Self::EmergencyStop,
            _ => Self::Drive(spd - 1),
        }
    }

    /// # Returns
    ///
    /// The model railroad interpreted speed of this arg.
    pub(crate) fn spd(&self) -> u8 {
        match *self {
            SpeedArg::Stop => 0x00,
            SpeedArg::EmergencyStop => 0x01,
            SpeedArg::Drive(spd) => (spd + 1) & 0x7F,
        }
    }

    /// # Returns
    ///
    /// A `u8` interpreted value of the given [`SpeedArg`].
    ///
    /// Please note that [`SpeedArg::Stop`] and [`SpeedArg::EmergencyStop`] are both cast to 0
    /// as they both indicates that the slots speed is 0 and only differ in how
    /// immediate this state is reached by the connected device.
    pub fn get_spd(&self) -> u8 {
        match *self {
            SpeedArg::Stop => 0x00,
            SpeedArg::EmergencyStop => 0x00,
            SpeedArg::Drive(spd) => spd,
        }
    }
}

/// Represents the direction and first five function bits of a slot.
///
/// Function bit 0 may control a trains light
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct DirfArg(u8);

impl DirfArg {
    /// Creates a new dirf arg with all possible functions set
    ///
    /// # Parameter
    ///
    /// - `dir`: The direction to set (`true` = forwards, `false` = backwards)
    /// - `f0`: Function bit 0 (train light control)
    /// - `f1`: Function bit 1
    /// - `f2`: Function bit 2
    /// - `f3`: Function bit 3
    /// - `f4`: Function bit 4
    pub fn new(dir: bool, f0: bool, f1: bool, f2: bool, f3: bool, f4: bool) -> Self {
        let mut dirf = if dir { 0x20 } else { 0x00 };
        if f0 {
            dirf |= 0x10
        }
        if f1 {
            dirf |= 0x01
        }
        if f2 {
            dirf |= 0x02
        }
        if f3 {
            dirf |= 0x04
        }
        if f4 {
            dirf |= 0x08
        }
        Self(dirf)
    }

    /// Parses the direction from a model railroad message.
    pub(crate) fn parse(dirf: u8) -> Self {
        Self(dirf & 0x3F)
    }

    /// # Returns
    ///
    /// The direction represented by this [`DirfArg`].
    /// `true` means forward, `false` means backwards.
    pub fn dir(&self) -> bool {
        self.0 & 0x20 != 0
    }

    /// # Returns
    ///
    /// The value of the requested f-flag.
    /// As there are only for f-flags are hold by one [`DirfArg`] only values from
    /// 0 to 4 are calculated other inputs may ever return `false`.
    pub fn f(&self, f_num: u8) -> bool {
        if f_num <= 4 {
            self.0 >> (if f_num == 0 { 4 } else { f_num - 1 }) & 1 != 0
        } else {
            false
        }
    }

    /// Sets the direction hold by this arg to the requested value
    ///
    /// # Parameters
    ///
    /// - `value`: The direction to set (`true` = forward, `false` = backward)
    pub fn set_dir(&mut self, value: bool) {
        if value {
            self.0 |= 0x20;
        } else {
            self.0 &= !0x20
        }
    }

    /// Sets the value of the requested f-flag.
    ///
    /// # Parameters
    ///
    /// - `f_num`: The f-flag to set. (Only values in range of 0 to 4 may create an effect).
    ///            Other inputs will be ignored.
    /// - `value`: The value to set the requested flag to.
    pub fn set_f(&mut self, f_num: u8, value: bool) {
        if f_num <= 4 {
            let mask = 1 << if f_num == 0 { 4 } else { f_num - 1 };
            if value {
                self.0 |= mask;
            } else {
                self.0 &= !mask;
            }
        }
    }

    /// Parses this [`DirfArg`] in the corresponding model railroad message format.
    ///
    /// # Returns
    ///
    /// The to this arg corresponding model railroad message value.
    pub(crate) fn dirf(&self) -> u8 {
        self.0
    }
}

/// Overriding the [`Debug`] trait, to show only the corresponding arg states
impl Debug for DirfArg {
    /// Prints the direction and all f-flags from 0 to 4 to the formatter
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "dirf: (dir: {}, f0: {}, f1: {}, f2: {}, f3: {}, f4: {})",
            self.dir(),
            self.f(0),
            self.f(1),
            self.f(2),
            self.f(3),
            self.f(4)
        )
    }
}

/// Holds the track information
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct TrkArg {
    /// The tracks power state (`ON`/`OFF`).
    power: bool,
    /// The tracks idle state.
    idle: bool,
    /// `true`: This master implements this protocol capability.
    /// `false`: This master is `DT200`.
    mlok1: bool,
    /// Indicates that masters programming track is busy.
    prog_busy: bool,
}

impl TrkArg {
    /// Creates a new arg representing the tracks status
    ///
    /// # Parameters
    ///
    /// - `power`: The tracks power state (`On`/`OFF`)
    /// - `idle`: The tracks idle state
    /// - `mlok1`: The protocol Version to use. 0 = `DT200`, 1 = this protocol
    /// - `prog_busy`: Busy status for programming track (Slot 124)
    pub fn new(power: bool, idle: bool, mlok1: bool, prog_busy: bool) -> Self {
        TrkArg {
            power,
            idle,
            mlok1,
            prog_busy,
        }
    }

    /// Parses a model railroad messages trk arg to this struct by extracting the required values.
    ///
    /// # Parameters
    ///
    /// - `trk_arg`: The track message to parse
    pub(crate) fn parse(trk_arg: u8) -> Self {
        let power = trk_arg & 0x01 == 0x01;
        let idle = trk_arg & 0x02 == 0x00;
        let mlok1 = trk_arg & 0x04 == 0x04;
        let prog_busy = trk_arg & 0x08 == 0x08;
        TrkArg {
            power,
            idle,
            mlok1,
            prog_busy,
        }
    }

    /// # Returns
    ///
    /// The power state of the track.
    pub fn power_on(&self) -> bool {
        self.power
    }

    /// # Returns
    ///
    /// The tracks master idle status.
    pub fn track_idle(&self) -> bool {
        self.idle
    }

    /// # Returns
    ///
    /// The available protocol version by the master.
    ///
    /// - `true` = this protocol is fully supported
    /// - `false` = `DT200`
    pub fn mlok1(&self) -> bool {
        self.mlok1
    }

    /// # Returns
    ///
    /// The programing tracks busy status.
    pub fn prog_busy(&self) -> bool {
        self.prog_busy
    }

    /// Parses this arg to a valid model railroad track message byte.
    ///
    /// # Returns
    ///
    /// The model railroad trk message byte matching this [`TrkArg`].
    pub(crate) fn trk_arg(&self) -> u8 {
        let mut trk_arg = if self.power { 0x01 } else { 0x00 };
        if !self.idle {
            trk_arg |= 0x02;
        }
        if self.mlok1 {
            trk_arg |= 0x04;
        }
        if self.prog_busy {
            trk_arg |= 0x08;
        }
        trk_arg
    }
}

/// Holds the function flags 5 to 8.
///
/// This function flags may be used for train sound management if available.
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct SndArg(u8);

impl SndArg {
    /// Creates a new [`SndArg`] with the function flags set.
    ///
    /// # Parameters
    ///
    /// - `f5`: Function flag 5
    /// - `f6`: Function flag 6
    /// - `f7`: Function flag 7
    /// - `f8`: Function flag 8
    pub fn new(f5: bool, f6: bool, f7: bool, f8: bool) -> Self {
        let mut snd = if f5 { 0x01 } else { 0x00 } as u8;
        if f6 {
            snd |= 0x02
        }
        if f7 {
            snd |= 0x04
        }
        if f8 {
            snd |= 0x08
        }
        Self(snd)
    }

    /// Parses a model railroad based function message byte to this arg.
    ///
    /// # Parameters
    ///
    /// - `snd`: A model railroad formatted snd byte
    pub(crate) fn parse(snd: u8) -> Self {
        Self(snd & 0x0F)
    }

    /// # Parameters
    ///
    /// - `f_num`: Which flag to look up
    ///
    /// # Returns
    ///
    /// The value of the `f_num`s function flag. Only values between 5 and 8 are allowed.
    pub fn f(&self, f_num: u8) -> bool {
        if (5..=8).contains(&f_num) {
            self.0 & 1 << (f_num - 5) != 0
        } else {
            false
        }
    }

    /// Sets the value of the `f_num`s function flag to `value`.
    ///
    /// # Parameters
    ///
    /// - `f_num`: The function flags index
    /// - `value`: Which value to set the function bit to
    pub fn set_f(&mut self, f_num: u8, value: bool) {
        if (5..=8).contains(&f_num) {
            let mask = 1 << (f_num - 5);
            if value {
                self.0 |= mask;
            } else {
                self.0 &= !mask;
            }
        }
    }

    /// Parses this [`SndArg`] to a model railroad snd message byte
    pub(crate) fn snd(&self) -> u8 {
        self.0
    }
}

/// Overrides the [`Debug`] trait to show only the corresponding function bits
impl Debug for SndArg {
    /// Prints the f flags from 5 to 8 to the formatter
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "snd: (f5: {}, f6: {}, f7: {}, f8: {})",
            self.f(5),
            self.f(6),
            self.f(7),
            self.f(8)
        )
    }
}

/// Represents the link status of a slot
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum Consist {
    /// Slot is linked up and down
    LogicalMid,
    /// Slot is only linked down
    LogicalTop,
    /// Slot is only linked up
    LogicalSubMember,
    /// Slot is not linked
    Free,
}

/// Represents the usage status of a slot
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum State {
    /// Indicates that this slot is in use by some device. The slot holds a loc address and is refreshed.
    ///
    /// If you want to mark your slot as [`State::InUse`] simply perform a `NULL`-Move on this slot. (Move message with eq, Hashual source and destination)
    InUse,
    /// A loco adr is in the slot but the slot was not refreshed.
    Idle,
    /// This slot holds some loc address and is refreshed.
    Common,
    /// No valid data in this slot, this slot is not refreshed.
    Free,
}

/// Represents the decoders speed control message format used
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum DecoderType {
    /// 28 step decoder with advanced DCC allowed
    Dcc28,
    /// 128 step decoder with advanced DVV allowed
    Dcc128,
    /// 28 step mode in 3 byte PKT regular mode
    Regular28,
    /// 28 step mode. Generates trinary packets for mobile address.
    AdrMobile28,
    /// 14 step speed mode (Speed will match values from 0 to 14)
    Step14,
    /// 128 speed mode packets
    Speed128,
}

/// Holds general slot status information.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct Stat1Arg {
    /// The slots purge status.
    s_purge: bool,
    /// The slots link status.
    consist: Consist,
    /// The slots usage status.
    state: State,
    /// The decoder type used by the slot.
    decoder_type: DecoderType,
}

impl Stat1Arg {
    /// Creates new slot status information
    ///
    /// # Parameters
    ///
    /// - `s_purge`: The slots purge status
    /// - `consist`: The slots link status
    /// - `state`: The slots usage status
    /// - `decoder_type`: The decoder type used to generate loc messages for this slot
    pub fn new(s_purge: bool, consist: Consist, state: State, decoder_type: DecoderType) -> Self {
        Stat1Arg {
            s_purge,
            consist,
            state,
            decoder_type,
        }
    }

    /// Parses a model railroad formatted `stat1` byte into this arg
    ///
    /// # Parameters
    ///
    /// - `stat1`: The status byte to parse
    pub(crate) fn parse(stat1: u8) -> Self {
        let s_purge = stat1 & 0x80 != 0;

        let consist = match stat1 & 0x48 {
            0x48 => Consist::LogicalMid,
            0x08 => Consist::LogicalTop,
            0x40 => Consist::LogicalSubMember,
            0x00 => Consist::Free,
            _ => Consist::Free,
        };

        let state = match stat1 & 0x30 {
            0x30 => State::InUse,
            0x20 => State::Idle,
            0x10 => State::Common,
            0x00 => State::Free,
            _ => State::Free,
        };

        let decoder_type = match stat1 & 0x07 {
            0x02 => DecoderType::Step14,
            0x01 => DecoderType::AdrMobile28,
            0x00 => DecoderType::Regular28,
            0x03 => DecoderType::Speed128,
            0x07 => DecoderType::Dcc128,
            0x04 => DecoderType::Dcc28,
            _ => panic!("The given decoder type was invalid!"),
        };

        Stat1Arg {
            s_purge,
            consist,
            state,
            decoder_type,
        }
    }

    /// # Returns
    ///
    /// The slots purge status
    pub fn s_purge(&self) -> bool {
        self.s_purge
    }

    /// # Returns
    ///
    /// The slots linking state
    pub fn consist(&self) -> Consist {
        self.consist
    }

    /// # Returns
    ///
    /// The usage state of the slot
    pub fn state(&self) -> State {
        self.state
    }

    /// # Returns
    ///
    /// The decoder type to use for this slot
    pub fn decoder_type(&self) -> DecoderType {
        self.decoder_type
    }

    /// Parses this arg to a model railroad defined stat1 message byte
    pub(crate) fn stat1(&self) -> u8 {
        let mut stat1: u8 = if self.s_purge { 0x80 } else { 0x00 };

        stat1 |= match self.consist {
            Consist::LogicalMid => 0x48,
            Consist::LogicalTop => 0x08,
            Consist::LogicalSubMember => 0x40,
            Consist::Free => 0x00,
        };

        stat1 |= match self.state {
            State::InUse => 0x30,
            State::Idle => 0x20,
            State::Common => 0x10,
            State::Free => 0x00,
        };

        stat1 |= match self.decoder_type {
            DecoderType::Dcc28 => 0x04,
            DecoderType::Dcc128 => 0x07,
            DecoderType::Regular28 => 0x00,
            DecoderType::AdrMobile28 => 0x01,
            DecoderType::Step14 => 0x02,
            DecoderType::Speed128 => 0x03,
        };

        stat1
    }
}

/// Extension part for the slot status holding some additional slot information
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct Stat2Arg {
    /// If slots ADV consist is suppressed
    has_adv: bool,
    /// ID1/2 is not used for ID
    no_id_usage: bool,
    /// If this ID is no encoded alias
    id_encoded_alias: bool,
}

impl Stat2Arg {
    /// Creates a new status argument
    ///
    /// # Parameters
    ///
    /// - `has_adv`: If this slot has suppressed ADV consist
    /// - `no_id_usage`: If this slots ID1/2 values are not used to represent the ID
    /// - `id_encoded_alias`: If this ID is no encoded alias
    pub fn new(has_adv: bool, no_id_usage: bool, id_encoded_alias: bool) -> Self {
        Stat2Arg {
            has_adv,
            no_id_usage,
            id_encoded_alias,
        }
    }

    /// Parses a received `stat2` byte by the model railroad to this struct
    pub(crate) fn parse(stat2: u8) -> Self {
        let has_adv = stat2 & 0x01 != 0;

        let no_id_usage = stat2 & 0x04 != 0;

        let id_encoded_alias = stat2 & 0x08 != 0;

        Stat2Arg {
            has_adv,
            no_id_usage,
            id_encoded_alias,
        }
    }

    /// # Returns
    ///
    /// If this slot has suppressed advanced control v
    pub fn has_adv(&self) -> bool {
        self.has_adv
    }

    /// # Returns
    ///
    /// If this slot has suppressed adv
    pub fn no_id_usage(&self) -> bool {
        self.no_id_usage
    }

    /// # Returns
    ///
    /// If this messages id is no encoded alias
    pub fn id_encoded_alias(&self) -> bool {
        self.id_encoded_alias
    }

    /// # Returns
    ///
    /// The values hold by this argument as one byte
    pub(crate) fn stat2(&self) -> u8 {
        let mut stat2 = if self.has_adv { 0x01 } else { 0x00 };
        if self.no_id_usage {
            stat2 |= 0x04;
        }
        if self.id_encoded_alias {
            stat2 |= 0x08;
        }
        stat2
    }
}

/// Represents a copy of the operation code with the highest bit erased
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct LopcArg(u8);

impl LopcArg {
    /// Creates a new operation code copy with the highest bit erased from the given op code byte.
    ///
    /// To get a messages operation code you can use: [Message::opc()]
    pub fn new(opc: u8) -> Self {
        LopcArg::parse(opc & 0x7F)
    }

    /// Parses a new operation code copy from an incoming byte
    pub(crate) fn parse(lopc: u8) -> Self {
        Self(lopc & 0x7F)
    }

    /// # Returns
    ///
    /// The operation code copy argument
    pub(crate) fn lopc(&self) -> u8 {
        self.0
    }

    /// Checks whether an messages operation code matches the operation code held by this argument
    ///
    /// # Parameter
    ///
    /// - `message`: The message to check operation code matching for
    ///
    /// # Returns
    ///
    /// If the messages operation code matches the operation code hold by this argument
    pub fn check_opc(&self, message: &Message) -> bool {
        message.opc() & 0x7F == self.0
    }
}

/// Holds a response code for a before received message
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct Ack1Arg(u8);

impl Ack1Arg {
    /// Creates a new acknowledgment answer
    ///
    /// # Parameter
    ///
    /// - `success`: If this acknowledgment indicates that the request was successfully
    pub fn new(success: bool) -> Self {
        Self(if success { 0x7F } else { 0x00 })
    }

    /// This creates a new acknowledgment answer with only the `code` to send as answer.
    ///
    /// `0x7F` means the request succeeded and `0x00` means the request was denied.
    ///
    /// If you want to mark that you accepted the message use `0x01` and when you want to indicate a blind acceptance use `0x40`
    pub fn new_advanced(code: u8) -> Self {
        Self(code & 0x7F)
    }

    /// Parses the acknowledgment type from a byte
    pub(crate) fn parse(ack1: u8) -> Self {
        Self(ack1)
    }

    /// # Returns
    ///
    /// The acknowledgment parsed to a byte
    pub fn ack1(&self) -> u8 {
        self.0
    }

    /// # Returns
    ///
    /// If this message indicates the operation succeeded
    pub fn success(&self) -> bool {
        self.0 == 0x7F
    }

    /// # Returns
    ///
    /// If the message has not failed
    pub fn limited_success(&self) -> bool {
        self.0 != 0x00
    }

    /// # Returns
    ///
    /// If this message indicates the operation failure
    pub fn failed(&self) -> bool {
        self.0 == 0x00
    }

    /// # Returns
    ///
    /// If this message indicates the operation was accepted but not succeeded yet
    pub fn accepted(&self) -> bool {
        self.0 == 0x01
    }

    /// # Returns
    ///
    /// If this message indicates the operation was accepted without checks, but not succeeded yet
    pub fn accepted_blind(&self) -> bool {
        self.0 == 0x40
    }
}

impl Display for Ack1Arg {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.failed() {
            write!(f, "ack1: (failed)")
        } else if self.accepted() {
            write!(f, "ack1: (accepted)")
        } else if self.accepted_blind() {
            write!(f, "ack1: (accepted_blind)")
        } else {
            write!(f, "ack1: (success, ack: {})", self.0,)
        }
    }
}

/// Indicates which source type the input came from
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum SourceType {
    /// Switch is connected over a DS54 port
    Ds54Aux,
    /// Switch is directly accessible
    Switch,
}

/// A sensors detection state
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum SensorLevel {
    /// The sensor detects some energy flow (sensor on)
    High,
    /// The sensor detects no energy flow (sensor off)
    Low,
}

impl std::ops::Not for SensorLevel {
    type Output = SensorLevel;

    fn not(self) -> Self::Output {
        match self {
            SensorLevel::High => SensorLevel::Low,
            SensorLevel::Low => SensorLevel::High,
        }
    }
}

/// Represents an sensor input argument
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct InArg {
    /// The sensors argument
    address: u16,
    /// The sensors source type
    input_source: SourceType,
    /// The sensors detection state
    sensor_level: SensorLevel,
    /// The sensors control bit that is reserved to future use
    control_bit: bool,
}

impl InArg {
    /// Creates a new sensors input argument
    ///
    /// # Parameters
    ///
    /// - `address`: The sensors address (0 - 2047)
    /// - `input_source`: The sensors input source type
    /// - `sensor_level`: The sensors state (High = On, Low = Off)
    /// - `control_bit`: Control bit that is reserved for future use.
    pub fn new(
        address: u16,
        input_source: SourceType,
        sensor_level: SensorLevel,
        control_bit: bool,
    ) -> Self {
        InArg {
            address: address & 0x07FF,
            input_source,
            sensor_level,
            control_bit,
        }
    }

    /// Parses the sensors information from two bytes `in1` and `in2`
    pub(crate) fn parse(in1: u8, in2: u8) -> Self {
        let mut address = in1 as u16;
        address |= (in2 as u16 & 0x0F) << 7;

        let input_source = if in2 & 0x20 == 0 {
            SourceType::Ds54Aux
        } else {
            SourceType::Switch
        };

        let sensor_level = if (in2 & 0x10) != 0 {
            SensorLevel::High
        } else {
            SensorLevel::Low
        };
        let control_bit = (in2 & 0x40) != 0;
        Self {
            address,
            input_source,
            sensor_level,
            control_bit,
        }
    }

    /// # Returns
    ///
    /// The address of this sensor
    pub fn address(&self) -> u16 {
        self.address
    }

    /// # Returns
    ///
    /// The address with the sensors source type set as least significant bit
    pub fn address_ds54(&self) -> u16 {
        (self.address << 1)
            | match self.input_source {
                SourceType::Switch => 1,
                SourceType::Ds54Aux => 0,
            }
    }

    /// # Returns
    ///
    /// The sensors source type
    pub fn input_source(&self) -> SourceType {
        self.input_source
    }

    /// # Returns
    ///
    /// The sensors state (High = On, Low = Off)
    pub fn sensor_level(&self) -> SensorLevel {
        self.sensor_level
    }

    /// # Returns
    ///
    /// The sensors control bit
    pub fn control_bit(&self) -> bool {
        self.control_bit
    }

    /// Sets the address of this sensor argument
    ///
    /// # Parameters
    ///
    /// - `address`: The address to set (0 - 2047)
    pub fn set_address(&mut self, address: u16) {
        if address <= 0x07FF {
            self.address = address;
        }
    }

    /// Sets the address with the sensors source type as least significant bit
    ///
    /// # Parameters
    ///
    /// - `address_ds54`: The address and as least significant the source type
    pub fn set_address_ds54(&mut self, address_ds54: u16) {
        if address_ds54 <= 0x0FFF {
            self.input_source = if address_ds54 & 1 == 0 {
                SourceType::Ds54Aux
            } else {
                SourceType::Switch
            };
            self.set_address(address_ds54 >> 1);
        }
    }

    /// Sets the sensors input source type
    ///
    /// # Parameters
    ///
    /// - `input_source`: The input source the sensor used
    pub fn set_input_source(&mut self, input_source: SourceType) {
        self.input_source = input_source;
    }

    /// Sets the sensors activation state
    ///
    /// # Parameters
    ///
    /// - `sensor_level`: The activation state to use (High = ON, Low = OFF)
    pub fn set_sensor_level(&mut self, sensor_level: SensorLevel) {
        self.sensor_level = sensor_level;
    }

    /// Sets the control bit of this sensor arg to the given value.
    ///
    /// # Parameters
    ///
    /// - `control_bit`: The bit to set
    pub fn set_control_bit(&mut self, control_bit: bool) {
        self.control_bit = control_bit;
    }

    /// Parses this sensors least significant address bit in one byte
    pub(crate) fn in1(&self) -> u8 {
        self.address as u8 & 0x7F
    }

    /// Parses this sensors most significant address bit and its input source type
    /// as well as the sensor activation state and control bit in one byte,
    pub(crate) fn in2(&self) -> u8 {
        let mut in2 = ((self.address >> 7) as u8) & 0x0F;
        in2 |= match self.input_source {
            SourceType::Ds54Aux => 0x00,
            SourceType::Switch => 0x20,
        };
        in2 |= match self.sensor_level {
            SensorLevel::High => 0x10,
            SensorLevel::Low => 0x00,
        };
        if self.control_bit {
            in2 |= 0x40;
        }
        in2
    }
}

/// Metainformation for a device
#[derive(Copy, Clone, Eq, Hash, PartialEq, Debug)]
pub enum SnArg {
    /// The devices meta information by device type
    /// - 0: Device address
    /// - 1: If this device is a switch
    /// - 2: If this device is active
    SwitchType(u16, bool, bool),
    /// The devices meta information by output
    /// - 0: Device address
    /// - 1: The activation state of the straight switch part
    /// - 2: The activation state of the curved switch part
    SwitchDirectionStatus(u16, SensorLevel, SensorLevel),
}

impl SnArg {
    /// Parses the sensors information from two bytes `sn1` and `sn2`
    pub(crate) fn parse(sn1: u8, sn2: u8) -> Self {
        let mut address = sn1 as u16;
        address |= (sn2 as u16 & 0x0F) << 7;

        let format = sn2 & 0x40 == 0x40;

        let t = sn2 & 0x10 == 0x10;
        let c = sn2 & 0x20 == 0x20;

        if format {
            SnArg::SwitchType(address, c, t)
        } else {
            SnArg::SwitchDirectionStatus(
                address,
                if c {
                    SensorLevel::High
                } else {
                    SensorLevel::Low
                },
                if t {
                    SensorLevel::High
                } else {
                    SensorLevel::Low
                },
            )
        }
    }

    /// # Returns
    ///
    /// The device address
    pub fn address(&self) -> u16 {
        match *self {
            SnArg::SwitchType(address, ..) => address,
            SnArg::SwitchDirectionStatus(address, ..) => address,
        }
    }

    /// # Returns
    ///
    /// Parses this low address bits in a writeable byte
    pub(crate) fn sn1(&self) -> u8 {
        (match *self {
            SnArg::SwitchDirectionStatus(address, ..) => address,
            SnArg::SwitchType(address, ..) => address,
        } as u8)
            & 0x7F
    }

    /// # Returns
    ///
    /// Parses the status information and the high address bits into a writeable byte
    pub(crate) fn sn2(&self) -> u8 {
        match *self {
            SnArg::SwitchType(address, is_switch, state) => {
                let mut sn2 = ((address >> 7) as u8 & 0x0F) | 0x40;

                sn2 |= if is_switch { 0x20 } else { 0x00 };
                sn2 | if state { 0x10 } else { 0x00 }
            }
            SnArg::SwitchDirectionStatus(address, straight_status, curved_status) => {
                let mut sn2 = (address >> 7) as u8 & 0x0F;

                sn2 |= match straight_status {
                    SensorLevel::High => 0x20,
                    SensorLevel::Low => 0x00,
                };
                sn2 | match curved_status {
                    SensorLevel::High => 0x10,
                    SensorLevel::Low => 0x00,
                }
            }
        }
    }
}

/// Id of the slot controlling device
///
/// - 0: No ID being used
/// - 00/80 - 3F/81: ID shows PC usage
/// - 00/02 - 3F/83: System reserved
/// - 00/04 - 3F/FE: normal throttle range
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct IdArg(u16);

impl IdArg {
    /// Creates a new device id
    ///
    /// # Parameters
    ///
    /// - `id`: A fourteen bit device address
    pub fn new(id: u16) -> Self {
        IdArg(id & 0x3FFF)
    }

    /// Parses the device id from two bytes `id1` and `id2`
    pub(crate) fn parse(id1: u8, id2: u8) -> Self {
        IdArg((((id2 & 0x7F) as u16) << 7) | ((id1 & 0x7F) as u16))
    }

    /// # Returns
    ///
    /// The device `id`
    pub fn id(&self) -> u16 {
        self.0
    }

    /// # Returns
    ///
    /// The seven least significant address bits
    pub(crate) fn id1(&self) -> u8 {
        self.0 as u8 & 0x7F
    }

    /// # Returns
    ///
    /// The seven most significant address bits
    pub(crate) fn id2(&self) -> u8 {
        (self.0 >> 7) as u8 & 0x7F
    }
}

/// Represents power information for a specific railway sector
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct MultiSenseArg {
    /// This messages three bit represented type
    m_type: u8,
    /// The present state
    present: bool,
    /// The board address corresponding to this message
    board_address: u8,
    /// The zone corresponding to this message
    zone: u8,
}

impl MultiSenseArg {
    /// Creates new power information for a specified railway sector
    ///
    /// # Parameters
    ///
    /// - `m_type`: The messages type
    /// - `present`: The present state of the sender
    /// - `board_address`: The board address
    /// - `zone`: The zone address
    pub fn new(m_type: u8, present: bool, board_address: u8, zone: u8) -> Self {
        Self {
            m_type: m_type & 0x07,
            present,
            board_address,
            zone,
        }
    }

    /// Parses the power information id from two bytes `m_high` and `zas`
    pub(crate) fn parse(m_high: u8, zas: u8) -> Self {
        let m_type = (0xE0 & m_high) >> 5;
        let present = 0x10 & m_high == 0x10;
        let board_address = ((0x0F & m_high) << 4) | ((zas & 0xF0) >> 4);
        let zone = 0x0F & zas;

        MultiSenseArg {
            m_type,
            present,
            board_address,
            zone,
        }
    }

    /// # Returns
    ///
    /// The three bit message type
    pub fn m_type(&self) -> u8 {
        self.m_type
    }

    /// # Returns
    ///
    /// The senders present status
    pub fn present(&self) -> bool {
        self.present
    }

    /// # Returns
    ///
    /// The sections board address
    pub fn board_address(&self) -> u8 {
        self.board_address
    }

    /// # Returns
    ///
    /// The sections zone
    pub fn zone(&self) -> u8 {
        self.zone
    }

    /// # Returns
    ///
    /// One byte holding the least significant board address and zone bits
    pub(crate) fn zas(&self) -> u8 {
        self.zone | ((self.board_address & 0x0F) << 4)
    }

    /// # Returns
    ///
    /// The low address bits as well as the messages type and present status as one byte
    pub(crate) fn m_high(&self) -> u8 {
        ((self.board_address & 0xF0) >> 4)
            | ((self.m_type & 0x07) << 5)
            | if self.present { 0x10 } else { 0x00 }
    }
}

/// The functions group
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum FunctionGroup {
    /// Function bits 9, 10 and 11 are available
    F9TO11,
    /// Function bits 13 to 19 are available
    F13TO19,
    /// Function bits 12, 20 and 28 are available
    F12F20F28,
    /// Function bit 21 to 27 are available
    F21TO27,
}

/// Represents the function bits of one function group.
///
/// - 0: The functions group type
/// - 1: The functions bits set
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct FunctionArg(u8, u8);

impl FunctionArg {
    /// Creates a new function arg for a given group.
    ///
    /// # Parameters
    ///
    /// - `group`: The functions group to set the values to.
    pub fn new(group: FunctionGroup) -> Self {
        FunctionArg(
            match group {
                FunctionGroup::F9TO11 => 0x07,
                FunctionGroup::F13TO19 => 0x08,
                FunctionGroup::F12F20F28 => 0x05,
                FunctionGroup::F21TO27 => 0x09,
            },
            0,
        )
    }

    /// Parses the group and function bits from two bits.
    pub(crate) fn parse(group: u8, function: u8) -> Self {
        FunctionArg(group, function)
    }

    /// # Returns
    ///
    /// The value of the `f_num`s function bit value if this bit is contained in
    /// this args function group.
    pub fn f(&self, f_num: u8) -> bool {
        if f_num > 8 && f_num < 12 && self.0 == 0x07 {
            (self.1 >> (f_num - 5)) & 1 != 0
        } else if (f_num == 12 || f_num == 20 || f_num == 28) && self.0 == 0x05 {
            (self.1
                >> (if f_num == 12 {
                    4
                } else if f_num == 20 {
                    5
                } else {
                    6
                }))
                & 1
                != 0
        } else if f_num > 12 && f_num < 20 && self.0 == 0x08 {
            (self.1 >> (f_num - 13)) & 1 != 0
        } else if f_num > 20 && f_num < 28 && self.0 == 0x09 {
            (self.1 >> (f_num - 21)) & 1 != 0
        } else {
            false
        }
    }

    /// Sets the `f_num` function bits value, if it is present in this args function group.
    ///
    /// # Parameters
    ///
    /// - `f_num`: The bit to set
    /// - `value`: The bits value
    ///
    /// # Returns
    ///
    /// A mutable reference of this struct instance.
    pub fn set_f(&mut self, f_num: u8, value: bool) -> &mut Self {
        let mask = if f_num > 8 && f_num < 12 && self.0 == 0x07 {
            1 << (f_num - 5)
        } else if (f_num == 12 || f_num == 20 || f_num == 28) && self.0 == 0x05 {
            1 << (if f_num == 12 {
                0
            } else if f_num == 20 {
                1
            } else {
                2
            })
        } else if f_num > 12 && f_num < 20 && self.0 == 0x08 {
            1 << (f_num - 13)
        } else if f_num > 20 && f_num < 28 && self.0 == 0x09 {
            1 << (f_num - 21)
        } else {
            0x00
        };

        if value {
            self.1 |= mask;
        } else {
            self.1 &= !mask;
        }

        self
    }

    /// # Returns
    ///
    /// The function group specifying which function values may be set.
    pub fn function_group(&self) -> FunctionGroup {
        match self.0 {
            0x07 => FunctionGroup::F9TO11,
            0x05 => FunctionGroup::F12F20F28,
            0x08 => FunctionGroup::F13TO19,
            0x09 => FunctionGroup::F21TO27,
            _ => FunctionGroup::F9TO11,
        }
    }

    /// # Returns
    ///
    /// The functions group represented as one byte.
    pub(crate) fn group(&self) -> u8 {
        self.0
    }

    /// # Returns
    ///
    /// The function bits represented as one byte.
    pub(crate) fn function(&self) -> u8 {
        self.1
    }
}

/// Overriding debug to only display the relevant function bits.
impl Debug for FunctionArg {
    /// Prints the group corresponding function bit values.
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self.function_group() {
            FunctionGroup::F9TO11 => {
                write!(
                    f,
                    "function_arg: (group: {:?}, f9: {}, f10: {}, f11: {})",
                    FunctionGroup::F9TO11,
                    self.f(9),
                    self.f(10),
                    self.f(11)
                )
            }
            FunctionGroup::F13TO19 => {
                write!(f,
                       "function_arg: (group: {:?}, f13: {}, f14: {}, f15: {}, f16: {}, f17: {}, f18: {}, f19: {})",
                       FunctionGroup::F13TO19,
                       self.f(13),
                       self.f(14),
                       self.f(15),
                       self.f(16),
                       self.f(17),
                       self.f(18),
                       self.f(19),
                )
            }
            FunctionGroup::F12F20F28 => {
                write!(
                    f,
                    "function_arg: (group: {:?}, f12: {}, f20: {}, f28: {})",
                    FunctionGroup::F12F20F28,
                    self.f(12),
                    self.f(20),
                    self.f(28)
                )
            }
            FunctionGroup::F21TO27 => {
                write!(f,
                       "function_arg: (group: {:?}, f21: {}, f22: {}, f23: {}, f24: {}, f25: {}, f26: {}, f27: {})",
                       FunctionGroup::F21TO27,
                       self.f(21),
                       self.f(22),
                       self.f(23),
                       self.f(24),
                       self.f(25),
                       self.f(26),
                       self.f(27)
                )
            }
        }
    }
}

/// Representing the command mode used to write to the programming track
///
/// # Type Codes Table
///
/// | [Pcmd::byte_mode] | [Pcmd::ops_mode] | [Pcmd::ty0] | [Pcmd::ty1] | Mode                            |
/// |-------------------|------------------|-------------|-------------|---------------------------------|
/// | 0                 | 0                | 0           | 0           | Abort operation                 |
/// | 1                 | 0                | 0           | 0           | Paged mode                      |
/// | x                 | 0                | 0           | 1           | Direct mode                     |
/// | x                 | 0                | 1           | 0           | Physical register               |
/// | x                 | 0                | 1           | 1           | service track reserved function |
/// | x                 | 1                | 0           | 0           | no feedback                     |
/// | x                 | 1                | 0           | 0           | feedback                        |
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct Pcmd {
    /// Whether to write or if `false` read
    write: bool,
    /// Whether to use byte or bitwise operation mode
    byte_mode: bool,
    /// Whether to use the main track or the programming track
    ops_mode: bool,
    /// First programing type select bit
    ty0: bool,
    /// Second programming type select bit
    ty1: bool,
}

impl Pcmd {
    /// Creates a new programm control argument
    ///
    /// For near information on `ty0` and `ty1` see [Pcmd].
    ///
    /// # Parameters
    ///
    /// - `write`: Whether to write or read
    /// - `byte_mode`: Whether to use bytewise or bitwise mode
    /// - `ops_mode`: Whether to use the main track or the programming track
    /// - `ty0`: See [Pcmd]
    /// - `ty1`: See [Pcmd]
    pub fn new(write: bool, byte_mode: bool, ops_mode: bool, ty0: bool, ty1: bool) -> Self {
        Pcmd {
            write,
            byte_mode,
            ops_mode,
            ty0,
            ty1,
        }
    }

    /// Reads the programming control information from one byte
    pub(crate) fn parse(pcmd: u8) -> Self {
        let write = pcmd & 0x20 == 0x20;
        let byte_mode = pcmd & 0x40 == 0x40;
        let ops_mode = pcmd & 0x02 == 0x02;
        let ty0 = pcmd & 0x80 == 0x80;
        let ty1 = pcmd & 0x01 == 0x01;

        Pcmd {
            write,
            byte_mode,
            ops_mode,
            ty0,
            ty1,
        }
    }

    /// # Returns
    ///
    /// Whether to write or read
    pub fn write(&self) -> bool {
        self.write
    }

    /// # Returns
    ///
    /// Whether to use byte or bit mode
    pub fn byte_mode(&self) -> bool {
        self.byte_mode
    }

    /// # Returns
    ///
    /// Whether to use the main or programming track
    pub fn ops_mode(&self) -> bool {
        self.ops_mode
    }

    /// See [Pcmd]
    pub fn ty0(&self) -> bool {
        self.ty0
    }

    /// See [Pcmd]
    pub fn ty1(&self) -> bool {
        self.ty1
    }

    /// Sets the write argument
    ///
    /// # Parameters
    ///
    /// - `write`: Whether to write or read
    pub fn set_write(&mut self, write: bool) {
        self.write = write
    }

    /// Sets the byte_mode argument
    ///
    /// # Parameters
    ///
    /// - `byte_mode`: Whether to use byte or bit mode
    pub fn set_byte_mode(&mut self, byte_mode: bool) {
        self.byte_mode = byte_mode
    }

    /// Sets the ops_mode argument
    ///
    /// # Parameters
    ///
    /// - `ops_mode`: Whether to use the main or programming track
    pub fn set_ops_mode(&mut self, ops_mode: bool) {
        self.ops_mode = ops_mode
    }

    /// See [Pcmd]
    pub fn set_ty0(&mut self, ty0: bool) {
        self.ty0 = ty0
    }

    /// See [Pcmd]
    pub fn set_ty1(&mut self, ty1: bool) {
        self.ty1 = ty1
    }

    /// # Returns
    ///
    /// Parses the programming information data into one representing byte
    pub(crate) fn pcmd(&self) -> u8 {
        let mut pcmd = if self.write { 0x20 } else { 0x00 };
        if self.byte_mode {
            pcmd |= 0x40;
        }
        if self.ops_mode {
            pcmd |= 0x02;
        }
        if self.ty0 {
            pcmd |= 0x80;
        }
        if self.ty1 {
            pcmd |= 0x01;
        }
        pcmd
    }
}

/// Holding programming error flags
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct PStat {
    /// User canceled operation
    user_aborted: bool,
    /// No read acknowledgment
    no_read_ack: bool,
    /// No write acknowledgment
    no_write_ack: bool,
    /// No train on the programming track to programm
    programming_track_empty: bool,
}

impl PStat {
    /// Creates new programming error information
    ///
    /// # Parameters
    ///
    /// - `user_aborted`: If an user canceled the programming operation
    /// - `no_read_ack`: No read acknowledgment received
    /// - `no_write_ack`: No write acknowledgment received
    /// - `programming_track_empty`: No train is on the programming track
    pub fn new(
        user_aborted: bool,
        no_read_ack: bool,
        no_write_ack: bool,
        programming_track_empty: bool,
    ) -> Self {
        PStat {
            user_aborted,
            no_read_ack,
            no_write_ack,
            programming_track_empty,
        }
    }

    /// Parses the error flags from one byte
    pub(crate) fn parse(stat: u8) -> Self {
        let user_aborted = stat & 0x01 == 0x01;
        let no_read_ack = stat & 0x02 == 0x02;
        let no_write_ack = stat & 0x04 == 0x04;
        let programming_track_empty = stat & 0x08 == 0x08;

        PStat {
            user_aborted,
            no_read_ack,
            no_write_ack,
            programming_track_empty,
        }
    }

    /// # Returns
    ///
    /// If the operation was aborted by a user
    pub fn user_aborted(&self) -> bool {
        self.user_aborted
    }

    /// # Returns
    ///
    /// If the operation was canceled by a missing read acknowledgment
    pub fn no_read_ack(&self) -> bool {
        self.no_read_ack
    }

    /// # Returns
    ///
    /// If the operation was canceled by a missing write acknowledgment
    pub fn no_write_ack(&self) -> bool {
        self.no_write_ack
    }

    /// # Returns
    ///
    /// If no train was found to programm
    pub fn programming_track_empty(&self) -> bool {
        self.programming_track_empty
    }

    /// # Returns
    ///
    /// A byte representing all found error states
    pub(crate) fn stat(&self) -> u8 {
        let mut stat = if self.user_aborted { 0x01 } else { 0x00 };
        if self.no_read_ack {
            stat |= 0x02;
        }
        if self.no_write_ack {
            stat |= 0x04;
        }
        if self.programming_track_empty {
            stat |= 0x08;
        }
        stat
    }
}

/// Holds control variables and data arguments.
#[derive(Copy, Clone, Eq, Hash, PartialEq, Default)]
pub struct CvDataArg(u16, u8);

impl CvDataArg {
    /// Creates a new empty arg.
    pub fn new() -> CvDataArg {
        CvDataArg(0, 0)
    }

    /// Parses cv and data from three byte
    pub(crate) fn parse(cvh: u8, cvl: u8, data7: u8) -> Self {
        let mut cv_arg = cvl as u16;
        let data = ((cvh & 0x02) << 6) | data7;

        let mut high_cv_arg = cvh & 0x01;
        high_cv_arg |= (cvh & 0x30) >> 3;

        cv_arg |= (high_cv_arg as u16) << 7;

        CvDataArg(cv_arg, data)
    }

    /// # Parameters
    ///
    /// - `d_num`: Wich data bit to return (Value must be between 0 and 7 (inclusive))
    ///
    /// # Returns
    ///
    /// The data bit specified by `d_num`
    pub fn data(&self, d_num: u8) -> bool {
        (self.1 >> d_num) & 0x01 != 0
    }

    /// # Parameters
    ///
    /// - `cv_num`: Wich cv bit to return (Value must be between 0 and 9 (inclusive))
    ///
    /// # Returns
    ///
    /// The cv bit specified by `cv_num`
    pub fn cv(&self, cv_num: u8) -> bool {
        self.0 >> cv_num & 1 != 0
    }

    /// Sets the specified data bit to the given state
    ///
    /// # Parameters
    ///
    /// - `d_num`: Wich data bit to set
    /// - `value`: The value to set the data bit to
    pub fn set_data(&mut self, d_num: u8, value: bool) -> &mut Self {
        let mask = 1 << d_num;

        if value {
            self.1 |= mask;
        } else {
            self.1 &= !mask;
        }

        self
    }

    /// Sets the specified cv bit to the given state
    ///
    /// # Parameters
    ///
    /// - `cv_num`: Wich cv bit to set
    /// - `value`: The value to set the cv bit to
    pub fn set_cv(&mut self, cv_num: u8, value: bool) -> &mut Self {
        let mask = (1 << cv_num) & 0x03FF;

        if value {
            self.0 |= mask;
        } else {
            self.0 &= !mask;
        }

        self
    }

    /// # Returns
    ///
    /// The high part of the cv values and the seventh data bit as one byte
    pub(crate) fn cvh(&self) -> u8 {
        let mut cvh = (self.0 >> 7) as u8;
        let high_cv = cvh & 0x06 << 3;
        cvh &= 0x01;
        cvh |= high_cv;
        if self.data(7) {
            cvh |= 0x02;
        }
        cvh
    }

    /// # Returns
    ///
    /// The low part of the cv values as one byte
    pub(crate) fn cvl(&self) -> u8 {
        self.0 as u8 & 0x7F
    }

    /// # Returns
    ///
    /// The data bits from 0 to 6 (inclusive) as one byte
    pub(crate) fn data7(&self) -> u8 {
        self.1 & 0x7F
    }
}

/// Overridden for precise value orientated output
impl Debug for CvDataArg {
    /// Writes all args and cv values to the formatter
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "cv_data_arg: (data: (d0: {}, d1: {}, d2: {}, d3: {}, d4: {}, d5: {}, d6: {}, d7: {}), cv: (cv0: {}, cv1: {}, cv2: {}, cv3: {}, cv4: {}, cv5: {}, cv6: {}, cv7: {}, cv8: {}, cv9: {}))",
            self.data(0),
            self.data(1),
            self.data(2),
            self.data(3),
            self.data(4),
            self.data(5),
            self.data(6),
            self.data(7),
            self.cv(0),
            self.cv(1),
            self.cv(2),
            self.cv(3),
            self.cv(4),
            self.cv(5),
            self.cv(6),
            self.cv(7),
            self.cv(8),
            self.cv(9)
        )
    }
}

/// Holding the clocks information
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct FastClock {
    /// The clocks tick rate. (0 = Frozen), (x = x to 1 rate),
    clk_rate: u8,
    /// Intern subminute counter
    frac_mins: u16,
    /// The clocks minutes
    mins: u8,
    /// The clocks set hours
    hours: u8,
    /// The clocks set days
    days: u8,
    /// The clock control
    clk_cntrl: u8,
}

impl FastClock {
    /// Creates a new clock synchronise information
    ///
    /// # Parameters
    ///
    /// - `clock_rate`: The clocks tick rate. (0 = Frozen), (x = x to 1 rate)
    /// - `frac_mins`: The internal subminute counter
    /// - `mins`: The clock mins calculated by 256-MINS%60
    /// - `hours`: The clocks hours calculated by 256-HRS%24
    /// - `days`: The number of 24 hour cycles passed
    /// - `clk_cntrl`: Clock control information. third bit must be true to mark this clock data valid.
    pub fn new(clk_rate: u8, frac_mins: u16, mins: u8, hours: u8, days: u8, clk_cntrl: u8) -> Self {
        FastClock {
            clk_rate,
            frac_mins,
            mins,
            hours,
            days,
            clk_cntrl,
        }
    }

    /// Calculates the clock information from 7 bytes
    ///
    /// # Parameters
    ///
    /// - `clock_rate`: The clocks tick rate. (0 = Frozen), (x = x to 1 rate)
    /// - `frac_minsl`: The least significant part of the internal subminute counter
    /// - `frac_minsh`: The most significant part of the internal subminute counter
    /// - `mins`: The clock mins calculated by 256-MINS%60
    /// - `hours`: The clocks hours calculated by 256-HRS%24
    /// - `days`: The number of 24 hour cycles passed
    /// - `clk_cntrl`: Clock control information. third bit must be true to mark this clock data valid.
    fn parse(
        clk_rate: u8,
        frac_minsl: u8,
        frac_minsh: u8,
        mins: u8,
        hours: u8,
        days: u8,
        clk_cntrl: u8,
    ) -> Self {
        FastClock {
            clk_rate: clk_rate & 0x7F,
            frac_mins: (frac_minsl as u16) | ((frac_minsh as u16) << 8),
            mins,
            hours,
            days,
            clk_cntrl,
        }
    }

    /// # Returns
    ///
    /// The clocks rate
    pub fn clk_rate(&self) -> u8 {
        self.clk_rate
    }

    /// # Returns
    ///
    /// The clocks least significant internal counter part
    fn frac_minsl(&self) -> u8 {
        self.frac_mins as u8
    }

    /// # Returns
    ///
    /// The clocks most significant internal counter part
    fn frac_minsh(&self) -> u8 {
        (self.frac_mins >> 8) as u8
    }

    /// # Returns
    ///
    /// The internal clock counter
    pub fn frac_mins(&self) -> u16 {
        self.frac_mins
    }

    /// # Returns
    ///
    /// The clocks minutes. Represented by (256-MINS%60)
    pub fn mins(&self) -> u8 {
        self.mins
    }

    /// # Returns
    ///
    /// The clocks hours. Represented by (256-HRS%24)
    pub fn hours(&self) -> u8 {
        self.hours
    }

    /// # Retuns
    ///
    /// The count of 24 hour cycles passed
    pub fn days(&self) -> u8 {
        self.days
    }

    /// # Returns
    ///
    /// General clock control information.
    ///
    /// The third bit represents the valid state of this message (0 = invalid)
    pub fn clk_cntrl(&self) -> u8 {
        self.clk_cntrl
    }
}

/// The function bits accessible by the corresponding [ImArg]
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum ImFunctionType {
    /// Functions 9 to 12 (inclusive) are accessible
    F9to12,
    /// Functions 13 to 20 (inclusive) are accessible
    F13to20,
    /// Functions 21 to 28 (inclusive) are accessible
    F21to28,
}

/// The address in the right format used by the corresponding [ImArg]
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum ImAddress {
    /// A short 8 bit address
    Short(u8),
    /// A long 16 bit address
    Long(u16),
}

/// This arg hold function bit information
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct ImArg {
    /// I don't get the concrete meaning and functionality of this arg
    dhi: u8,
    /// This is the address to set the function bits to
    address: ImAddress,
    /// This is the functions settable by this arg
    function_type: ImFunctionType,
    /// This holds the function bits
    function_bits: u8,
    /// Unused for now, do what you want
    im5: u8,
}

impl ImArg {
    /// Creates a new function arg
    ///
    /// # Parameters
    ///
    /// - `dhi`: I don't get the concrete meaning and functionality of this arg
    /// - `address`: The address to set the function bits for
    /// - `function_type`: Wich functions should be settable
    /// - `im5`: Unused parameter
    pub fn new(dhi: u8, address: ImAddress, function_type: ImFunctionType, im5: u8) -> Self {
        ImArg {
            dhi,
            address,
            function_type,
            function_bits: 0x00,
            im5,
        }
    }

    /// Calculates the information of one im arg from eight bytes
    ///
    /// # Parameters
    ///
    /// - `_`: Not used, as it was always the same value
    /// - `reps`: The function bits range type
    /// - `dhi`: Not understood by me
    /// - `im1-5`: The address and function bits
    pub(crate) fn parse(
        _: u8,
        reps: u8,
        dhi: u8,
        im1: u8,
        im2: u8,
        im3: u8,
        im4: u8,
        im5: u8,
    ) -> ImArg {
        if reps == 0x44 || (reps == 0x34 && (im3 & 0x20) == 0x20) {
            let address = ImAddress::Long(((im2 as u16) << 8) | im1 as u16);

            let function_type = if im3 == 0x5E {
                ImFunctionType::F13to20
            } else if im3 == 0x5F {
                ImFunctionType::F21to28
            } else {
                ImFunctionType::F9to12
            };
            let mut function_bits = match function_type {
                ImFunctionType::F21to28 => im4,
                ImFunctionType::F13to20 => im4,
                ImFunctionType::F9to12 => im3 & !0x20,
            };

            function_bits &= 0x7F;

            Self {
                dhi,
                address,
                function_type,
                function_bits,
                im5,
            }
        } else {
            let address = ImAddress::Short(im1);

            let function_type = if im2 == 0x5E {
                ImFunctionType::F13to20
            } else if im2 == 0x5F {
                ImFunctionType::F21to28
            } else {
                ImFunctionType::F9to12
            };
            let mut function_bits = match function_type {
                ImFunctionType::F13to20 => im3,
                ImFunctionType::F21to28 => im3,
                ImFunctionType::F9to12 => im2 & !0x2F,
            };

            function_bits &= 0x7F;

            Self {
                dhi,
                address,
                function_type,
                function_bits,
                im5,
            }
        }
    }

    /// # Returns
    ///
    /// The type of this function arg as one byte
    pub(crate) fn reps(&self) -> u8 {
        match self.address {
            ImAddress::Short(_) => match self.function_type {
                ImFunctionType::F9to12 => 0x24,
                ImFunctionType::F13to20 => 0x34,
                ImFunctionType::F21to28 => 0x34,
            },
            ImAddress::Long(_) => match self.function_type {
                ImFunctionType::F9to12 => 0x34,
                ImFunctionType::F13to20 => 0x44,
                ImFunctionType::F21to28 => 0x44,
            },
        }
    }

    /// # Returns
    ///
    /// The dhi byte, holding special address and bit information.
    pub fn dhi(&self) -> u8 {
        self.dhi
    }

    /// # Returns
    ///
    /// The address in long or short format
    pub fn address(&self) -> ImAddress {
        self.address
    }

    /// # Returns
    ///
    /// The type specifying wich function bits are settable
    pub fn function_type(&self) -> ImFunctionType {
        self.function_type
    }

    /// Calculates the `f_num`s function bit
    ///
    /// # Parameters
    ///
    /// - `f_num`: The function bits number to get
    ///
    /// # Returns
    ///
    /// The value of the `f_num`s function bit
    pub fn f(&self, f_num: u8) -> bool {
        let dist = match self.function_type {
            ImFunctionType::F13to20 => 21,
            ImFunctionType::F21to28 => 13,
            ImFunctionType::F9to12 => 9,
        };

        (self.function_bits >> (f_num - dist)) & 0x01 == 0x01
    }

    /// Sets the `f_num`s function bit to the given value `f`.
    ///
    /// # Parameters
    ///
    /// - `f_num`: The function bit to set
    /// - `f`: The value to set the function bit to
    pub fn set_f(&mut self, f_num: u8, f: bool) {
        let dist = match self.function_type {
            ImFunctionType::F13to20 => 21,
            ImFunctionType::F21to28 => 13,
            ImFunctionType::F9to12 => 9,
        };

        let mask = 0x01 << (f_num - dist);

        if f {
            self.function_bits |= mask;
        } else {
            self.function_bits &= !mask;
        }
    }

    /// # Returns
    ///
    /// The first function arg
    pub(crate) fn im1(&self) -> u8 {
        match self.address {
            ImAddress::Short(adr) => adr,
            ImAddress::Long(adr) => adr as u8,
        }
    }

    /// # Returns
    ///
    /// The second function arg
    pub(crate) fn im2(&self) -> u8 {
        match self.address {
            ImAddress::Short(_) => match self.function_type {
                ImFunctionType::F9to12 => (self.function_bits & 0x7F) | 0x20,
                ImFunctionType::F13to20 => 0x5E,
                ImFunctionType::F21to28 => 0x5F,
            },
            ImAddress::Long(adr) => (adr >> 8) as u8,
        }
    }

    /// # Returns
    ///
    /// The third function arg
    pub(crate) fn im3(&self) -> u8 {
        match self.address {
            ImAddress::Short(_) => {
                if self.function_type == ImFunctionType::F9to12 {
                    0x00
                } else {
                    self.function_bits
                }
            }
            ImAddress::Long(_) => match self.function_type {
                ImFunctionType::F9to12 => (self.function_bits & 0x7F) | 0x20,
                ImFunctionType::F13to20 => 0x5E,
                ImFunctionType::F21to28 => 0x5F,
            },
        }
    }

    /// # Returns
    ///
    /// The fourth function arg
    pub(crate) fn im4(&self) -> u8 {
        if self.reps() == 0x34 && self.function_type != ImFunctionType::F9to12 {
            return self.function_bits;
        }
        0x00
    }

    /// # Returns
    ///
    /// The fifth function arg
    pub(crate) fn im5(&self) -> u8 {
        self.im5
    }
}

/// Holds messages for writing data to slots
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum WrSlDataStructure {
    /// Represents clock sync information
    ///
    /// # Parameters
    ///
    /// - `FastClock`: The clock information
    /// - `TrkArg`: The track
    /// - `IdArg`: The ID of the slots user
    DataTime(FastClock, TrkArg, IdArg),
    /// Creates new data to write to the programming track
    ///
    /// # Parameters
    ///
    /// - `Pcmd`: The programming command to use
    /// - `AddressArg`: Operation mode programming bits as address
    /// - `TrkArg`: The current track information to set
    /// - `CvDataArg`: The command value and data bits to programm
    DataPt(Pcmd, AddressArg, TrkArg, CvDataArg),
    /// Represents a general message to write data to one specified slot
    ///
    /// # Parameters
    ///
    /// - `SlotArg`: The slot to write to
    /// - `Stat1Arg`: The slots general status information
    /// - `Stat2Arg`: Additional slot status information
    /// - `AddressArg`: The slots corresponding address
    /// - `SpeedArg`: The slots set speed
    /// - `DirfArg`: The direction and low function bits
    /// - `TrkArg`: The general track information
    /// - `SndArg`: Additional function bits
    /// - `IdArg`: The ID of the slots user
    DataGeneral(
        SlotArg,
        Stat1Arg,
        Stat2Arg,
        AddressArg,
        SpeedArg,
        DirfArg,
        TrkArg,
        SndArg,
        IdArg,
    ),
}

impl WrSlDataStructure {
    /// Parses eleven incoming bytes to one write slot data message
    pub(crate) fn parse(
        arg1: u8,
        arg2: u8,
        arg3: u8,
        arg4: u8,
        arg5: u8,
        arg6: u8,
        arg7: u8,
        arg8: u8,
        arg9: u8,
        arg10: u8,
        arg11: u8,
    ) -> Self {
        if arg1 == 0x7C {
            WrSlDataStructure::DataPt(
                Pcmd::parse(arg2),
                AddressArg::parse(arg4, arg5),
                TrkArg::parse(arg6),
                CvDataArg::parse(arg7, arg8, arg9),
            )
        } else if arg1 == 0x7B {
            WrSlDataStructure::DataTime(
                FastClock::parse(arg2, arg3, arg4, arg5, arg7, arg8, arg9),
                TrkArg::parse(arg6),
                IdArg::parse(arg10, arg11),
            )
        } else {
            WrSlDataStructure::DataGeneral(
                SlotArg::parse(arg1),
                Stat1Arg::parse(arg2),
                Stat2Arg::parse(arg7),
                AddressArg::parse(arg8, arg3),
                SpeedArg::parse(arg4),
                DirfArg::parse(arg5),
                TrkArg::parse(arg6),
                SndArg::parse(arg9),
                IdArg::parse(arg10, arg11),
            )
        }
    }

    /// # Returns
    ///
    /// The slot this message is written to
    pub fn slot_type(&self) -> u8 {
        match self {
            WrSlDataStructure::DataPt(..) => 0x7C,
            WrSlDataStructure::DataTime(..) => 0x7B,
            WrSlDataStructure::DataGeneral(slot, ..) => slot.slot(),
        }
    }

    /// # Returns
    ///
    /// This message as a sequence of 13 bytes
    pub(crate) fn to_message(self) -> Vec<u8> {
        match self {
            WrSlDataStructure::DataPt(pcmd, adr, trk, cv_data) => {
                vec![
                    0xEF,
                    0x0E,
                    0x7C,
                    pcmd.pcmd(),
                    0x00,
                    adr.adr2(),
                    adr.adr1(),
                    trk.trk_arg(),
                    cv_data.cvh(),
                    cv_data.cvl(),
                    cv_data.data7(),
                    0x00,
                    0x00,
                ]
            }
            WrSlDataStructure::DataTime(fast_clock, trk, id) => {
                vec![
                    0xEF,
                    0x0E,
                    0x7B,
                    fast_clock.clk_rate(),
                    fast_clock.frac_minsl(),
                    fast_clock.frac_minsh(),
                    fast_clock.mins(),
                    trk.trk_arg(),
                    fast_clock.hours(),
                    fast_clock.days(),
                    fast_clock.clk_cntrl(),
                    id.id1(),
                    id.id2(),
                ]
            }
            WrSlDataStructure::DataGeneral(
                slot,
                stat1,
                stat2,
                adr,
                speed,
                dirf,
                trk,
                sound,
                id,
            ) => {
                vec![
                    0xEF,
                    0x0E,
                    slot.slot(),
                    stat1.stat1(),
                    adr.adr1(),
                    speed.spd(),
                    dirf.dirf(),
                    trk.trk_arg(),
                    stat2.stat2(),
                    adr.adr2(),
                    sound.snd(),
                    id.id1(),
                    id.id2(),
                ]
            }
        }
    }
}

/// Lissy IR reports status information
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct LissyIrReport {
    arg1: u8,
    dir: bool,
    unit: u16,
    address: u16,
}

impl LissyIrReport {
    /// Creates a new report
    ///
    /// # Parameters
    ///
    /// - `dir`: The direction
    /// - `unit`: The reports unit
    /// - `address`: The reports address
    pub fn new(dir: bool, unit: u16, address: u16) -> Self {
        LissyIrReport {
            arg1: 0x00,
            dir,
            unit,
            address,
        }
    }

    /// Parses the report information from five bytes
    ///
    /// # Parameters
    ///
    /// - `arg1`: Specifies the report type
    /// - `high_unit`: The most significant unit bits and the direction
    /// - `low_unit`: The least significant unit bits
    /// - `high_adr`: The most significant address bits
    /// - `low_adr`: The least significant address bits
    pub(crate) fn parse(arg1: u8, high_unit: u8, low_unit: u8, high_adr: u8, low_adr: u8) -> Self {
        let dir = high_unit & 0x40 == 0x40;
        let unit = (((high_unit & 0x3F) as u16) << 7) | (low_unit as u16);
        let address = (((high_adr & 0x7F) as u16) << 7) | (low_adr as u16);

        LissyIrReport {
            arg1,
            dir,
            unit,
            address,
        }
    }

    /// # Returns
    ///
    /// This message represented by a vector of seven bytes
    pub(crate) fn to_message(self) -> Vec<u8> {
        let mut high_unit = ((self.unit >> 7) as u8) & 0x3F;
        if self.dir {
            high_unit |= 0x40;
        }
        let low_unit = self.unit as u8 & 0x7F;
        let high_adr = ((self.address >> 7) as u8) & 0x7F;
        let low_adr = self.address as u8 & 0x7F;
        vec![
            0xE4, 0x08, self.arg1, high_unit, low_unit, high_adr, low_adr,
        ]
    }

    /// # Returns
    ///
    /// The messages type byte
    pub fn arg1(&self) -> u8 {
        self.arg1
    }

    /// # Returns
    ///
    /// The direction
    pub fn dir(&self) -> bool {
        self.dir
    }

    /// # Returns
    ///
    /// The unit of this message
    pub fn unit(&self) -> u16 {
        self.unit
    }

    /// # Returns
    ///
    /// The messages address
    pub fn address(&self) -> u16 {
        self.address
    }
}

/// Holds report information of a rfid5 report message
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct RFID5Report {
    arg1: u8,
    address: u16,
    rfid0: u8,
    rfid1: u8,
    rfid2: u8,
    rfid3: u8,
    rfid4: u8,
    rfid_hi: u8,
}

impl RFID5Report {
    /// Creates new report information
    ///
    /// # Parameters
    ///
    /// - `address`: The reporters address
    /// - `rfid0` - `rfid4` and `rfid_hi`: The reported rfid values
    pub fn new(
        address: u16,
        rfid0: u8,
        rfid1: u8,
        rfid2: u8,
        rfid3: u8,
        rfid4: u8,
        rfid_hi: u8,
    ) -> Self {
        RFID5Report {
            arg1: 0x41,
            address,
            rfid0,
            rfid1,
            rfid2,
            rfid3,
            rfid4,
            rfid_hi,
        }
    }

    /// Parses this message from nine bytes
    ///
    /// # Parameters
    ///
    /// - `arg1`: This reports type byte
    /// - `high_adr`: This most significant address part
    /// - `low_adr`: This least significant address part
    /// - `rfid0` - `rfid4` and `rfid_hi`: The reported rfid values
    pub(crate) fn parse(
        arg1: u8,
        high_adr: u8,
        low_adr: u8,
        rfid0: u8,
        rfid1: u8,
        rfid2: u8,
        rfid3: u8,
        rfid4: u8,
        rfid_hi: u8,
    ) -> Self {
        let address = (((high_adr & 0x7F) as u16) << 7) | (low_adr as u16);
        RFID5Report {
            arg1,
            address,
            rfid0,
            rfid1,
            rfid2,
            rfid3,
            rfid4,
            rfid_hi,
        }
    }

    /// # Returns
    ///
    /// This message parsed represented by 11 bytes
    pub(crate) fn to_message(self) -> Vec<u8> {
        let high_adr = ((self.address >> 7) as u8) & 0x7F;
        let low_adr = (self.address as u8) & 0x7F;
        vec![
            0xE4,
            0x0C,
            self.arg1,
            high_adr,
            low_adr,
            self.rfid0,
            self.rfid1,
            self.rfid2,
            self.rfid3,
            self.rfid4,
            self.rfid_hi,
        ]
    }

    /// # Returns
    ///
    /// The messages type byte
    pub fn arg1(&self) -> u8 {
        self.arg1
    }

    /// # Returns
    ///
    /// The reporters address
    pub fn address(&self) -> u16 {
        self.address
    }

    /// # Returns
    ///
    /// The first reported rfid byte
    pub fn rfid0(&self) -> u8 {
        self.rfid0
    }

    /// # Returns
    ///
    /// The second reported rfid byte
    pub fn rfid1(&self) -> u8 {
        self.rfid1
    }

    /// # Returns
    ///
    /// The third reported rfid byte
    pub fn rfid2(&self) -> u8 {
        self.rfid2
    }

    /// # Returns
    ///
    /// The fourth reported rfid byte
    pub fn rfid3(&self) -> u8 {
        self.rfid3
    }

    /// # Returns
    ///
    /// The fifth reported rfid byte
    pub fn rfid4(&self) -> u8 {
        self.rfid4
    }

    /// # Returns
    ///
    /// The last reported rfid byte
    pub fn rfid_hi(&self) -> u8 {
        self.rfid_hi
    }
}

/// Holds report information of a rfid7 report message
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct RFID7Report {
    arg1: u8,
    address: u16,
    rfid0: u8,
    rfid1: u8,
    rfid2: u8,
    rfid3: u8,
    rfid4: u8,
    rfid5: u8,
    rfid6: u8,
    rfid_hi: u8,
}

impl RFID7Report {
    /// Creates new report information
    ///
    /// # Parameters
    ///
    /// - `address`: The reporters address
    /// - `rfid0` - `rfid6` and `rfid_hi`: The reported rfid values
    pub fn new(
        address: u16,
        rfid0: u8,
        rfid1: u8,
        rfid2: u8,
        rfid3: u8,
        rfid4: u8,
        rfid5: u8,
        rfid6: u8,
        rfid_hi: u8,
    ) -> Self {
        RFID7Report {
            arg1: 0x41,
            address,
            rfid0,
            rfid1,
            rfid2,
            rfid3,
            rfid4,
            rfid5,
            rfid6,
            rfid_hi,
        }
    }

    /// Parses this message from eleven bytes
    ///
    /// # Parameters
    ///
    /// - `arg1`: This reports type byte
    /// - `high_adr`: This most significant address part
    /// - `low_adr`: This least significant address part
    /// - `rfid0` - `rfid6` and `rfid_hi`: The reported rfid values
    pub(crate) fn parse(
        arg1: u8,
        high_adr: u8,
        low_adr: u8,
        rfid0: u8,
        rfid1: u8,
        rfid2: u8,
        rfid3: u8,
        rfid4: u8,
        rfid5: u8,
        rfid6: u8,
        rfid_hi: u8,
    ) -> Self {
        let address = (((high_adr & 0x7F) as u16) << 7) | (low_adr as u16);
        RFID7Report {
            arg1,
            address,
            rfid0,
            rfid1,
            rfid2,
            rfid3,
            rfid4,
            rfid5,
            rfid6,
            rfid_hi,
        }
    }

    /// # Returns
    ///
    /// This message represented by 13 bytes
    pub(crate) fn to_message(self) -> Vec<u8> {
        let high_adr = ((self.address >> 7) as u8) & 0x7F;
        let low_adr = (self.address as u8) & 0x7F;
        vec![
            0xE4,
            0x0E,
            self.arg1,
            high_adr,
            low_adr,
            self.rfid0,
            self.rfid1,
            self.rfid2,
            self.rfid3,
            self.rfid4,
            self.rfid5,
            self.rfid6,
            self.rfid_hi,
        ]
    }

    /// # Returns
    ///
    /// The messages type byte
    pub fn arg1(&self) -> u8 {
        self.arg1
    }

    /// # Returns
    ///
    /// The reporters address
    pub fn address(&self) -> u16 {
        self.address
    }

    /// # Returns
    ///
    /// The first reported rfid byte
    pub fn rfid0(&self) -> u8 {
        self.rfid0
    }

    /// # Returns
    ///
    /// The second reported rfid byte
    pub fn rfid1(&self) -> u8 {
        self.rfid1
    }

    /// # Returns
    ///
    /// The third reported rfid byte
    pub fn rfid2(&self) -> u8 {
        self.rfid2
    }

    /// # Returns
    ///
    /// The fourth reported rfid byte
    pub fn rfid3(&self) -> u8 {
        self.rfid3
    }

    /// # Returns
    ///
    /// The fifth reported rfid byte
    pub fn rfid4(&self) -> u8 {
        self.rfid4
    }

    /// # Returns
    ///
    /// The sixth reported rfid byte
    pub fn rfid5(&self) -> u8 {
        self.rfid5
    }

    /// # Returns
    ///
    /// The seventh reported rfid byte
    pub fn rfid6(&self) -> u8 {
        self.rfid6
    }

    /// # Returns
    ///
    /// The last reported rfid byte
    pub fn rfid_hi(&self) -> u8 {
        self.rfid_hi
    }
}

/// Holds wheel counter report information
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct WheelcntReport {
    arg1: u8,
    unit: u16,
    direction: bool,
    count: u16,
}

impl WheelcntReport {
    /// Creates new wheel counter report information
    ///
    /// # Parameters
    ///
    /// - `unit`: The reports unit
    /// - `direction`: The reports direction
    /// - `count`: The reports wheel count
    pub fn new(unit: u16, direction: bool, count: u16) -> Self {
        WheelcntReport {
            arg1: 0x40,
            unit,
            direction,
            count,
        }
    }

    /// Parses the wheel count information from five bytes
    ///
    /// # Parameters
    ///
    /// - `arg1`: The reports type byte
    /// - `high_unit`: The most significant unit bits and the direction
    /// - `low_unit`: The least significant unit bits
    /// - `high_count`: The most significant count bits
    /// - `low_count`: The least significant count bits
    pub(crate) fn parse(
        arg1: u8,
        high_unit: u8,
        low_unit: u8,
        high_count: u8,
        low_count: u8,
    ) -> Self {
        let count = ((high_count as u16) << 7) | (low_count as u16);
        let direction = high_unit & 0x40 == 0x40;
        let unit = (((high_unit & 0x3F) as u16) << 7) | (low_unit as u16);
        WheelcntReport {
            arg1,
            unit,
            direction,
            count,
        }
    }

    /// # Returns
    ///
    /// This message represented by seven bytes
    pub(crate) fn to_message(self) -> Vec<u8> {
        let mut high_unit = ((self.unit >> 7) as u8) & 0x3F;
        if self.direction {
            high_unit |= 0x40;
        }
        let low_unit = self.unit as u8 & 0x7F;
        let high_count = ((self.count >> 7) as u8) & 0x7F;
        let low_count = self.count as u8 & 0x7F;
        vec![
            0xE4, 0x08, self.arg1, high_unit, low_unit, high_count, low_count,
        ]
    }

    /// # Returns
    ///
    /// This reports type byte
    pub fn arg1(&self) -> u8 {
        self.arg1
    }

    /// # Returns
    ///
    /// The unit of this report
    pub fn unit(&self) -> u16 {
        self.unit
    }

    /// # Returns
    ///
    /// The count hold by this message
    pub fn count(&self) -> u16 {
        self.count
    }

    /// # Returns
    ///
    /// This messages direction
    pub fn direction(&self) -> bool {
        self.direction
    }
}

/// Represents a report message
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum RepStructure {
    /// A Lissy IR report
    LissyIrReport(LissyIrReport),
    /// A rfid5 report
    RFID5Report(RFID5Report),
    /// A rfid7 report
    RFID7Report(RFID7Report),
    /// A wheel count report
    WheelcntReport(WheelcntReport),
}

impl RepStructure {
    /// Parses a report message from the given bytes
    ///
    /// # Parameters
    ///
    /// - `count`: The messages length
    /// - `args`: The messages arguments to parse
    pub(crate) fn parse(count: u8, args: &[u8]) -> Result<Self, MessageParseError> {
        if args[0] == 0x00 {
            if count != 0x08 {
                Err(MessageParseError::UnexpectedEnd(0xE4))
            } else {
                Ok(Self::LissyIrReport(LissyIrReport::parse(
                    args[0], args[1], args[2], args[3], args[4],
                )))
            }
        } else if args[0] == 0x40 {
            if count != 0x08 {
                Err(MessageParseError::UnexpectedEnd(0xE4))
            } else {
                Ok(Self::WheelcntReport(WheelcntReport::parse(
                    args[0], args[1], args[2], args[3], args[4],
                )))
            }
        } else if args[0] == 0x41 && count == 0x0C {
            Ok(Self::RFID5Report(RFID5Report::parse(
                args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8],
            )))
        } else if args[0] == 0x41 && count == 0x0E {
            Ok(Self::RFID7Report(RFID7Report::parse(
                args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8],
                args[9], args[10],
            )))
        } else {
            Err(MessageParseError::InvalidFormat(
                "The report message (opcode: 0xE4) was in invalid format!".into(),
            ))
        }
    }
}

/// The destination slot to move data to
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct DstArg(u16);

impl DstArg {
    /// Creates a new destination slot
    ///
    /// # Parameters
    ///
    /// - `dst`: The destination
    pub fn new(dst: u16) -> Self {
        DstArg(dst)
    }

    /// Parses the destination from two bytes
    ///
    /// # Parameters
    ///
    /// - `dst_low`: The seven least significant destination address bytes
    /// - `dst_high`: The seven most significant destination address bytes
    pub(crate) fn parse(dst_low: u8, dst_high: u8) -> Self {
        let dst = ((dst_high as u16) << 7) | (dst_low as u16);
        DstArg(dst)
    }

    /// # Returns
    ///
    /// The destination address of the slot move
    pub fn dst(&self) -> u16 {
        self.0
    }

    /// # Returns
    ///
    /// The seven least significant destination address bits
    pub(crate) fn dst_low(&self) -> u8 {
        self.0 as u8 & 0x7F
    }

    /// # Returns
    ///
    /// The seven most significant destination address bits
    pub(crate) fn dst_high(&self) -> u8 {
        (self.0 >> 7) as u8 & 0x7F
    }
}

/// Holds eight movable bytes and peer data
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct PxctData {
    pxc: u8,
    d1: u8,
    d2: u8,
    d3: u8,
    d4: u8,
    d5: u8,
    d6: u8,
    d7: u8,
    d8: u8,
}

impl PxctData {
    /// Creates new peer data
    ///
    /// # Parameters
    ///
    /// - `pxc`: The peer data
    /// - `d1` - `d8`: The data
    pub fn new(pxc: u8, d1: u8, d2: u8, d3: u8, d4: u8, d5: u8, d6: u8, d7: u8, d8: u8) -> Self {
        PxctData {
            pxc,
            d1,
            d2,
            d3,
            d4,
            d5,
            d6,
            d7,
            d8,
        }
    }

    /// Parses the data from 10 bytes
    ///
    /// # Parameters
    ///
    /// - `pxct1`, `pxct2`: The peer data
    /// - `d1` - `d8`: The data
    pub(crate) fn parse(
        pxct1: u8,
        d1: u8,
        d2: u8,
        d3: u8,
        d4: u8,
        pxct2: u8,
        d5: u8,
        d6: u8,
        d7: u8,
        d8: u8,
    ) -> Self {
        let pxc = ((pxct1 & 0x70) >> 4) | ((pxct2 & 0x70) >> 1);

        PxctData {
            pxc,
            d1: d1 | ((pxct1 & 0x01) << 6),
            d2: d2 | ((pxct1 & 0x02) << 5),
            d3: d3 | ((pxct1 & 0x04) << 4),
            d4: d4 | ((pxct1 & 0x08) << 3),
            d5: d5 | ((pxct2 & 0x01) << 6),
            d6: d6 | ((pxct2 & 0x02) << 5),
            d7: d7 | ((pxct2 & 0x04) << 4),
            d8: d8 | ((pxct2 & 0x08) << 3),
        }
    }

    /// # Returns
    ///
    /// The peer data
    pub fn pxc(&self) -> u8 {
        self.pxc
    }

    /// # Returns
    ///
    /// The low part of the peer data and one data bit of the first four the data bits
    pub(crate) fn pxct1(&self) -> u8 {
        let mut pxct1 = (self.pxc & 0x07) << 4;

        if self.d1 & 0x40 == 0x40 {
            pxct1 |= 0x01;
        }
        if self.d2 & 0x40 == 0x40 {
            pxct1 |= 0x02;
        }
        if self.d3 & 0x40 == 0x40 {
            pxct1 |= 0x04;
        }
        if self.d4 & 0x40 == 0x40 {
            pxct1 |= 0x08;
        }

        pxct1
    }

    /// # Returns
    ///
    /// The high part of the peer data and one data bit of the last four the data bits
    pub(crate) fn pxct2(&self) -> u8 {
        let mut pxct2 = (self.pxc & 0x78) << 1;

        if self.d5 & 0x40 == 0x40 {
            pxct2 |= 0x01;
        }
        if self.d6 & 0x40 == 0x40 {
            pxct2 |= 0x02;
        }
        if self.d7 & 0x40 == 0x40 {
            pxct2 |= 0x04;
        }
        if self.d8 & 0x40 == 0x40 {
            pxct2 |= 0x08;
        }

        pxct2
    }

    /// # Returns
    ///
    /// The first data byte to move
    pub fn d1(&self) -> u8 {
        self.d1 & 0x3F
    }

    /// # Returns
    ///
    /// The second data byte to move
    pub fn d2(&self) -> u8 {
        self.d2 & 0x3F
    }

    /// # Returns
    ///
    /// The third data byte to move
    pub fn d3(&self) -> u8 {
        self.d3 & 0x3F
    }

    /// # Returns
    ///
    /// The fourth data byte to move
    pub fn d4(&self) -> u8 {
        self.d4 & 0x3F
    }

    /// # Returns
    ///
    /// The fifth data byte to move
    pub fn d5(&self) -> u8 {
        self.d5 & 0x3F
    }

    /// # Returns
    ///
    /// The sixth data byte to move
    pub fn d6(&self) -> u8 {
        self.d6 & 0x3F
    }

    /// # Returns
    ///
    /// The seventh data byte to move
    pub fn d7(&self) -> u8 {
        self.d7 & 0x3F
    }

    /// # Returns
    ///
    /// The eighth data byte to move
    pub fn d8(&self) -> u8 {
        self.d8 & 0x3F
    }
}

/// Send when service mode is aborted
///
/// As I do not now how this message is structured this message bytes is for now open to use.
/// Please feel free to contribute to provide a more powerful version of this arg
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub struct ProgrammingAbortedArg {
    /// The count of args to write to the message 0x10 or 0x15
    pub arg_len: u8,
    /// The first argument
    pub arg01: u8,
    /// The second argument
    pub arg02: u8,
    /// The third argument
    pub arg03: u8,
    /// The fourth argument
    pub arg04: u8,
    /// The fifth argument
    pub arg05: u8,
    /// The sixth argument
    pub arg06: u8,
    /// The seventh argument
    pub arg07: u8,
    /// The eighth argument
    pub arg08: u8,
    /// The ninth argument
    pub arg09: u8,
    /// The tenth argument
    pub arg10: u8,
    /// The eleventh argument
    pub arg11: u8,
    /// The twelfth argument
    pub arg12: u8,
    /// The thirteenth argument
    pub arg13: u8,
    /// The fourteenth argument
    pub arg14: u8,
    /// The fifteenth argument
    pub arg15: u8,
    /// The sixteenth argument
    pub arg16: u8,
    /// The seventeenth argument
    pub arg17: u8,
    /// The eighteenth argument
    pub arg18: u8,
}

impl ProgrammingAbortedArg {
    /// Creates a new service mode aborted message.
    ///
    /// # Parameters
    ///
    /// - `len`: The messages length (0x10 or 0x15)
    /// - `args`: The argument values. 0x10 = 0 - 12 filled, 0x15 = 0 - 17 filled
    pub fn new(len: u8, args: &[u8]) -> Self {
        ProgrammingAbortedArg::parse(len, args)
    }

    /// Parses a new service mode aborted message.
    ///
    /// # Parameters
    ///
    /// - `len`: The messages length (0x10 or 0x15)
    /// - `args`: The argument values. 0x10 = 0 - 12 filled, 0x15 = 0 - 17 filled
    pub(crate) fn parse(len: u8, args: &[u8]) -> Self {
        match len {
            0x10 => ProgrammingAbortedArg {
                arg_len: len,
                arg01: args[0],
                arg02: args[1],
                arg03: args[2],
                arg04: args[3],
                arg05: args[4],
                arg06: args[5],
                arg07: args[6],
                arg08: args[7],
                arg09: args[8],
                arg10: args[9],
                arg11: args[10],
                arg12: args[11],
                arg13: args[12],
                arg14: 0,
                arg15: 0,
                arg16: 0,
                arg17: 0,
                arg18: 0,
            },

            0x15 => ProgrammingAbortedArg {
                arg_len: len,
                arg01: args[0],
                arg02: args[1],
                arg03: args[2],
                arg04: args[3],
                arg05: args[4],
                arg06: args[5],
                arg07: args[6],
                arg08: args[7],
                arg09: args[8],
                arg10: args[9],
                arg11: args[10],
                arg12: args[11],
                arg13: args[12],
                arg14: args[13],
                arg15: args[14],
                arg16: args[15],
                arg17: args[16],
                arg18: args[17],
            },
            _ => ProgrammingAbortedArg {
                arg_len: len,
                arg01: *args.first().unwrap_or(&0u8),
                arg02: *args.get(1).unwrap_or(&0u8),
                arg03: *args.get(2).unwrap_or(&0u8),
                arg04: *args.get(3).unwrap_or(&0u8),
                arg05: *args.get(4).unwrap_or(&0u8),
                arg06: *args.get(5).unwrap_or(&0u8),
                arg07: *args.get(6).unwrap_or(&0u8),
                arg08: *args.get(7).unwrap_or(&0u8),
                arg09: *args.get(8).unwrap_or(&0u8),
                arg10: *args.get(9).unwrap_or(&0u8),
                arg11: *args.get(10).unwrap_or(&0u8),
                arg12: *args.get(11).unwrap_or(&0u8),
                arg13: *args.get(12).unwrap_or(&0u8),
                arg14: *args.get(13).unwrap_or(&0u8),
                arg15: *args.get(14).unwrap_or(&0u8),
                arg16: *args.get(15).unwrap_or(&0u8),
                arg17: *args.get(16).unwrap_or(&0u8),
                arg18: *args.get(17).unwrap_or(&0u8),
            },
        }
    }

    /// # Returns
    ///
    /// This message as a count of bytes
    pub(crate) fn to_message(self) -> Vec<u8> {
        match self.arg_len {
            0x10 => vec![
                0xE6, 0x10, self.arg01, self.arg02, self.arg03, self.arg04, self.arg05, self.arg06,
                self.arg07, self.arg08, self.arg09, self.arg10, self.arg11, self.arg12, self.arg13,
            ],
            _ => vec![
                0xE6, 0x15, self.arg01, self.arg02, self.arg03, self.arg04, self.arg05, self.arg06,
                self.arg07, self.arg08, self.arg09, self.arg10, self.arg11, self.arg12, self.arg13,
                self.arg14, self.arg15, self.arg16, self.arg17, self.arg18,
            ],
        }
    }
}