aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/JobModel.cs
blob: 35569d74f55f904c55cdf49f3325502b14fd6484 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tango.PPC.Maintenance.Views
{
    /// <summary>
    /// Interaction logic for GeneralGuideView.xaml
    /// </summary>
    public partial class GeneralGuideView : UserControl
    {
        public GeneralGuideView()
        {
            InitializeComponent();
        }

        private void TouchCheckBox_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
a id='n315' href='#n315'>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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.ColorConversion;
using Tango.Core;
using Tango.Core.DI;
using Tango.PPC.Common.Notifications;

namespace Tango.PPC.Jobs.Models
{
    public class JobModel : ExtendedObject
    {
        private bool _preventChange;

        #region Properties

        public String Guid { get; set; }

        public int ID { get; set; }

        protected String _name;
        
        public String Name
        {
            get
            {
                return _name;
            }

            set
            {
                if (_name != value)
                {
                    _name = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        protected DateTime _creationdate;

        /// <summary>
        /// Gets or sets the JobModel creation date.
        /// </summary>
        [JsonIgnore]
        public DateTime CreationDate
        {
            get
            {
                return _creationdate;
            }
            set
            {
                if (_creationdate != value)
                {
                    _creationdate = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        private double _lastLength;
        public double Length
        {
            get
            {
                _lastLength = GetLength();
                return _lastLength;
            }
        }

        private double GetLength()
        {
            if (Segments != null)
            {
                if((EnableInterSegment && IsAllSegmentsPerSpool))
                    return GroupingSegments.Count > 0 ? (GroupingSegments.Sum(x => x.LengthWithInterSegment) ) : 0;
                return GroupingSegments.Sum(x => x.FullLength);
                //return Segments.Sum(x => x.LengthWithInterSegment);
               // return Segments.Sum(x => x.LengthWithFactor) + ((EnableInterSegment && IsAllSegmentsPerSpool) ? (InterSegmentLength * (Segments.Count > 0 ? Segments.Count - 1 : Segments.Count)) : 0);
            }
            else
            {
                return 0;
            }
        }

        protected Double _lengthpercentagefactor;

        /// <summary>
        /// Gets or sets the JobModel length percentage factor.
        /// </summary>

        public Double LengthPercentageFactor
        {
            get
            {
                return _lengthpercentagefactor;
            }

            set
            {
                if (_lengthpercentagefactor != value)
                {
                    _lengthpercentagefactor = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        protected Int32 _numberofunits;
        /// <summary>
        /// Gets or sets the JobModel number of units.
        /// </summary>
        public Int32 NumberOfUnits
        {
            get
            {
                return _numberofunits;
            }

            set
            {
                if (_numberofunits != value)
                {
                    _numberofunits = value;
                    RaisePropertyChangedAuto();
                    OnUpdateLengthhWeight();
                    RaisePropertyChanged(nameof(GetEstimatedDuration));
                }
            }
        }
        
        protected Double _intersegmentlength;

        /// <summary>
        /// Gets or sets the JobModel inter segment length.
        /// </summary>
        public Double InterSegmentLength
        {
            get
            {
                return _intersegmentlength;
            }

            set
            {
                if (_intersegmentlength != value)
                {
                    _intersegmentlength = value;
                    RaisePropertyChangedAuto();
                    OnInterSegmentlengthChanged();
                }
            }
        }

        protected SpoolsDistributions _spoolsdistribution;
        public bool IsAllSegmentsPerSpool
        {
            get { return ((SpoolsDistributions)_spoolsdistribution) == SpoolsDistributions.AllSegments; }
            set
            {
                _spoolsdistribution = value ? SpoolsDistributions.AllSegments : SpoolsDistributions.SingleSegment;
                RaisePropertyChangedAuto();
            }
        }
        /// <summary>
        /// Gets the total job segments length multiplied by number of units if it is an embroidery job.
        /// </summary>
        [JsonIgnore]
        public double LengthIncludingNumberOfUnits
        {
            get
            {
                //JsonConvert.SerializeObject(this,Formatting.Indented);

                _lastLength = GetLength();
                var l = _lastLength * Math.Max(NumberOfUnits, 1);
                
                if (EnableInterSegment && NumberOfUnits > 1)
                {
                    l += ((NumberOfUnits - 1) * InterSegmentLength);
                }

                return l;
            }
        }

        [JsonIgnore]
        public double LengthIncludingNumberOfUnitsAndSpools
        {
            get
            {
                if(NumberSpools >= 4)
                {
                    return LengthIncludingNumberOfUnits * NumberSpools;
                }
                
                return LengthIncludingNumberOfUnits;
            }
        }

        protected Rml _rml;

        /// <summary>
        /// Gets or sets the JobModel rml.
        /// </summary>
        [JsonIgnore]
        public virtual Rml Rml
        {
            get
            {
                return _rml;
            }

            set
            {
                if (_rml != value)
                {
                    _rml = value;
                    RaisePropertyChangedAuto();
                    RaisePropertyChanged( nameof(GetEstimatedDuration));
                    RaisePropertyChanged(nameof(GramPerLength));
                    OnUpdateLengthhWeight();
                }
            }
        }

        public string RmlGuid
        {
            get
            {
                return Rml.Guid;
            }
        }
        protected ColorSpace _colorspace;

        /// <summary>
        /// Gets or sets the JobModel color spaces.
        /// </summary>
        [JsonIgnore]
        public virtual ColorSpace ColorSpace
        {
            get
            {
                return _colorspace;
            }
            set
            {
                if (_colorspace != value)
                {
                    _colorspace = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        /// <summary>
        /// Gets or sets the available color spaces.
        /// </summary>
        [JsonIgnore]
        public List<ColorSpace> ColorSpacesList { get; set; }
        
        protected Customer _customer;

        /// <summary>
        /// Gets or sets the JobModel customer.
        /// </summary>
        [JsonIgnore]
        public virtual Customer Customer
        {
            get
            {
                return _customer;
            }
            set
            {
                if (_customer != value)
                {
                    _customer = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        protected Machine _machine;

        /// <summary>
        /// Gets or sets the JobModel machine.
        /// </summary>
        [JsonIgnore]
        public virtual Machine Machine
        {
            get
            {
                return _machine;
            }

            set
            {
                if (_machine != value)
                {
                    _machine = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        protected SpoolType _spooltype;

        /// <summary>
        /// Gets or sets the JobModel spool types.
        /// </summary>
        [JsonIgnore]
        public virtual SpoolType SpoolType
        {
            get
            {
                return _spooltype;
            }

            set
            {
                if (_spooltype != value)
                {
                    _spooltype = value;
                    RaisePropertyChangedAuto();

                }
            }
        }

        private string _spoolTypeName;

        public string SpoolTypeName
        {
            get { return _spoolTypeName; }
            set {
                _spoolTypeName = value;
                RaisePropertyChangedAuto();
            }
        }


        public string SpoolGuid
        {
            get {  return SpoolType.Guid; }
        }

        private int _numberSpools;
        [JsonIgnore]
        public int NumberSpools
        {
            get { return _numberSpools; }
            set {
                if(_numberSpools != value)
                {
                    _numberSpools = value;
                    RaisePropertyChangedAuto();
                    OnNumberOfSpoolsChanged();
                    OnUpdateLengthhWeight();
                }
            }
        }

        protected Int32 _copies;
        [JsonIgnore]
        public Int32 Copies //_numberOfUnitsMultipliedBySpools
        {
            get
            {
                return _copies;
            }

            set
            {
                if (_copies != value)
                {
                    if (NumberSpools>=4)
                    {
                        if(value < NumberSpools)
                            _copies = NumberSpools;
                        else
                        {
                            int coeff = (int)(value + NumberSpools - 1) / NumberSpools;
                            _copies = coeff * NumberSpools;
                        }
                        NumberOfUnits = (int)_copies / NumberSpools;
                        OnUpdateLengthhWeight();
                    }
                    else
                        _copies = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        protected User _user;

        /// <summary>
        /// Gets or sets the JobModel user.
        /// </summary>
        [JsonIgnore]
        public virtual User User
        {
            get
            {
                return _user;
            }

            set
            {
                if (_user != value)
                {
                    _user = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        public JobTypes JobType { get; set;  }
        
        protected SynchronizedObservableCollection<SegmentModel> _segments;

        /// <summary>
        /// Gets or sets the JobModel segments.
        /// </summary>
        [JsonIgnore]
        public SynchronizedObservableCollection<SegmentModel> Segments
        {
            get
            {
                return _segments;
            }
            set
            {
                if (_segments != value)
                {
                    _segments = value;
                    RaisePropertyChangedAuto();
                    OnSegmentsChanged();
                }
            }
        }

        protected SynchronizedObservableCollection<SegmentsGroupModel> _segmentsGroups;

        /// <summary>
        /// Gets or sets the JobModel segments.
        /// </summary>
        [JsonIgnore]
        public SynchronizedObservableCollection<SegmentsGroupModel> SegmentsGroups
        {
            get
            {
                return _segmentsGroups;
            }
            set
            {
                if (_segmentsGroups != value)
                {
                    _segmentsGroups = value;
                    RaisePropertyChangedAuto();
                   
                }
            }
        }

        private bool _enableintersegment;
        public bool EnableInterSegment
        {
            get
            {
                return _enableintersegment;
            }

            set
            {
                if (_enableintersegment != value)
                {
                    _enableintersegment = value;
                    RaisePropertyChangedAuto();
                    OnEnableInterSegmentChanged();
                }
            }
        }
        
        private ObservableCollection<SegmentModel> _effectiveSegments;
        [JsonIgnore]
        public ObservableCollection<SegmentModel> EffectiveSegments
        {
            get
            {
                 return _effectiveSegments;
            }
        }

        protected SynchronizedObservableCollection<ISegmentModel> _groupingSegments;
        /// <summary>
        /// Gets or sets the display segments.
        /// </summary>
        [JsonIgnore]
        public SynchronizedObservableCollection<ISegmentModel> GroupingSegments
        {
            get
            {
                return _groupingSegments;
            }
            set
            {
                if (_groupingSegments != value)
                {
                    _groupingSegments = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        private bool _selectAllSegments;
        [JsonIgnore]
        public bool SelectAllSegments
        {
            get { return _selectAllSegments; }
            set
            {
                _selectAllSegments = value;
                OnSelectAllChanged();
                RaisePropertyChangedAuto();
            }
        }

        [JsonIgnore]
        public bool HasSelectedItems
        {
            get {
                return GroupingSegments.Any(x => x.IsSelected);
            }
        }

        [JsonIgnore]
        public bool IsReverseEnable
        {
            get
            {
                var items = GroupingSegments.Where(x=> x.IsSelected).ToList();
                
                foreach(var item in items)
                {
                    if(item is SegmentModel seg)
                    {
                        if(seg.IsGradient)
                            return true;
                    }
                    else if (item is SegmentsGroupModel group)
                    {
                        foreach (var segment in group.Segments)
                        {
                            if (segment.IsGradient)
                            {
                                return true;
                            }
                        }
                    }
                }
                return false ;
            }
        }

        private List<ISegmentModel> _segmentsToCopy;
        [JsonIgnore]
        public List<ISegmentModel> SegmentsToCopy
        {
            get
            {
                return _segmentsToCopy;
            }
            set
            {
                if (_segmentsToCopy != value)
                {
                    _segmentsToCopy = value;
                    RaisePropertyChanged(nameof(HasSegmentsToCopy));
                }
            }
        }

        [JsonIgnore]
        public int IndexToPasteCopySegments { get; set; }

        [JsonIgnore]
        public bool HasSegmentsToCopy
        {
            get
            {
                return SegmentsToCopy.Count > 0;
            }
        }

        
        ProcessParametersTable _processParametersTable;
       [JsonIgnore]
        public ProcessParametersTable ProcessParametersTable
        {
            get
            {
                InitializeProcessParameters();
                return _processParametersTable;
            }
            set
            {
                if (value != _processParametersTable)
                {
                    _processParametersTable = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        private  void InitializeProcessParameters()
        {
            _processParametersTable =  GetRecommendedProcessParameters();
        }

        [JsonIgnore]
        public TimeSpan GetEstimatedDuration
        {
            get
            {
                if (Rml == null || Rml.GetActiveProcessGroup().ProcessParametersTables == null)
                    return TimeSpan.Zero;
                var processParameters = ProcessParametersTable;
                if(processParameters == null)
                    processParameters = Rml.GetActiveProcessGroup().ProcessParametersTables.FirstOrDefault();

                if (processParameters.DyeingSpeed != 0)
                {
                    return TimeSpan.FromSeconds((LengthIncludingNumberOfUnits + processParameters.DryerBufferLengthMeters) / (processParameters.DyeingSpeed / 100d));
                }
                return TimeSpan.Zero;
            }
        }

        [JsonIgnore]
        public int LastGroupID
        {
            get
            {
                var groups = GroupingSegments.ToList().OfType<SegmentsGroupModel>().ToList();
                return groups.Count() == 0 ? 0 : groups.Max(x => x.SegmentIndex);
            }
        }

        /// <summary>
        /// Gets the ordered segments with groups.
        /// </summary>
        public List<ISegmentModel> OrderedSegmentsWithGroups
        {
            get
            {
                if (Segments == null)
                    return null;
                List<ISegmentModel> orderedSegmentsWithGroups = new List<ISegmentModel>();

                orderedSegmentsWithGroups.AddRange(Segments.Where(x => x.SegmentsGroupModel == null));
                orderedSegmentsWithGroups.AddRange(SegmentsGroups);
                return orderedSegmentsWithGroups.OrderBy(x => x.SegmentIndex).ToList();
            }
        }

        


        [JsonIgnore]
        public bool IsEureka { get; set; }

        [JsonIgnore]
        public double GramPerLength
        {
            get
            {
                if(Rml != null)
                    return Rml.GetGramPer1000mLength;
                return 0d;
            }
        }
        
        [JsonIgnore]
        public double WeightIncludingNumberOfUnits
        {
            get
            {
                if (Rml == null)
                    return 0;

                var gramPerlength = Rml.GetGramPer1000mLength;
                var weight = (LengthIncludingNumberOfUnits * gramPerlength) / (1000);//(g)
                return weight;
            }
        }
        
        [JsonIgnore]
        public double WeightIncludingNumberOfUnitsAndSpools
        {
            get
            {
                if (NumberSpools >= 4)
                {
                    return WeightIncludingNumberOfUnits * NumberSpools;
                }

                return WeightIncludingNumberOfUnits;
            }
        }

        #endregion

        #region constructors

        public JobModel(List<ColorSpace> list, bool isEureka)
        {
            ColorSpacesList = list;
            _segments = new SynchronizedObservableCollection<SegmentModel>();
            _segmentsGroups = new SynchronizedObservableCollection<SegmentsGroupModel>();
            _groupingSegments = new SynchronizedObservableCollection<ISegmentModel>();
            SegmentsToCopy = new List<ISegmentModel>();
            _effectiveSegments = new ObservableCollection<SegmentModel>();

            Segments.CollectionChanged -= Segments_CollectionChanged;
            Segments.CollectionChanged += Segments_CollectionChanged;

            GroupingSegments.CollectionChanged -= SegmentsGroup_CollectionChanged;
            GroupingSegments.CollectionChanged += SegmentsGroup_CollectionChanged;
            SelectAllSegments = false;
            //if not eurika
            if (!isEureka)
                NumberOfUnits = 1;
            else
                NumberOfUnits = 4;
            IsEureka = isEureka;
        }

        #endregion

        #region modifications

        private void OnSegmentsChanged()
        {
            LoadGroupingSegments();
        }
        
        /// <summary>
        /// Loads the grouping segments.
        /// </summary>
        public void LoadGroupingSegments()
        {
            _groupingSegments.Clear();
            //List<ISegmentModel> segmentsWithGroups = new List<ISegmentModel>();

            //segmentsWithGroups.AddRange(Segments.Where(x => x.SegmentsGroupModel == null));
            //segmentsWithGroups.AddRange(SegmentsGroups);
            OrderedSegmentsWithGroups.ForEach(x => _groupingSegments.Add(x));
           
            if (_groupingSegments.Count > 0)
                _groupingSegments.Last().IsLast = true;
            RaisePropertyChanged(nameof(GroupingSegments));
        }

        /// <summary>
        /// Arranges the segment indixes in groups.
        /// </summary>
        public void ArrangeSegmentIndixesInGroups()
        {
            foreach (var group in SegmentsGroups)
            {
                int index = 1;
                foreach (var segm in Segments)
                {
                    segm.SegmentIndex = index;
                    index++;
                }
            }
        }

        /// <summary>
        /// Arranges the segments indixes.
        /// </summary>
        public void ArrangeSegmentsIndixes()
        {
            int index = 1;
            int count = GroupingSegments.Count();
            ArrangeSegmentIndixesInGroups();
            foreach (var segment in GroupingSegments)
            {
                segment.IsLast = (index == count) ? true : false;
                segment.SegmentIndex = index++;
            }
            UpdateEffectiveSegments();
        }

        /// <summary>
        /// Creates the inter segment.
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public static SegmentModel CreateInterSegment(double length)
        {
            SegmentModel segment =  new SegmentModel()
            {
                IsInterSegment = true,
                Length = length,
                Name = "Inter Segment"
            };
            segment.BrushStops.Add(new BrushStopModel(segment)
            {
                ColorSpace = ColorSpaces.RGB,
                Color = System.Windows.Media.Colors.White,
                LastChangedColorSpace = ColorSpaces.RGB

            });
            return segment;
        }

        /// <summary>
        /// Adds the new segment.
        /// </summary>
        /// <param name="length">The length.</param>
        /// <param name="segmentIndex">Index of the segment.</param>
        /// <returns></returns>
        public SegmentModel AddNewSegment( double length, Int32 segmentIndex)
        {
            SegmentModel segment = new SegmentModel();
            segment.Name = "Standard Segment";
            segment.SegmentIndex = segmentIndex + 1;
            segment.Length = length;

            segment.Job = this;
            segment.EnableInterSegment = InterSegmentLength > 0;
            
            Segments.Add(segment);
            if (segmentIndex < GroupingSegments.Count())
            {
                GroupingSegments.Insert(segmentIndex , segment);
            }
            else
                GroupingSegments.Add(segment);
            ArrangeSegmentsIndixes();
            
            return segment;
        }

        protected ProcessParametersTable GetRecommendedProcessParameters()
        {
                try
                {
                    IColorConverter _converter = new DefaultColorConverter();
                    bool useLightInks = true;
                    if (OrderedSegmentsWithGroups.Count > 1 && !EnableInterSegment)
                        useLightInks = false;
                    List<BrushStop> stops = new List<BrushStop>();
                    foreach (var segment in OrderedSegmentsWithGroups)
                    {
                        if (segment is SegmentModel innerSegment)
                        {
                            foreach (var stop in innerSegment.BrushStops.OrderBy(x => x.StopIndex).ToList())
                            {
                                BrushStop bs = stop.CreateBrushStop();
                                stops.Add(bs);
                            }
                        }
                        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())
                                {
                                    BrushStop bs = stop.CreateBrushStop();
                                    stops.Add(bs);
                                }
                            }
                        }
                    }
                    return _converter.GetRecommendedProcessParameters(Rml, Machine, stops, Rml.GetActiveProcessGroup(), useLightInks);
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error calculating recommended process parameters.");
                    return null;
                }
        }
        #endregion

        #region changes

        private void OnEnableInterSegmentChanged()
        {
            if(EnableInterSegment == false && InterSegmentLength > 0)
            {
                _intersegmentlength = 0;
                RaisePropertyChanged(nameof(InterSegmentLength));
            }
            _groupingSegments.ToList().ForEach(x => x.EnableInterSegment = EnableInterSegment);
            OnLengthChanged();
            UpdateEffectiveSegments();
        }

        private void UpdateEffectiveSegments()
        {
            if (EnableInterSegment && IsAllSegmentsPerSpool)
            {
                int max = GroupingSegments.Count > 0 ? GroupingSegments.Max(x => x.SegmentIndex) : 0;

                ObservableCollection<SegmentModel> effectiveSegments = new ObservableCollection<SegmentModel>();

                foreach (var s in GroupingSegments.OrderBy(x => x.SegmentIndex))
                {
                    if (s is SegmentModel)
                    {
                        SegmentModel segment = s as SegmentModel;
                        effectiveSegments.Add(segment);
                        if (segment.SegmentIndex != max && segment.EnableInterSegment)
                        {
                            effectiveSegments.Add(CreateInterSegment(InterSegmentLength));
                        }
                    }
                    else if (s is SegmentsGroupModel)
                    {
                        SegmentsGroupModel segmentsGroup = s as SegmentsGroupModel;
                        List<SegmentModel> segments = segmentsGroup.Segments.ToList();
                        for (int repeats = 0; repeats < segmentsGroup.Repeats; ++repeats)
                        {
                            for (int i = 0; i < segments.Count; ++i)
                            {
                                //if (repeats > 0)
                                //{
                                //    effectiveSegments.Add(segments[i].Clone());
                                //}
                                //else
                                    effectiveSegments.Add(segments[i]);
                                if (EnableInterSegment && !(repeats == (segmentsGroup.Repeats - 1) && i == (segments.Count - 1)))
                                {
                                    effectiveSegments.Add(CreateInterSegment(InterSegmentLength));
                                }
                            }
                        }
                        if (EnableInterSegment && segmentsGroup.SegmentIndex != max )
                        {
                            effectiveSegments.Add(CreateInterSegment(InterSegmentLength));
                        }
                    }
                }
                _effectiveSegments = effectiveSegments;
            }
            else
            {
                ObservableCollection<SegmentModel> effectiveSegments = new ObservableCollection<SegmentModel>();
                foreach (var s in GroupingSegments.OrderBy(x => x.SegmentIndex))
                {
                    if (s is SegmentModel)
                    {
                        SegmentModel segment = s as SegmentModel;
                        effectiveSegments.Add(segment);
                    }
                    else if (s is SegmentsGroupModel)
                    {
                        SegmentsGroupModel segmentsGroup = s as SegmentsGroupModel;
                        List<SegmentModel> segments = segmentsGroup.Segments.ToList();
                        for (int repeats = 0; repeats < segmentsGroup.Repeats; repeats++)
                        {
                            for (int i = 0; i < segments.Count; i++)
                            {
                                //if (repeats > 0)
                                //{
                                //    effectiveSegments.Add(segments[i].Clone());
                                //}
                                //else
                                effectiveSegments.Add(segments[i]);
                            }
                        }
                    }
                }
                _effectiveSegments = effectiveSegments;
            }
            RaisePropertyChanged(nameof(EffectiveSegments));
        }

        /// <summary>
        /// Handles the CollectionChanged event of the Segments collection.
        /// </summary>
        private void Segments_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (var segment in Segments.ToList())
            {
                segment.PropertyChanged -= Segment_PropertyChanged;
                segment.PropertyChanged += Segment_PropertyChanged;
            }
            
            OnLengthChanged();
            //UpdateEffectiveSegments();
        }
        
        /// <summary>
        /// Handles the PropertyChanged event of all job segments.
        /// </summary>
        private void Segment_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(SegmentModel.Length))
            {
                OnLengthChanged();
                UpdateEffectiveSegments();
            }
            else if (e.PropertyName == nameof(SegmentModel.SegmentIndex))
            {
                //UpdateEffectiveSegments();
            }
            else if (e.PropertyName == nameof(SegmentModel.EnableInterSegment))
            {
                OnLengthChanged();
                UpdateEffectiveSegments();
            }
            else if(e.PropertyName == nameof(SegmentModel.IsSelected))
            {
                if (_preventChange) return;
                if(Segments.ToList().TrueForAll(x => x.IsSelected))
                {
                    _selectAllSegments = true;
                }
                else 
                {
                    _selectAllSegments = false;
                }
                RaisePropertyChanged(nameof(SelectAllSegments));
                UpdateSelectedButtons();
            }
        }
        
        private void SegmentsGroup_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (var group in GroupingSegments.ToList())
            {
                if (group is SegmentsGroupModel)
                {
                    (group as SegmentsGroupModel).PropertyChanged -= SegmentsGroup_PropertyChanged;
                    (group as SegmentsGroupModel).PropertyChanged += SegmentsGroup_PropertyChanged;
                }
            }
            OnLengthChanged();
            UpdateEffectiveSegments();
        }

        private void SegmentsGroup_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(SegmentsGroupModel.Repeats))
            {
                OnLengthChanged();
                UpdateEffectiveSegments();
            }
            else if (e.PropertyName == nameof(SegmentModel.IsSelected))
            {
                if (_preventChange) return;
                if (GroupingSegments.ToList().TrueForAll(x => x.IsSelected))
                {
                    _selectAllSegments = true;
                }
                else
                {
                    _selectAllSegments = false;
                }
                RaisePropertyChanged(nameof(SelectAllSegments));
                UpdateSelectedButtons();
            }
        }

        private void UpdateSelectedButtons()
        {
            RaisePropertyChanged(nameof(HasSelectedItems));
            RaisePropertyChanged(nameof(IsReverseEnable));
        }

        /// <summary>
        /// Called when the <see cref="Length"/> property has been changed
        /// </summary>
        public async void OnLengthChanged()
        {
            if (_lastLength != GetLength())
            {
                var maxLength = SpoolType.Length;
                var joblength = LengthIncludingNumberOfUnits;


                if (joblength > maxLength)
                {
                    var maxRep = (maxLength == 0 ? 999 : (maxLength / Length));

                    if(!IsEureka)
                        await TangoIOC.Default.GetInstance<INotificationProvider>().ShowError($"You have reached the maximum length for this job {maxLength}.");

                    //if(NumberOfUnits > 1 && NumberOfUnits > maxRep)
                    //{
                    //    NumberOfUnits = (int)maxRep;
                    //}
                }
                
                OnUpdateLengthhWeight();
                RaisePropertyChanged(nameof(Length));

            }
            RaisePropertyChanged(nameof(GetEstimatedDuration)); 
        }

        private void OnUpdateLengthhWeight()
        {
            RaisePropertyChanged(nameof(LengthIncludingNumberOfUnits));
            RaisePropertyChanged(nameof(LengthIncludingNumberOfUnitsAndSpools));
            RaisePropertyChanged(nameof(WeightIncludingNumberOfUnits));
            RaisePropertyChanged(nameof(WeightIncludingNumberOfUnitsAndSpools));
        }

        private void OnSelectAllChanged()
        {
            if (Segments != null )
            {
                _preventChange = true;
                GroupingSegments.ToList().ForEach(x => x.IsSelected = SelectAllSegments);
                _preventChange = false;
                UpdateSelectedButtons();
            }
        }

        private void OnInterSegmentlengthChanged()
        {
            _preventChange = true;
            if (GroupingSegments.Count > 0)
            {
                int max = GroupingSegments.Max(x => x.SegmentIndex);
                GroupingSegments.Where(i => i.SegmentIndex != max).ToList().ForEach(x => x.EnableInterSegment = InterSegmentLength > 0);
            }
            _enableintersegment = InterSegmentLength > 0;
            _preventChange = false;
           
            OnLengthChanged();
            UpdateEffectiveSegments();
        }
        
        private void OnNumberOfSpoolsChanged()
        {
            if(NumberSpools >= 4)
            {
                int coeff = (int)(Copies + NumberSpools -1) / NumberSpools;
                _copies = coeff * NumberSpools;
                RaisePropertyChanged(nameof(Copies));
            }
        }
        
        #endregion

        #region collapsed mode actions

        public void InsertWhiteGapToSelectedSegments()
        {
            if (false == Segments.ToList().Any(x => x.IsSelected))
                return;
            LogManager.Log("Copy selected segments.");
            int max = GroupingSegments.Max(x => x.SegmentIndex);
            GroupingSegments.Where(i => i.IsSelected && i.SegmentIndex != max).ToList().ForEach(y => y.EnableInterSegment = true);
        }

        public void SwapSegments(int index1, int index2)
        {
            if (index1 < 0 || index1 >= GroupingSegments.Count)
                return;
            if (index2 < 0 || index2 >= GroupingSegments.Count)
                return;
            var tmpIndex = GroupingSegments[index2];
            GroupingSegments[index2] = GroupingSegments[index1];
            GroupingSegments[index1] = tmpIndex;
        }

        #endregion

    }
}