aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/ViewModels/JobViewVM.cs
blob: 2ffa84992b4db9d7c15f8cf8dbabeb6b3b707fec (plain)
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.DI;
using Tango.PPC.Power.ViewModels;

namespace Tango.PPC.Power
{
    public static class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        static ViewModelLocator()
        {
            TangoIOC.Default.Register<MainViewVM>();
        }

        /// <summary>
        /// Gets the main view VM.
        /// </summary>
        public static MainViewVM MainViewVM
        {
            get
            {
                return TangoIOC.Default.GetInstance<MainViewVM>();
            }
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.PPC.Common;
using Tango.PPC.Jobs.Messages;
using System.Data.Entity;
using Tango.Core.Commands;
using System.Windows;
using Tango.Touch.Controls;
using System.Windows.Media;
using Tango.DragAndDrop;
using System.ComponentModel;
using System.Windows.Data;
using Tango.PPC.Jobs.Dialogs;
using Tango.PPC.Jobs.Views;
using System.Runtime.InteropServices;
using System.Threading;
using Tango.SharedUI.Helpers;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.ViewContracts;
using System.Collections.ObjectModel;
using Tango.PPC.Common.Models;
using Tango.Logging;
using Tango.PPC.Common.Messages;
using Tango.BL.Builders;
using Tango.PPC.Jobs.AppButtons;
using Tango.Core.Threading;
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using Tango.Explorer;
using Tango.PPC.Storage;
using System.IO;
using Tango.ColorConversion;
using Tango.Integration.Operation;
using Tango.BL.Enumerations;
using Tango.PPC.Common.Lubrication;
using Tango.PPC.Jobs.Models;
using Tango.Core;
using Tango.PPC.Jobs.UndoRedoCommands;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Tango.PPC.Jobs.ColorCorrectionTool;
using Tango.PPC.Common.Printing;
using Tango.Core.ExtensionMethods;
using Tango.PPC.Common.Resume;

namespace Tango.PPC.Jobs.ViewModels
{
    /// <summary>
    /// Represents the selected job view model.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.PPCViewModel" />
    public class JobViewVM : PPCViewModel<IJobView>, INavigationObjectReceiver<JobNavigationObject>, INavigationBlocker
    {
        private ObservablesContext _db;
        private bool _can_navigate_back;
        private bool _not_show_warning;
        //private Thread _check_gamut_thread;
        private Job _job_to_load;
        private JobNavigationIntent _job_to_load_intent;
        private static Dictionary<String, List<FineTuneItem>> _jobs_fine_tune_items;
        public static StartPrintingButton _start_printing_btn;
        private ActionTimer _volumeConversionTimer;
        private IColorConverter _converter;
        private string _current_job_string;
        private bool startingJob = false;
        private List<ColorCatalog> _catalogs;
        private string _jsonJobModelLoaded;

        #region Properties

        private Job _job;
        /// <summary>
        /// Gets or sets the selected job.
        /// </summary>
        public Job Job
        {
            get { return _job; }
            set
            {
                _job = value;
                RaisePropertyChangedAuto();
            }
        }

        private JobModel _jobModel;
        public JobModel JobModel
        {
            get { return _jobModel; }
            set
            {
                _jobModel = value;
                RaisePropertyChangedAuto();
            }
        }

        public bool CanEdit
        {
            get {
                return Job != null && 
                    (!MachineProvider.MachineOperator.IsPrinting 
                        || MachineProvider.MachineOperator.RunningJob == null 
                        || MachineProvider.MachineOperator.RunningJob.Guid != Job.Guid) 
                    && !HasResumeModel;
            }
        }

        public bool CanDropResume
        {
            get
            {
                return Job != null && HasResumeModel &&
                    (!MachineProvider.MachineOperator.IsPrinting
                      ||  (MachineProvider.MachineOperator.RunningJob != null
                        && MachineProvider.MachineOperator.RunningJob.Guid != Job.Guid));
            }
        }

        private ICollectionView _segmentsCollectionView;
        /// <summary>
        /// Gets or sets the job segments collection view.
        /// </summary>
        public ICollectionView SegmentsCollectionView
        {
            get { return _segmentsCollectionView; }
            set
            {
                _segmentsCollectionView = value;
                RaisePropertyChangedAuto();
            }
        }

        private List<ColorSpace> _colorSpaces;
        /// <summary>
        /// Gets or sets the available color spaces.
        /// </summary>
        public List<ColorSpace> ColorSpaces
        {
            get { return _colorSpaces; }
            set { _colorSpaces = value; RaisePropertyChangedAuto(); }
        }

        private List<Rml> _rmls;
        /// <summary>
        /// Gets or sets the available RMLS.
        /// </summary>
        public List<Rml> Rmls
        {
            get { return _rmls; }
            set { _rmls = value; RaisePropertyChangedAuto(); }
        }

        private Rml _selectedRML;
        /// <summary>
        /// Gets or sets the selected RML.
        /// </summary>
        public Rml SelectedRML
        {
            get { return _selectedRML; }
            set { _selectedRML = value; RaisePropertyChangedAuto(); OnSelectedRmlChanged(); }
        }

        private List<SpoolType> _spoolTypes;
        /// <summary>
        /// Gets or sets the available spool types.
        /// </summary>
        public List<SpoolType> SpoolTypes
        {
            get { return _spoolTypes; }
            set { _spoolTypes = value; RaisePropertyChangedAuto(); }
        }

        private List<Customer> _customers;
        /// <summary>
        /// Gets or sets the available customers.
        /// </summary>
        public List<Customer> Customers
        {
            get { return _customers; }
            set { _customers = value; RaisePropertyChangedAuto(); }
        }

        private String _customersFilter;
        /// <summary>
        /// Gets or sets the customers filter.
        /// </summary>
        public String CustomersFilter
        {
            get { return _customersFilter; }
            set { _customersFilter = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets the customers automatic complete provider.
        /// </summary>
        public AutoCompleteProvider<Customer> CustomersAutoCompleteProvider { get; set; }

        private ObservableCollection<FineTuneItem> _fineTuneItems;
        /// <summary>
        /// Gets or sets the fine tune items.
        /// </summary>
        public ObservableCollection<FineTuneItem> FineTuneItems
        {
            get { return _fineTuneItems; }
            set { _fineTuneItems = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<FineTuneItem> _approvalFineTuneItems;
        /// <summary>
        /// Gets or sets the fine tune items.
        /// </summary>
        public ObservableCollection<FineTuneItem> ApprovalFineTuneItems
        {
            get { return _approvalFineTuneItems; }
            set { _approvalFineTuneItems = value; RaisePropertyChangedAuto(); }
        }

        private List<ColorCatalog> _availableCatalogs;
        public List<ColorCatalog> AvailableCatalogs
        {
            get { return _availableCatalogs; }
            set { _availableCatalogs = value; RaisePropertyChangedAuto(); }
        }



        private bool _isFullMode;

        public bool IsFullMode
        {
            get { return _isFullMode; }
            set
            {
                _isFullMode = value;
                RaisePropertyChangedAuto();
            }
        }

        private bool _isBasicMode;

        public bool IsBasicMode
        {
            get { return _isBasicMode; }
            set
            {
                if (_isBasicMode != value)
                {
                    if (value == true && JobModel != null && JobModel.Segments.Count > 0)
                    {
                        var firstSegment = JobModel.Segments.Where(x => x.SegmentIndex == 1).FirstOrDefault();
                        if (JobModel.Segments.Count > 1 || firstSegment.IsGradient)
                        {
                            SetOrDiscardAll();
                            return;
                        }
                        JobModel.EnableInterSegment = false;
                    }
                    _isBasicMode = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        private bool _isSummaryOpened;

        public bool IsSummaryOpened
        {
            get { return _isSummaryOpened; }
            set
            {
                _isSummaryOpened = value;
                RaisePropertyChangedAuto();
            }
        }
        private bool _isWeigthView;

        public bool IsWeightView
        {
            get { return _isWeigthView; }
            set
            {
                if (_isWeigthView != value)
                {
                    _isWeigthView = value;

                    RaisePropertyChangedAuto();
                }
            }
        }
        
        private JobResumeModel _resumeModel;
        public JobResumeModel ResumeModel
        {
            get { return _resumeModel; }
            set { _resumeModel = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasResumeModel)); RaisePropertyChanged(nameof(CanEdit)); }
        }

        public bool HasResumeModel
        {
            get { return ResumeModel != null; }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the add solid segment command.
        /// </summary>
        public RelayCommand<ISegmentModel> AddNewSegmentCommand { get; set; }

        /// <summary>
        /// Gets or sets the remove segment command.
        /// </summary>
        public RelayCommand<SegmentModel> RemoveSegmentCommand { get; set; }

        /// <summary>
        /// Gets or sets the copy segment command.
        /// </summary>
        public RelayCommand<SegmentModel> DuplicateSegmentCommand { get; set; }

        /// <summary>
        /// Gets or sets the remove job command.
        /// </summary>
        public RelayCommand<SegmentModel> AddColorCommand { get; set; }

        /// <summary>
        /// Gets or sets the remove job command.
        /// </summary>
        public RelayCommand<BrushStopModel> EditColorCommand { get; set; }

        /// <summary>
        /// Gets or sets the replace brush stop command.
        /// </summary>
        public RelayCommand<BrushStop> ReplaceBrushStopCommand { get; set; }

        /// <summary>
        /// Gets or sets the dye command.
        /// </summary>
        public RelayCommand DyeCommand { get; set; }

        /// <summary>
        /// Gets or sets the export embroidery command.
        /// </summary>
        public RelayCommand ExportEmbroideryCommand { get; set; }

        public RelayCommand EditJobDetailsCommand { get; set; }

        public RelayCommand RepeatUnitsCommand { get; set; }

        public RelayCommand JobModeSwitchCommand { get; set; }

        public RelayCommand<SegmentsGroupModel> UngroupSegmentsCommand { get; set; }
        public RelayCommand<SegmentsGroupModel> DeleteSegmentsGroupCommand { get; set; }
        public RelayCommand<SegmentsGroupModel> RepeatSegmentsGroupCommand { get; set; }

        public RelayCommand NavigateBackToJobs { get; set; }

        public RelayCommand DropResumeCommand { get; set; }

        #endregion

        #region collapsed mode commands

        public RelayCommand InsertWhiteGapCommand { get; set; }
        public RelayCommand ReverseCommand { get; set; }
        public RelayCommand DeleteSegmentCommand { get; set; }
        public RelayCommand RepeateSegmentCommand { get; set; }
        public RelayCommand PasteCommand { get; set; }
        public RelayCommand CopyCommand { get; set; }
        public RelayCommand UndoCommand { get; set; }
        public RelayCommand RedoCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes the <see cref="JobViewVM"/> class.
        /// </summary>
        static JobViewVM()
        {

            _jobs_fine_tune_items = new Dictionary<string, List<FineTuneItem>>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="JobViewVM"/> class.
        /// </summary>
        public JobViewVM()
        {
            _converter = new DefaultColorConverter();

            _volumeConversionTimer = new ActionTimer(TimeSpan.FromMilliseconds(50));

            RegisterForMessage<JobSelectedMessage>(HandleJobSelectedMessage);

            FineTuneItems = new ObservableCollection<FineTuneItem>();
            ApprovalFineTuneItems = new ObservableCollection<FineTuneItem>();

            CustomersAutoCompleteProvider = new AutoCompleteProvider<Customer>((customer, filter) =>
            {
                return customer.Name.ToLower().StartsWith(filter != null ? filter.ToLower() : String.Empty);
            });


            //Initialize Commands
            AddNewSegmentCommand = new RelayCommand<ISegmentModel>(AddNewSegment);
            RemoveSegmentCommand = new RelayCommand<SegmentModel>(RemoveSegment);
            DuplicateSegmentCommand = new RelayCommand<SegmentModel>(DuplicateSegment);

            AddColorCommand = new RelayCommand<SegmentModel>(AddColor);
            EditColorCommand = new RelayCommand<BrushStopModel>(EditColor);

            DyeCommand = new RelayCommand(StartJob, CanStartJob);

            ExportEmbroideryCommand = new RelayCommand(ExportEmbroidery);
            RepeatUnitsCommand = new RelayCommand(RepeatUnits);

            EditJobDetailsCommand = new RelayCommand(EditJobDetails);
            JobModeSwitchCommand = new RelayCommand(JobModeSwitch);
            InsertWhiteGapCommand = new RelayCommand(InsertWhiteGap);
            ReverseCommand = new RelayCommand(Reverse);
            DeleteSegmentCommand = new RelayCommand(DeleteSegments);
            DeleteSegmentsGroupCommand = new RelayCommand<SegmentsGroupModel>(DeleteSegmentsGroup);
            RepeateSegmentCommand = new RelayCommand(RepeateSegments);
            UngroupSegmentsCommand = new RelayCommand<SegmentsGroupModel>(UngroupSegments);
            RepeatSegmentsGroupCommand = new RelayCommand<SegmentsGroupModel>(RepeatSegmentsGroup);
            PasteCommand = new RelayCommand(Paste);
            CopyCommand = new RelayCommand(Copy);
            UndoCommand = new RelayCommand(Undo);//(x) => { return UndoRedoManager.Instance.IsEnableUndoOperation(); }
            RedoCommand = new RelayCommand(Redo);//(x) => { return UndoRedoManager.Instance.IsEnableRedoOperation();}
            DropResumeCommand = new RelayCommand(DropResume, (x)=> CanDropResume);


            NavigateBackToJobs = new RelayCommand(NavigateBack);
            IsFullMode = true;
            IsSummaryOpened = true;
            _not_show_warning = false;
        }

        #endregion

        #region Job Management

        /// <summary>
        /// Loads the job.
        /// </summary>
        private async void LoadJob()
        {
            try
            {
                if (!(_job_to_load == null || (_job_to_load != null && Job != null && _job_to_load.Guid == Job.Guid)))
                {
                    //View.ScrollToTop();

                    LogManager.Log($"Loading selected job '{_job_to_load.Name}'...");
                    IsFree = false;

                    _can_navigate_back = false;

                    if (_db != null)
                    {
                        if (Job != null)
                        {
                            //Job.RmlChanged -= OnRmlChanged;
                            Job.NameChanged -= Job_NameChanged;
                        }

                        if (Rmls != null)
                        {
                            Rmls.Where(x => x.Cct != null && x.Cct.Data != null).ToList().ForEach(x => x.Cct.Data = null);
                            Rmls.ForEach(x => x.Cct = null);

                            if (SelectedRML != null)
                            {
                                SelectedRML.Cct = null;
                                SelectedRML = null;
                            }

                            Rmls = null;
                        }

                        _db.Dispose();
                        GC.Collect();
                    }

                    _db = ObservablesContext.CreateDefault();

                    _catalogs = await new CatalogsCollectionBuilder(_db)
                        .SetAll()
                        .WithGroups()
                        .WithItems()
                        .ForSite(MachineProvider.Machine.SiteGuid)
                        .BuildListAsync();

                    Job = await new JobBuilder(_db).Set(_job_to_load.Guid)
                        .WithConfiguration()
                        .WithUser()
                        .WithSegments()
                        .WithBrushStops()
                        .WithSegmentsGroups()
                        .BuildAsync();

                    try
                    {
                        ResumeModel = JobResumeManager.GetJobResumeModel(Job.Guid);

                        if (ResumeModel != null)
                        {
                            LogManager.Log($"Job resume info found:\n{ResumeModel.ToJsonString()}");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error retrieving job resume info from db.");
                    }

                    RaisePropertyChanged(nameof(CanEdit));
                     DropResumeCommand.RaiseCanExecuteChanged();

                    Job.NameChanged -= Job_NameChanged;
                    Job.NameChanged += Job_NameChanged;

                    Job.ValidateOnPropertyChanged = true;

                    //GetLubricationLevel();
                    //await SetSpoolTension(Job.Rml);

                    LogManager.Log("Loading RMLS...");
                    Rmls = (await new RmlsCollectionBuilder(_db).SetAll().WithSpools().ForHeadType(MachineProvider.Machine.MachineHeadType).ForSite(MachineProvider.Machine.SiteGuid).BuildAsync()).OrderBy(x => x.FinalName).ToList();
                    //Rmls = (await new RmlsCollectionBuilder(_db).SetAll().WithSpools().BuildAsync()).OrderBy(x => x.FinalName).ToList();
                    LogManager.Log("Loading Color Spaces...");
                    ColorSpaces = await _db.ColorSpaces.Where(x => x.Code != (int)BL.Enumerations.ColorSpaces.CMYK).ToListAsync();
                    LogManager.Log("Loading Spool Types...");
                    SpoolTypes = await _db.SpoolTypes.ToListAsync();
                    LogManager.Log("Loading Customers...");
                    Customers = await _db.Customers.Where(x => x.OrganizationGuid == MachineProvider.Machine.OrganizationGuid).ToListAsync();

                    AvailableCatalogs = await new CatalogsCollectionBuilder(_db).SetAll().WithGroups().WithItems().ForSite(MachineProvider.Machine.SiteGuid).BuildListAsync();

                    _selectedRML = Job.Rml;
                    RaisePropertyChanged(nameof(SelectedRML));

                    await LoadRML(_selectedRML);
                    if (BuildProvider.IsEureka && Job.Segments.Count == 1 && Job.Segments[0].BrushStops.Count <= 1)
                    {
                        IsBasicMode = true;
                    }
                    else IsBasicMode = false;
                    IsWeightView = false;

                    LoadJobModel();

                    _job_to_load = null;

                    _current_job_string = Job.ToJobFileWhenLoaded().ToString();
                }

                if (!_jobs_fine_tune_items.ContainsKey(Job.Guid) && Job.JobFineTuningStatus == BL.Enumerations.FineTuningStatuses.PendingApproval)
                {
                    Job.JobFineTuningStatus = BL.Enumerations.FineTuningStatuses.Unspecified;
                }

                LogManager.Log($"Job editing state = '{Job.JobEditingState}'.");

                //if (Job.JobEditingState == BL.Enumerations.EditingStates.SampleDye && Job.JobSampleDyeStatus == BL.Enumerations.SampleDyeStatuses.PendingApproval)
                //{
                //    LogManager.Log("Directing view to display sample dye region.");
                //    View.DisplaySampleDye();
                //}
                //else if (Job.JobEditingState == BL.Enumerations.EditingStates.FineTuning && Job.JobFineTuningStatus == BL.Enumerations.FineTuningStatuses.PendingApproval)
                //{
                //    LogManager.Log("Directing view to display fine tuning region.");
                //    View.DisplayFineTuning();
                //}

                IsFullMode = true;
                DyeCommand.RaiseCanExecuteChanged();
            }
            catch (Exception ex)
            {
                IsFree = true;
                LogManager.Log(ex, $"Error loading job '{(_job_to_load != null ? _job_to_load.Name : "null")}'");
                await NotificationProvider.ShowError("An error occurred while trying to load the selected job.");
                _can_navigate_back = true;
                await NavigationManager.NavigateBack();
            }
            finally
            {
                IsFree = true;
            }
        }

        private void LoadJobModel()
        {
            SpoolType PPCSpoolType = null;

            if (Settings.SpoolTypeGuid.IsNotNullOrEmpty())
            {
                PPCSpoolType = _spoolTypes.FirstOrDefault(x => x.Guid == Settings.SpoolTypeGuid);
            }
            else
            {
                PPCSpoolType = _spoolTypes.FirstOrDefault(x => x.Type == BL.Enumerations.SpoolTypes.StandardSpool);
            }

            if (Job.SpoolType.Type == BL.Enumerations.SpoolTypes.FlatSpool)
            {
                PPCSpoolType = _spoolTypes.FirstOrDefault(x => x.Type == BL.Enumerations.SpoolTypes.FlatSpool);
            }

            var jobModel = new JobModel(ColorSpaces, BuildProvider.IsEureka)
            {
                Name = Job.Name,
                Guid = Job.Guid,
                ID = Job.ID,
                CreationDate = Job.CreationDate,
                LengthPercentageFactor = Job.LengthPercentageFactor,
                NumberOfUnits = Job.NumberOfUnits,
                IsAllSegmentsPerSpool = Job.IsAllSegmentsPerSpool,
                Rml = Job.Rml,
                ColorSpace = Job.ColorSpace,
                SpoolType = Job.SpoolType,
                SpoolTypeName = PPCSpoolType.Name,
                User = Job.User,
                Machine = Job.Machine,
                JobType = Job.JobType,
                InterSegmentLength = Job.InterSegmentLength,
                EnableInterSegment = Job.EnableInterSegment,
                NumberSpools = (BuildProvider.IsEureka ? Job.NumberOfSpools : 1),
                Copies = (BuildProvider.IsEureka ? Job.NumberOfUnits * Job.NumberOfSpools : Job.NumberOfUnits)
            };
            Dictionary<string, SegmentsGroupModel> guidToGroup = new Dictionary<string, SegmentsGroupModel>();
            if (Job.Version < 2)
            {
                int segmentindex = 1;
                foreach (var segm in Job.OrderedSegments)
                {
                    if (segm.BrushStops.Count > 1)
                    {
                        var brushes = segm.BrushStops;
                        Segment currentSegment = segm;
                        double lengthOfOldSegment = segm.Length;
                        for (int index = 0; index < (brushes.Count - 1); index++)
                        {
                            SegmentModel csegmentModel = new SegmentModel(jobModel, segm.Guid)
                            {
                                Name = segm.Name,
                                Length = segm.Length,
                                SegmentIndex = segm.SegmentIndex,
                                IsInterSegment = segm.IsInterSegment,
                                Job = jobModel
                            };

                            csegmentModel.SegmentIndex = segmentindex++;

                            brushes[index].Segment = currentSegment;
                            brushes[index + 1].Segment = currentSegment;
                            csegmentModel.Length = (lengthOfOldSegment * (brushes[index + 1].OffsetPercent - brushes[index].OffsetPercent) / 100d);
                            BrushStopModel brushStopModelFirst = new BrushStopModel(brushes[index], csegmentModel, 1);
                            BrushStopModel brushStopModelSecond = new BrushStopModel(brushes[index + 1], csegmentModel, 1);
                            csegmentModel.CreateGradientBrushes(brushStopModelFirst, brushStopModelSecond);

                            jobModel.Segments.Add(csegmentModel);
                        }
                    }
                    else
                    {
                        SegmentModel segmentModel = LoadSegmentModel(segm, jobModel);
                        segmentModel.SegmentIndex = segmentindex++;
                        jobModel.Segments.Add(segmentModel);
                    }
                }

            }
            else
            {
                var segments = Job.OrderedSegmentsWithGroups;
                foreach (var segment in segments)
                {
                    if (segment is Segment simpleSegment)
                    {
                        SegmentModel segmentModel = LoadSegmentModel(simpleSegment, jobModel);
                        jobModel.Segments.Add(segmentModel);
                    }
                    else if (segment is SegmentsGroup group)
                    {
                        SegmentsGroupModel segmentsGroupModel = new SegmentsGroupModel(jobModel) { SegmentIndex = group.SegmentIndex, Repeats = group.Repeats };

                        foreach (var innerSegment in group.OrderedSegments)
                        {
                            SegmentModel segmentModel = LoadSegmentModel(innerSegment, jobModel);
                            jobModel.Segments.Add(segmentModel);
                            segmentsGroupModel.Segments.Add(segmentModel);
                            segmentModel.SegmentsGroupModel = segmentsGroupModel;
                        }
                        jobModel.SegmentsGroups.Add(segmentsGroupModel);
                    }
                }
            }

            jobModel.InterSegmentLength = Job.EnableInterSegment ? Job.InterSegmentLength : 0;
            jobModel.LoadGroupingSegments();
            JobModel = jobModel;
            //create grouping
            SegmentsCollectionView = CollectionViewSource.GetDefaultView(JobModel.GroupingSegments);
            SegmentsCollectionView.SortDescriptions.Add(new SortDescription(nameof(SegmentModel.SegmentIndex), ListSortDirection.Ascending));
            UndoRedoManager.Instance.ClearAll();
            ArrangeSegmentsIndixes();
            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
            };
            _jsonJobModelLoaded = JsonConvert.SerializeObject(JobModel, Formatting.Indented, settings);
        }

        /// <summary>
        /// Loads the segment model.
        /// </summary>
        /// <param name="segm">The segm.</param>
        /// <param name="jobModel">The job model.</param>
        /// <returns></returns>
        private SegmentModel LoadSegmentModel(Segment segm, JobModel jobModel)
        {
            SegmentModel segmentModel = new SegmentModel(jobModel, segm.Guid)
            {
                Name = segm.Name,
                Length = segm.Length,
                SegmentIndex = segm.SegmentIndex,
                IsInterSegment = segm.IsInterSegment,
                Job = jobModel,
            };
            foreach (var brushStop in segm.BrushStops)
            {
                BrushStopModel brushStopModel = new BrushStopModel(brushStop, segmentModel, Job.Version);
                segmentModel.BrushStops.Add(brushStopModel);
            }

            segmentModel.ArrangeBrushStopsPosition();
            segmentModel.UpdateMiddleColorBrush();
            return segmentModel;
        }

        private void Job_NameChanged(object sender, string e)
        {
            DyeCommand.RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Starts the job.
        /// </summary>
        private async void StartJob()
        {
            if (startingJob) return;

            try
            {
                Debug.WriteLine("Job Starting...");
                startingJob = true;
                LogManager.Log("Start job command pressed. Starting job and navigating to job progress view...");
                await Save();

                var printConfig = new PrintingConfiguration();

                if (HasResumeModel)
                {
                    printConfig.FirstUnitStartPosition = ResumeModel.FirstUnitStartPosition;
                    printConfig.GlobalStartPosition = ResumeModel.GlobalStartPosition;
                    printConfig.RemainingUnits = ResumeModel.RemainingUnits;
                    //LogManager.Log($"!!!!!  Start Job resume :    FirstUnitStartPosition = {ResumeModel.FirstUnitStartPosition}   GlobalStartPosition = {ResumeModel.GlobalStartPosition}    RemainingUnits = {ResumeModel.RemainingUnits}", LogCategory.Debug);

                }

                var handler = await PrintingManager.Print(Job, _db, printConfig);

                RaisePropertyChanged(nameof(CanEdit));
                DropResumeCommand.RaiseCanExecuteChanged();

                if (!BuildProvider.IsEureka)
                {
                    await NavigationManager.NavigateTo<JobsV2Module>(nameof(JobProgressView));
                }

                startingJob = false;
            }
            catch (InsufficientLiquidQuantityException)
            {
                //Ignore..
            }
            catch (OperationCanceledException)
            {
                //Ignore..
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Could not start the current job.");
                await NotificationProvider.ShowError($"{ex.Message}.");
            }
            finally
            {
                startingJob = false;
            }
        }

        /// <summary>
        /// Determines whether this instance [can start job].
        /// </summary>
        private bool CanStartJob()
        {
            try
            {
                //var test = JobModel != null && !JobModel.Segments.SelectMany(x => x.BrushStops).Where(x => x.Position == BrushStopModel.PositionStatus.FirstColor || x.Position == BrushStopModel.PositionStatus.SecondColor).ToList().Exists(x => x.IsOutOfGamut);
                //return Job != null && Job.Validate(_db) && !Job.Segments.SelectMany(x => x.BrushStops).Where(x => !x.IsTransparent && !x.IsWhite).ToList().Exists(x => x.IsOutOfGamut || (x.BrushColorSpace == BL.Enumerations.ColorSpaces.Volume && x.IsLiquidVolumesOutOfRange));
                return JobModel != null && !JobModel.Segments.ToList().Exists(x => x.BrushStops.Count == 0);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return false;
            }
        }

        private async void EditJobDetails()
        {
            try
            {
                LogManager.Log("Editing the job details.");
                JobCreationViewVM vm = new JobCreationViewVM(_spoolTypes.ToList(), _rmls, JobModel.Rml.Guid, JobModel.InterSegmentLength, true);
                vm.UseFlatSpool = Job.SpoolType.Type == BL.Enumerations.SpoolTypes.FlatSpool;
                vm.JobName = JobModel.Name;
                vm.SelectedSpoolType = JobModel.SpoolType;
                vm = await NotificationProvider.ShowDialog<JobCreationViewVM>(vm);

                if (!vm.DialogResult) return;

                Job.Name = vm.JobName;
                JobModel.Name = vm.JobName;
                if (vm.UseFlatSpool)
                {
                    Job.SpoolType = _spoolTypes.FirstOrDefault(x => x.Type == BL.Enumerations.SpoolTypes.FlatSpool);
                }
                else
                {
                    if (Settings.SpoolTypeGuid.IsNotNullOrEmpty())
                    {
                        Job.SpoolType = _spoolTypes.FirstOrDefault(x => x.Guid == Settings.SpoolTypeGuid);
                    }
                    else
                    {
                        Job.SpoolType = _spoolTypes.FirstOrDefault(x => x.Type == BL.Enumerations.SpoolTypes.StandardSpool);
                    }
                }

                JobModel.SpoolType = Job.SpoolType;
                JobModel.SpoolTypeName = Job.SpoolType.Name;

                Job.InterSegmentLength = vm.WhiteGap;
                Job.EnableInterSegment = vm.WhiteGap > 0;
                JobModel.InterSegmentLength = vm.WhiteGap;
                JobModel.EnableInterSegment = vm.WhiteGap > 0;

                //   Job.SpoolType = vm.SelectedSpoolType;
                //  JobModel.SpoolType = vm.SelectedSpoolType;//update  length!!!!

                if (vm.SelectedRML.Guid != SelectedRML.Guid)
                {
                    SelectedRML = _rmls.FirstOrDefault(x => x.Guid == vm.SelectedRML.Guid);
                    // for LAB color remove all available tests
                    foreach (var segment in JobModel.OrderedSegmentsWithGroups)
                    {
                        if (segment is SegmentModel innerSegment)
                        {
                            foreach (var stop in innerSegment.BrushStops.OrderBy(x => x.StopIndex).ToList())
                            {
                                if (stop.ColorSpace == Tango.BL.Enumerations.ColorSpaces.LAB)
                                {
                                    var testColor = TrialsLogEngine.Default.GetByBrushStopGuid(stop.Guid);
                                    if (testColor != null && testColor.TrialslogList.Count > 0)
                                    {
                                        TrialsLogEngine.Default.Delete(testColor);
                                    }
                                }
                            }
                        }
                        else if (segment is SegmentsGroupModel group)
                        {
                            foreach (var segm_group in group.Segments.OrderBy(x => x.SegmentIndex))
                            {
                                foreach (var stop in segm_group.BrushStops.OrderBy(x => x.StopIndex).ToList())
                                {
                                    if (stop.ColorSpace == Tango.BL.Enumerations.ColorSpaces.LAB)
                                    {
                                        var testColor = TrialsLogEngine.Default.GetByBrushStopGuid(stop.Guid);
                                        if (testColor != null && testColor.TrialslogList.Count > 0)
                                        {
                                            TrialsLogEngine.Default.Delete(testColor);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (vm.IsDuplicate)
                {
                    try
                    {
                        await Save();
                        //Duplicate new job
                        var cloned = Job.Clone();
                        cloned.JobIndex = (_db.Jobs.Max(x => x.JobIndex) + 1);
                        _db.Jobs.Add(cloned);
                        await _db.SaveChangesAsync();
                        _job_to_load = cloned;
                        LoadJob();
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error duplicate job.");
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error editing the job.");
            }
        }

        private async void RepeatUnits()
        {
            var maxLength = Job.SpoolType.Length;
            var maxRep = (maxLength == 0 ? 999 : (maxLength / JobModel.Length));

            var vm = await NotificationProvider.ShowDialog<RepeatJobViewVM>(new RepeatJobViewVM("Repeat All", JobModel.NumberOfUnits)
            {
                MaxRepeations = (int)maxRep
            });

            if (vm.DialogResult)
            {
                JobModel.NumberOfUnits = vm.Repeats;
            }
        }

        #endregion

        #region RML Changed

        private async void OnSelectedRmlChanged()
        {
            await LoadRML(SelectedRML);
        }

        private async Task LoadRML(Rml rml)
        {
            if (rml != null)
            {
                if (Job.Rml != rml || rml.Cct == null)
                {
                    bool updateRML = Job.Rml != rml;

                    Job.Rml = await new RmlBuilder(_db)
                        .Set(rml.Guid)
                        .WithActiveParametersGroup()
                        .WithCCT()
                        .WithGbdAndLub()
                        .WithCAT(MachineProvider.Machine.Guid)
                        .WithLiquidFactors()
                        .WithSpools()
                        .BuildAsync();
                    if (JobModel != null)
                    {
                        JobModel.Rml = Job.Rml;
                    }

                    if (updateRML && JobModel != null)
                    {
                        NotificationProvider.SetGlobalBusyMessage("Updating IsOutOfGammut due to the change RML...");
                        foreach (var segment in JobModel.OrderedSegmentsWithGroups)
                        {
                            if (segment is SegmentModel innerSegment)
                            {
                                innerSegment.UpdateWeightOnRMLChange(IsWeightView);
                                innerSegment.UpdateBrushStops();

                            }
                            else if (segment is SegmentsGroupModel group)
                            {
                                foreach (var segm in group.Segments)
                                {
                                    segm.UpdateWeightOnRMLChange(IsWeightView);
                                    segm.UpdateBrushStops();
                                }
                            }
                        }

                        DyeCommand.RaiseCanExecuteChanged();
                        NotificationProvider.ReleaseGlobalBusyMessage();
                    }
                    //if (updateVolumes)
                    //{
                    //    NotificationProvider.SetGlobalBusyMessage("Updating job liquid volumes...");
                    //    foreach (var stop in Job.Segments.SelectMany(x => x.BrushStops).Where(x => x.BrushColorSpace == BL.Enumerations.ColorSpaces.RGB || x.BrushColorSpace == BL.Enumerations.ColorSpaces.LAB).ToList())
                    //    {
                    //        try
                    //        {
                    //            var output = await _converter.ConvertAsync(stop, false, false);
                    //            output.ApplyOnBrushStopVolumesOnly(stop);
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            LogManager.Log(ex, $"Error updating stop volumes after changing thread on segment {stop.Segment.SegmentIndex}, stop {stop.StopIndex}.");
                    //        }
                    //    }
                    //    NotificationProvider.ReleaseGlobalBusyMessage();
                    //}
                }
            }
        }

        #endregion

        #region Segments Management

        /// <summary>
        /// Adds a new  segment.
        /// </summary>
        private void AddNewSegment(ISegmentModel segment)
        {
            try
            {
                LogManager.Log("Adding new segment...");

                UndoRedoManager.Instance.InsertAndExecuteCommand(new AddNewSegmentCommand(JobModel, segment, Settings.DefaultSegmentLength > 0 ? Settings.DefaultSegmentLength : 10));
                ArrangeSegmentsIndixes();
                DyeCommand.RaiseCanExecuteChanged();
                if (segment.SegmentIndex >= (JobModel.GroupingSegments.Count() - 1))
                {
                    View.ScrollToEnd();
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Could not add a new segment.");
                NotificationProvider.ShowError("An error occurred while trying to add a new segment.");
            }
        }

        private async void RepeatSegmentsGroup(SegmentsGroupModel group)
        {
            var maxLength = Job.SpoolType.Length == 0 ? 999 : Job.SpoolType.Length;
            var maxRep = (maxLength - JobModel.Length) / group.Length;

            var vm = await NotificationProvider.ShowDialog<RepeatJobViewVM>(new RepeatJobViewVM($"Edit \"Group {group.SegmentIndex}\" Repeat", group.Repeats)
            {
                MaxRepeations = (int)maxRep
            });

            if (vm.DialogResult)
            {
                group.Repeats = vm.Repeats;
            }
        }

        /// <summary>
        /// Removes the segment.
        /// </summary>
        private async void RemoveSegment(SegmentModel segment)
        {
            if (JobModel.Segments.Count > 1)
            {
                try
                {
                    if (await NotificationProvider.ShowQuestion("Are you sure you want to remove the selected segment?"))
                    {
                        UndoRedoManager.Instance.InsertAndExecuteCommand(new RemoveSegmentCommand(JobModel, segment));
                        ArrangeSegmentsIndixes();

                        DyeCommand.RaiseCanExecuteChanged();
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Could not remove segment.");
                    await NotificationProvider.ShowError("An error occurred while trying to delete segment.");
                }
            }
            else
            {
                await NotificationProvider.ShowInfo("A job must contain at least one color segment.");
            }
        }

        private async void DeleteSegmentsGroup(SegmentsGroupModel segmentsGroup)
        {
            if (JobModel.GroupingSegments.Count > 1)
            {
                try
                {
                    if (await NotificationProvider.ShowQuestion("Are you sure you want to remove the selected group of segments?"))
                    {

                        UndoRedoManager.Instance.InsertAndExecuteCommand(new DeleteSegmentsGroupCommand(JobModel, segmentsGroup));
                        ArrangeSegmentsIndixes();

                        DyeCommand.RaiseCanExecuteChanged();
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Could not remove group of segments.");
                    await NotificationProvider.ShowError("An error occurred while trying to delete group of segments.");
                }
            }
            else
            {
                await NotificationProvider.ShowInfo("A job must contain at least one color segment.");
            }
        }
        /// <summary>
        /// Duplicates the segment.
        /// </summary>
        private void DuplicateSegment(SegmentModel segment)
        {
            UndoRedoManager.Instance.InsertAndExecuteCommand(new DuplicateSegmentCommand(JobModel, segment));
            ArrangeSegmentsIndixes();

            DyeCommand.RaiseCanExecuteChanged();
            if (segment.SegmentIndex >= (JobModel.GroupingSegments.Count() - 1))
            {
                View.ScrollToEnd();
            }
        }

        private void ArrangeSegmentsIndixes()
        {
            JobModel.ArrangeSegmentsIndixes();
            SegmentsCollectionView.Refresh();
        }

        #endregion

        #region Brush Stops Management

        /// <summary>
        /// Click on AddColor button. Add newBrush;
        /// </summary>
        private async void AddColor(SegmentModel segment)
        {
            LogManager.Log($"Adding new color to segment {segment.SegmentIndex}.");

            if ((segment.IsGradient && segment.Length < 10) || (segment.HasColors && segment.Length < 5))
            {
                await NotificationProvider.ShowInfo("Color transitions are best visible with segment length of 5 meters and above.");
            }

            var vm = await NotificationProvider.ShowDialog<ColorSelectionViewVM>(new ColorSelectionViewVM()
            {
                DialogEditObject = new ColorSelectionViewVM.DialogObject()
                {
                    SelectedSegment = segment,
                    BrushStopForEdit = new BrushStopModel(segment),
                    IsEditingMode = false,
                    Catalogs = _catalogs
                }
            });

            if (vm.DialogResult)
            {
                AddBrushStop(segment, vm.SelectedBrushStop);
                DyeCommand.RaiseCanExecuteChanged();
            }
            // SetSegmentLiquidVolumes(segment);
        }

        /// <summary>
        /// Click on Edit Color button. Add newBrush;
        /// </summary>
        private async void EditColor(BrushStopModel brushStop)
        {
            if (brushStop == null)
            {
                await NotificationProvider.ShowError("The edit brush is null!");
                return;
            }
            SegmentModel segment = brushStop.SegmentModel;
            LogManager.Log($"Edit brush stop.");

            var vm = await NotificationProvider.ShowDialog<ColorSelectionViewVM>(new ColorSelectionViewVM()
            {
                DialogEditObject = new ColorSelectionViewVM.DialogObject()
                {
                    SelectedSegment = segment,
                    BrushStopForEdit = brushStop,
                    IsEditingMode = true,
                    Catalogs = _catalogs
                }
            });


            if (vm.DialogResult)
            {
                UndoRedoManager.Instance.InsertAndExecuteCommand(new EditBrushStopColorCommand(segment, brushStop, vm.SelectedBrushStop));
                DyeCommand.RaiseCanExecuteChanged();

            }
        }

        /// <summary>
        /// Adds the brush stop.
        /// </summary>
        public async void AddBrushStop(SegmentModel segment, BrushStopModel newBrushStop)
        {
            if (newBrushStop == null || segment == null)
                return;

            if (segment.IsGradient)
            {
                if (false == _not_show_warning)
                {
                    var vm = await NotificationProvider.ShowDialog<AddSegmentWarningDialogVM>(new AddSegmentWarningDialogVM());
                    if (vm.NotShow)
                    {
                        _not_show_warning = true;
                    }
                }
            }
            UndoRedoManager.Instance.InsertAndExecuteCommand(new AddBrushStopCommand(JobModel, segment, newBrushStop));
            ArrangeSegmentsIndixes();

        }
        #endregion

        #region Job Selection Message

        /// <summary>
        /// Handles the job selected message.
        /// </summary>
        /// <param name="message">The message.</param>
        private void HandleJobSelectedMessage(JobSelectedMessage message)
        {
            _job_to_load = message.Job;
        }

        #endregion

        #region Sample Dye

        /// <summary>
        /// Starts a sample dye.
        /// </summary>
        //private async void StartSampleDye()
        //{
        //    try
        //    {
        //        LogManager.Log("Sample dye command pressed...");

        //        await PrintingManager.PrintSample(Job, _db);

        //        await NavigationManager.NavigateTo<JobsV2Module>(nameof(JobProgressView));
        //    }
        //    catch (Exception ex)
        //    {
        //        LogManager.Log(ex, $"Error executing sample dye for job {Job.Name}.");
        //        await NotificationProvider.ShowError(ex.Message);
        //    }
        //}


        #endregion

        #region Export Embroidery

        private async void ExportEmbroidery()
        {
            try
            {
                if (!StorageProvider.IsConnected)
                {
                    await NotificationProvider.ShowError("No storage device connected.");
                    return;
                }

                var result = await NavigationManager.
                  NavigateForResult<StorageModule,
                  Storage.Views.MainView, ExplorerFileItem,
                  Storage.Models.StorageNavigationRequest>(
                  new Storage.Models.StorageNavigationRequest()
                  {
                      Intent = Storage.Models.StorageNavigationIntent.SaveFile,
                      DefaultFileName = Job.Name + Path.GetExtension(Job.EmbroideryFileName),
                      Filter = Path.GetExtension(Job.EmbroideryFileName),
                      Title = "Export Embroidery File",
                  });

                if (result != null)
                {
                    File.WriteAllBytes(Path.HasExtension(result.Path) ? result.Path : result.Path + Path.GetExtension(Job.EmbroideryFileName), Job.EmbroideryFileData);
                    await NotificationProvider.ShowSuccess("Embroidery file exported successfully.");
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error exporting embroidery file.");
                await NotificationProvider.ShowError("An error occurred while trying to save the selected embroidery file.");
            }
        }

        #endregion

        #region IPPC ViewModel Overrides

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            MachineProvider.MachineOperator.PrintingEnded += MachineOperator_PrintingEnded;
            MachineProvider.MachineOperator.PrintingStarted += MachineOperator_PrintingStarted;

            JobResumeManager.JobResumeUpdated += JobResumeManager_JobResumeUpdated;
            JobResumeManager.JobResumeDropped += JobResumeManager_JobResumeDropped;
        }

        /// <summary>
        /// Called when the navigation system has navigated to this VM view.
        /// </summary>
        public override void OnNavigatedTo()
        {
            if (!MachineProvider.MachineOperator.IsPrinting)
            {
                _start_printing_btn.Push();
            }

            base.OnNavigatedTo();
            LoadJob();
        }



        public override void OnBeforeNavigatedTo()
        {
            base.OnBeforeNavigatedTo();
        }

        public override void OnBeforeNavigatedFrom()
        {
            base.OnBeforeNavigatedFrom();
        }

        /// <summary>
        /// Called when the navigation system has navigated from this VM view.
        /// </summary>
        public override void OnNavigatedFrom()
        {
            _start_printing_btn.Pop();

            base.OnNavigatedFrom();
            _job_to_load_intent = JobNavigationIntent.Default;
        }

        public override void OnNavigatedTo(PPCViewModel fromVM)
        {
            base.OnNavigatedTo(fromVM);
        }

        /// <summary>
        /// Called before the navigation system navigates back from this object.
        /// Return false to abort the navigation.
        /// </summary>
        /// <returns></returns>
        public async override Task<bool> OnNavigateBackRequest()
        {
            if (!IsFree) return false;
            try
            {
                if (!_can_navigate_back)
                {
                    await Save();

                    Job = null;
                    JobModel = null;
                    SegmentsCollectionView = null;
                }
                return true;
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error saving job to database. " + ex.InnerException.Message);
                await NotificationProvider.ShowError("Error saving the current job.");
                return true;
            }
        }

        private async void NavigateBack()
        {
            if (IsFree)
            {
                if (await OnNavigateBackRequest())
                {
                    LogManager.Log("Back command to Jobs pressed.");
                    await NavigationManager.NavigateTo<JobsV2Module>(nameof(JobsView));
                }

            }
        }

        public override void OnApplicationReady()
        {
            base.OnApplicationReady();

            _start_printing_btn = new StartPrintingButton(DyeCommand, MachineProvider.MachineOperator);
        }
        #endregion

        #region INavigationObjectReceiver

        public void OnNavigatedToWithObject(JobNavigationObject e)
        {
            _job_to_load_intent = e.Intent;
            _job_to_load = e.Job;
        }

        #endregion

        #region collapsed mode

        private void JobModeSwitch()
        {
            IsFullMode = !IsFullMode;
            //UndoRedoManager.Instance.ClearAll(); According to QA! request - #6314
        }

        private void InsertWhiteGap()
        {
            JobModel.InsertWhiteGapToSelectedSegments();
        }

        private void Reverse()
        {
            if (false == JobModel.GroupingSegments.ToList().Any(x => x.IsSelected))
                return;

            UndoRedoManager.Instance.InsertAndExecuteCommand(new ReverseCommand(JobModel));

            ArrangeSegmentsIndixes();

        }

        private async void DeleteSegments()
        {
            if (!JobModel.HasSelectedItems)
                return;
            if (JobModel.GroupingSegments.ToList().Where(x => x.IsSelected).ToList().Count == JobModel.GroupingSegments.Count)
            {
                await NotificationProvider.ShowInfo("A job must contain at least one segment. Please, change selection.");
                return;
            }

            if (JobModel.GroupingSegments.Count > 1)
            {
                try
                {
                    if (await NotificationProvider.ShowQuestion("Are you sure you want to remove these selected segments?"))
                    {
                        UndoRedoManager.Instance.InsertAndExecuteCommand(new RemoveSegmentsCommand(JobModel));

                        ArrangeSegmentsIndixes();

                        DyeCommand.RaiseCanExecuteChanged();
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Could not remove segments.");
                    await NotificationProvider.ShowError("An error occurred while trying to delete segments.");
                }
            }
            else
            {
                await NotificationProvider.ShowInfo("A job must contain at least one color segment.");
            }
        }

        private void RepeateSegments()
        {
            if ((JobModel.GroupingSegments.ToList().Where(x => x.IsSelected).Count()) < 2)
                return;
            UndoRedoManager.Instance.InsertAndExecuteCommand(new RepeatCommand(JobModel));
            ArrangeSegmentsIndixes();
        }

        private void UngroupSegments(SegmentsGroupModel segmentsGroup)
        {
            UndoRedoManager.Instance.InsertAndExecuteCommand(new UnGroupSegmentsCommand(JobModel, segmentsGroup));
            ArrangeSegmentsIndixes();
        }

        private void Paste()
        {
            UndoRedoManager.Instance.InsertAndExecuteCommand(new PasteSegmentsCommand(JobModel));
            ArrangeSegmentsIndixes();
        }

        private void Copy()
        {
            UndoRedoManager.Instance.InsertAndExecuteCommand(new CopySegmentCommand(JobModel));
            ArrangeSegmentsIndixes();
        }

        private void Undo()
        {
            UndoRedoManager.Instance.Undo();
            ArrangeSegmentsIndixes();
            DyeCommand.RaiseCanExecuteChanged();
        }

        private void Redo()
        {
            UndoRedoManager.Instance.Redo();
            ArrangeSegmentsIndixes();
            DyeCommand.RaiseCanExecuteChanged();
        }

        private void SetOrDiscardAll()
        {
            InvokeUI(async () =>
            {
                IsBasicMode = await MessageDiscardAllChanges();
            });
        }

        private async Task<bool> MessageDiscardAllChanges()
        {
            try
            {
                MessageDiscardAdvancedOptionsVM vm = new MessageDiscardAdvancedOptionsVM();
                vm = await NotificationProvider.ShowDialog<MessageDiscardAdvancedOptionsVM>(vm);

                if (vm.DialogResult)
                {
                    //delete all 
                    var segments = JobModel.OrderedSegmentsWithGroups;
                    var firstSegment = JobModel.Segments.Where(x => x.SegmentIndex == 1).FirstOrDefault();
                    firstSegment.SegmentsGroupModel = null;
                    JobModel.GroupingSegments.Clear();
                    JobModel.Segments.Clear();
                    JobModel.Segments.Add(firstSegment);
                    JobModel.GroupingSegments.Add(firstSegment);
                    JobModel.EnableInterSegment = false;
                    JobModel.InterSegmentLength = 0;
                    if (firstSegment.IsGradient)
                    {
                        BrushStopModel firstbrush = firstSegment.FirstBrushStop;
                        firstSegment.BrushStops.Clear();
                        firstSegment.BrushStops.Add(firstbrush);
                        firstSegment.ArrangeBrushStopsIndexes();
                    }

                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Save from models to db

        private async Task Save()
        {
            if (JobModel == null)
                return;
            UndoRedoManager.Instance.ClearAll();

            var colorSpaces = await _db.ColorSpaces.ToListAsync();

            Job.ColorSpace = colorSpaces.FirstOrDefault();
            Job.Version = 2;
            Job.Name = JobModel.Name;
            Job.NumberOfUnits = JobModel.NumberOfUnits;
            Job.EnableInterSegment = JobModel.InterSegmentLength > 0;
            Job.InterSegmentLength = JobModel.InterSegmentLength;

            if (Job.SpoolType.Type == BL.Enumerations.SpoolTypes.FlatSpool)
            {
                Job.SpoolType = _spoolTypes.FirstOrDefault(x => x.Code == (int)BL.Enumerations.SpoolTypes.FlatSpool);
            }
            else if (Settings.SpoolTypeGuid != null)
            {
                Job.SpoolType = _spoolTypes.FirstOrDefault(x => x.Guid == Settings.SpoolTypeGuid);
            }
            else
            {
                Job.SpoolType = _spoolTypes.FirstOrDefault(x => x.Code == (int)BL.Enumerations.SpoolTypes.StandardSpool);
            }
            Job.NumberOfSpools = JobModel.NumberSpools;

            var oldSegments = Job.Segments.ToList();

            foreach (var segment in Job.OrderedSegmentsWithGroups.ToList())
            {
                if (segment is Segment)
                {
                    Segment simplesegment = segment as Segment;
                    simplesegment.BrushStops.ToList().ForEach(x =>
                    {
                        _db.BrushStops.Remove(x);
                    });
                    _db.Segments.Remove(simplesegment);
                }
                else if (segment is SegmentsGroup)
                {
                    SegmentsGroup segmentsGroup = segment as SegmentsGroup;
                    foreach (var groupSegment in segmentsGroup.Segments.ToList())
                    {
                        groupSegment.BrushStops.ToList().ForEach(x =>
                        {
                            _db.BrushStops.Remove(x);
                        });
                        _db.Segments.Remove(groupSegment);
                    }
                    _db.SegmentsGroups.Remove(segmentsGroup);
                }
            }

            Job.Segments.Clear();

            Dictionary<int, SegmentsGroup> segmentIndexToGroup = new Dictionary<int, SegmentsGroup>();

            //foreach (var segment in JobModel.Segments.OrderBy(x => x.SegmentIndex).ToList())
            foreach (var segment in JobModel.OrderedSegmentsWithGroups)
            {
                if (segment is SegmentModel innerSegment)
                {
                    var dbSegment = new Segment();
                    dbSegment.Guid = System.Guid.NewGuid().ToString();
                    dbSegment.Name = "Standard Segment";

                    dbSegment.Job = Job;
                    dbSegment.SegmentIndex = segment.SegmentIndex;
                    dbSegment.Length = segment.Length;
                    _db.Segments.Add(dbSegment);
                    List<BrushStop> brushStopList = new List<BrushStop>();//to prevent collection changed event and override offset in timer
                    foreach (var stop in innerSegment.BrushStops.OrderBy(x => x.StopIndex).ToList())
                    {
                        var dbStop = new BrushStop();
                        dbStop.Guid = stop.Guid;
                        dbStop.Segment = dbSegment;
                        //_db.BrushStops.Add(dbStop);
                        brushStopList.Add(dbStop);

                        dbStop.ColorSpace = colorSpaces.FirstOrDefault(x => x.Code == (int)stop.ColorSpace);
                        dbStop.Red = stop.Red;
                        dbStop.Green = stop.Green;
                        dbStop.Blue = stop.Blue;
                        dbStop.L = stop.L;
                        dbStop.A = stop.A;
                        dbStop.B = stop.B;
                        dbStop.Cyan = stop.Cyan;
                        dbStop.Magenta = stop.Magenta;
                        dbStop.Yellow = stop.Yellow;
                        dbStop.Black = stop.Black;
                        dbStop.BestMatchR = stop.BestMatchColor.R;
                        dbStop.BestMatchG = stop.BestMatchColor.G;
                        dbStop.BestMatchB = stop.BestMatchColor.B;

                        dbStop.OffsetPercent = stop.OffsetPercent;
                        dbStop.StopIndex = stop.StopIndex;
                        dbStop.IsOutOfGamut = stop.IsOutOfGamut;

                        dbStop.SetVolume(LiquidTypes.Cyan, stop.Cyan);
                        dbStop.SetVolume(LiquidTypes.Magenta, stop.Magenta);
                        dbStop.SetVolume(LiquidTypes.Yellow, stop.Yellow);
                        dbStop.SetVolume(LiquidTypes.Black, stop.Black);

                        dbStop.ColorCatalog = stop.ColorCatalog;
                        dbStop.ColorCatalogsItem = stop.ColorCatalogsItem;
                    }
                    _db.BrushStops.AddRange(brushStopList);
                }
                else if (segment is SegmentsGroupModel group)
                {
                    SegmentsGroup dbSegmentsGroup = new SegmentsGroup();
                    dbSegmentsGroup.Guid = System.Guid.NewGuid().ToString();
                    dbSegmentsGroup.Repeats = group.Repeats;
                    dbSegmentsGroup.SegmentIndex = group.SegmentIndex;
                    dbSegmentsGroup.Job = Job;
                    _db.SegmentsGroups.Add(dbSegmentsGroup);
                    foreach (var segm_group in group.Segments.OrderBy(x => x.SegmentIndex))
                    {
                        var dbSegment = new Segment();
                        dbSegment.Guid = System.Guid.NewGuid().ToString();
                        dbSegment.Name = "Standard Segment";

                        dbSegment.Job = Job;
                        dbSegment.SegmentIndex = segm_group.SegmentIndex;
                        dbSegment.Length = segm_group.Length;
                        dbSegment.SegmentsGroupGuid = dbSegmentsGroup.Guid;
                        _db.Segments.Add(dbSegment);
                        List<BrushStop> brushStopList = new List<BrushStop>();//to prevent collection changed event and override offset in timer
                        foreach (var stop in segm_group.BrushStops.OrderBy(x => x.StopIndex).ToList())
                        {
                            var dbStop = new BrushStop();
                            dbStop.Segment = dbSegment;
                            //_db.BrushStops.Add(dbStop);
                            brushStopList.Add(dbStop);

                            dbStop.ColorSpace = colorSpaces.FirstOrDefault(x => x.Code == (int)stop.ColorSpace);
                            dbStop.Red = stop.Red;
                            dbStop.Green = stop.Green;
                            dbStop.Blue = stop.Blue;
                            dbStop.L = stop.L;
                            dbStop.A = stop.A;
                            dbStop.B = stop.B;
                            dbStop.Cyan = stop.Cyan;
                            dbStop.Magenta = stop.Magenta;
                            dbStop.Yellow = stop.Yellow;
                            dbStop.Black = stop.Black;
                            dbStop.BestMatchR = stop.BestMatchColor.R;
                            dbStop.BestMatchG = stop.BestMatchColor.G;
                            dbStop.BestMatchB = stop.BestMatchColor.B;

                            dbStop.OffsetPercent = stop.OffsetPercent;
                            dbStop.StopIndex = stop.StopIndex;
                            dbStop.IsOutOfGamut = stop.IsOutOfGamut;

                            dbStop.SetVolume(LiquidTypes.Cyan, stop.Cyan);
                            dbStop.SetVolume(LiquidTypes.Magenta, stop.Magenta);
                            dbStop.SetVolume(LiquidTypes.Yellow, stop.Yellow);
                            dbStop.SetVolume(LiquidTypes.Black, stop.Black);

                            dbStop.ColorCatalog = stop.ColorCatalog;
                            dbStop.ColorCatalogsItem = stop.ColorCatalogsItem;
                        }
                        _db.BrushStops.AddRange(brushStopList);
                    }
                }
            }
            Job.LastUpdated = DateTime.UtcNow;
            Job.IsSynchronized = false;

            if (Job.JobStatus != JobStatuses.Draft)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                    PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
                };
                string jsonJobModelSaved = JsonConvert.SerializeObject(JobModel, Formatting.Indented, settings);
                var json1 = JObject.Parse(_jsonJobModelLoaded);
                var json2 = JObject.Parse(jsonJobModelSaved);
                if (false == JToken.DeepEquals(json1, json2))
                    Job.JobStatus = BL.Enumerations.JobStatuses.Draft;
            }

            RaiseMessage(new JobSavedMessage() { Job = Job });

            await _db.SaveChangesAsync();

            JsonSerializerSettings settings1 = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
            };
            _jsonJobModelLoaded = JsonConvert.SerializeObject(JobModel, Formatting.Indented, settings1);

        }

        #endregion

        #region Machine Operator Events

        private void MachineOperator_PrintingStarted(object sender, PrintingEventArgs e)
        {
            RaisePropertyChanged(nameof(CanEdit));
            InvokeUI( () =>
            {
                DropResumeCommand.RaiseCanExecuteChanged();
            });
        }

        private void MachineOperator_PrintingEnded(object sender, Integration.Operation.PrintingEventArgs e)
        {
            if (IsVisible)
            {
                _start_printing_btn.Push();
            }

            RaisePropertyChanged(nameof(CanEdit));
            InvokeUI(() =>
            {
                DropResumeCommand.RaiseCanExecuteChanged();
            });
        }


        #endregion

        #region Resume

        private void JobResumeManager_JobResumeUpdated(object sender, Common.Resume.JobResumeUpdatedEventArgs e)
        {
            if (Job.Guid == e.JobGuid)
            {
                ResumeModel = e.ResumeModel;
            }
        }

        private void JobResumeManager_JobResumeDropped(object sender, Common.Resume.JobResumeDroppedEventArgs e)
        {
            if (Job.Guid == e.JobGuid)
            {
                ResumeModel = null;
            }
        }

        private async void DropResume()
        {
            if (Job != null && HasResumeModel)
            {
                if (await NotificationProvider.ShowQuestion("Are you sure you want to Edit the Job?" + System.Environment.NewLine + "Job progress data will reset."))
                {
                    JobResumeManager.DropResume(Job.Guid);
                }
            }
        }

        #endregion
    }
}