aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/SegmentModel.cs
blob: fb4932a2ef89fb85bae740c2ac7319b67baba699 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Threading;
using Tango.SharedUI;
using System.Windows;
using System.Windows.Media;
using System.Collections.Specialized;
using System.Reflection;
using Tango.BL.Enumerations;
using System.Diagnostics;
using Tango.Core.Commands;
using Tango.PPC.Jobs.UndoRedoCommands;
using Newtonsoft.Json;

namespace Tango.PPC.Jobs.Models
{
    public class SegmentModel : ExtendedObject, ISegmentModel
    {
        private double _lastLength;
        private LinearGradientBrush _brush;
        private ActionTimer _brushStopCollectionChangedActionTimer;

        #region Properties

        public string GUID { get; set; }
        
        protected String _name;
        /// <summary>
        /// Gets or sets the SegmentModel name.
        /// </summary>
        public String Name
        {
            get
            {
                return _name;
            }

            set
            {
                if (_name != value)
                {
                    _name = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        protected double _length;
        /// <summary>
        /// Gets or sets the length.
        /// </summary>
        public double Length
        {
            get
            {
                return _length;
            }
            set
            {
                if (_length != value)
                {
                    _length = value;
                    OnLengthChanged(value);
                    RaisePropertyChangedAuto();
                }
            }
        }

        public double FullLength
        {
            get
            {
                return _length;
            }
        }

        public bool IsGroupSegment
        {
            get
            {
                return SegmentsGroupModel != null;
            }
        }
        
        public void LengthBeforeChange(double value)
        {
            _lastLength = Length;
        }

        public void LengthChanged(double value)
        {
            UndoRedoManager.Instance.InsertAndExecuteCommand(new ChangeLengthCommand(this, _lastLength, value));
            _lastLength = Length;
        }

        private double _weight;

        public double Weight
        {
            get { return _weight; }
            set
            {
                if(_weight != value)
                {
                    _weight = value;
                    OnWeightChanged();
                    RaisePropertyChangedAuto();
                }
            }
        }

        protected Int32 _segmentindex;
        /// <summary>
        /// Gets or sets the index of the segment.
        /// </summary>
        public Int32 SegmentIndex
        {
            get
            {
                return _segmentindex;
            }
            set
            {
                if (_segmentindex != value)
                {
                    _segmentindex = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        private bool _isInterSegment;
        /// <summary>
        /// Gets or sets a value indicating whether this segment is an inter segment.
        /// </summary>
        public bool IsInterSegment
        {
            get { return _isInterSegment; }
            set { _isInterSegment = value;
                RaisePropertyChangedAuto(); }
        }

        protected Boolean _enableintersegment;
        public Boolean EnableInterSegment
        {
            get { return _enableintersegment; }
            set
            {
                if (_enableintersegment != value)
                {
                    _enableintersegment = value;
                    RaisePropertyChangedAuto();
                    
                }
                RaisePropertyChanged(nameof(LengthWithInterSegment));
                RaisePropertyChanged(nameof(InterSegmentLength));
                RaisePropertyChanged(nameof(ShowGap));
            }
        }

        [JsonIgnore]
        public bool ShowGap
        {
            get
            {
                return EnableInterSegment && !IsLast;
            }
        }

        public double LengthWithInterSegment
        {
            get
            {
                if (Job != null && !IsInterSegment)
                {
                    // var length = Length;// + Length * (Job.LengthPercentageFactor / 100);
                    return EnableInterSegment ? (Length + Job.InterSegmentLength) : Length;
                }
                return Length;
                // return Job != null && !IsInterSegment ? (Length + Length * (Job.LengthPercentageFactor / 100)) : Length; 
            }
        }

        public double InterSegmentLength
        {
            get { return Job == null ? 0 : Job.InterSegmentLength; }
        }

        public double LengthWithFactor
        {
            get { return Job != null && !IsInterSegment ? (Length + Length * (Job.LengthPercentageFactor / 100)) : Length; }
        }

        protected SynchronizedObservableCollection<BrushStopModel> _brushstops;

        /// <summary>
        /// Gets or sets the segmentbase brush stops.
        /// </summary>

        public  SynchronizedObservableCollection<BrushStopModel> BrushStops
        {
            get
            {
                return _brushstops;
            }

            set
            {
                if (_brushstops != value)
                {
                    _brushstops = value;
                    OnBrushStopsChanged(_brushstops);
                    RaisePropertyChangedAuto();
                }
            }
        }

        private JobModel _job;
        [JsonIgnore]
        public JobModel Job
        {
            get
            {
                return _job;
            }

            set
            {
                if (_job != value)
                {
                    _job = value;
                }
            }
        }

        [JsonIgnore]
        public Brush SegmentBrush
        {
            get
            {
                return GetSegmentBrush();
            }
        }

        [JsonIgnore]
        public bool IsOffsetChanged { get; set; }
        
        public double LeftOffset
        {
            get { return FirstBrushStop != null? FirstBrushStop.OffsetPercent : 0; }
            set {
                if (FirstBrushStop != null && FirstBrushStop.OffsetPercent != value)
                {
                    FirstBrushStop.OffsetPercent = value;
                    RaisePropertyChangedAuto();
                    IsOffsetChanged = true;
                    RaisePropertyChanged(nameof(SegmentBrush));
                    RaisePropertyChanged(nameof(LeftOffsetLabel));
                    IsOffsetChanged = false;
                }
            }
        }

        private double _leftOffsetChangeComleted;

        [JsonIgnore]
        public double LeftOffsetChangeComleted
        {
            get { return _leftOffsetChangeComleted; }
            set {
                if(_leftOffsetChangeComleted != value)
                {
                    _leftOffsetChangeComleted = value;
                    RaisePropertyChangedAuto();
                    UndoRedoManager.Instance.InsertAndExecuteCommand(new ChangeOffsetCommand(this, LeftOffsetStartChanging, _leftOffsetChangeComleted, OffsetType.Left));
                    
                }
            }
        }

        private double _leftOffsetStartChanging;

        [JsonIgnore]
        public double LeftOffsetStartChanging
        {
            get { return _leftOffsetStartChanging; }
            set
            {
                _leftOffsetStartChanging = value;
                RaisePropertyChangedAuto();
            }
        }

        public double MiddleOffset
        {
            get { return (MiddleBrushStop != null) ? MiddleBrushStop.OffsetPercent : 50; }
            set { 
                
                if (MiddleBrushStop != null && MiddleBrushStop.OffsetPercent != value)
                {
                    MiddleBrushStop.OffsetPercent = value;
                   RaisePropertyChangedAuto();
                    IsOffsetChanged = true;
                    RaisePropertyChanged(nameof(SegmentBrush));
                    RaisePropertyChanged(nameof(MiddleOffsetLabel));
                    IsOffsetChanged = false;
                }
            }
        }

        private double _middleOffsetChangeComleted;

        [JsonIgnore]
        public double MiddleOffsetChangeComleted
        {
            get { return _middleOffsetChangeComleted; }
            set
            {
                if (_middleOffsetChangeComleted != value)
                {
                    _middleOffsetChangeComleted = value;
                    RaisePropertyChangedAuto();
                    UndoRedoManager.Instance.InsertAndExecuteCommand(new ChangeOffsetCommand(this, MiddleOffsetStartChanging, _middleOffsetChangeComleted, OffsetType.Middle));
                   
                }
            }
        }

        private double _middleOffsetStartChanging;

        [JsonIgnore]
        public double MiddleOffsetStartChanging
        {
            get { return _middleOffsetStartChanging; }
            set
            {
                _middleOffsetStartChanging = value;
                RaisePropertyChangedAuto();
            }
        }

        public double RightOffset
        {
            get { return SecondBrushStop != null ? SecondBrushStop.OffsetPercent: 100; }
            set { 
                
                if (SecondBrushStop != null && SecondBrushStop.OffsetPercent != value)
                {
                    SecondBrushStop.OffsetPercent = value;
                    RaisePropertyChangedAuto();
                    IsOffsetChanged = true;
                    RaisePropertyChanged(nameof(SegmentBrush));
                    RaisePropertyChanged(nameof(RightOffsetLabel));
                    IsOffsetChanged = false;
                }
            }
        }

        private double _rightOffsetChangeComleted;
        [JsonIgnore]
        public double RightOffsetChangeComleted
        {
            get { return _rightOffsetChangeComleted; }
            set
            {
                if (_rightOffsetChangeComleted != value)
                {
                    _rightOffsetChangeComleted = value;
                    RaisePropertyChangedAuto();
                    UndoRedoManager.Instance.InsertAndExecuteCommand(new ChangeOffsetCommand(this, RightOffsetStartChanging, _rightOffsetChangeComleted, OffsetType.Right));
                    
                }
            }
        }

        private double _rightOffsetStartChanging;
        [JsonIgnore]
        public double RightOffsetStartChanging
        {
            get { return _rightOffsetStartChanging; }
            set
            {
                _rightOffsetStartChanging = value;
                RaisePropertyChangedAuto();
            }
        }

        [JsonIgnore]
        public String LeftOffsetLabel
        {
            get
            {
                double length = Math.Round( LeftOffset * Length / 100, 1);
                return String.Format($"{Math.Round(LeftOffset,0)}%({length}m)" );
            }
        }

        [JsonIgnore]
        public String RightOffsetLabel
        {
            get
            {
                double length = Math.Round((100-RightOffset) * Length / 100, 1);
                return String.Format($"{Math.Round(RightOffset, 0)}%({length}m)");
            }
        }

        [JsonIgnore]
        public String MiddleOffsetLabel
        {
            get
            {
                double offset = Math.Abs(MiddleOffset - 50);
                double length = Math.Round(offset * Length / 100, 1);
                double roundValue = Math.Round(MiddleOffset, 0);
                return String.Format($"{Math.Round(MiddleOffset, 0)}%({length}m)");
            }
        }

        [JsonIgnore]
        public bool HasColors
        {
            get
            {
                return BrushStops.Count > 0;
            }
        }

        [JsonIgnore]
        public bool IsGradient
        {
            get
            {
                return BrushStops.Count > 1;
            }
        }

        /// <summary>
        /// Gets the second brush stop.
        /// </summary>
        [JsonIgnore]
        public BrushStopModel SecondBrushStop
        {
            get
            {
                if (BrushStops.Count > 1)
                    return BrushStops.Where(x => x.Position == BrushStopModel.PositionStatus.SecondColor).FirstOrDefault();

                return null;
            }
        }

        /// <summary>
        /// Gets the first brush stop.
        /// </summary>
        [JsonIgnore]
        public BrushStopModel FirstBrushStop
        {
            get
            {
                if(BrushStops.Count > 0)
                {
                    var brushStop = BrushStops.Where(x => x.Position == BrushStopModel.PositionStatus.FirstColor).FirstOrDefault();
                    return brushStop;
                }
                
                return null;
            }

        }

        /// <summary>
        /// Gets the middle brush stop.
        /// </summary>
        [JsonIgnore]
        public BrushStopModel MiddleBrushStop
        {
            get
            {
                if (BrushStops.Count > 1)
                    return BrushStops.Where(x => x.Position == BrushStopModel.PositionStatus.Middle).FirstOrDefault();

                return null;
            }
        }

        [JsonIgnore]
        public bool HasOutOfGamutBrush
        {
            get
            {
                bool hasError = (FirstBrushStop != null && FirstBrushStop.IsOutOfGamut)
                    || (SecondBrushStop != null && SecondBrushStop.IsOutOfGamut);
                
                return hasError;
            }
        }

        private bool _isSelected;
        [JsonIgnore]
        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value;
                RaisePropertyChangedAuto();
            }
        }

        private bool _isLast;
        [JsonIgnore]
        public bool IsLast
        {
            get { return _isLast; }
            set
            {
                if (_isLast != value)
                {
                    _isLast = value;
                    RaisePropertyChangedAuto();
                    RaisePropertyChanged(nameof(ShowGap));
                }
            }
        }

        public SegmentsGroupModel SegmentsGroupModel { get; set; }

        #endregion

        #region command
        [JsonIgnore]
        public RelayCommand AddGapCommand { get; set; }
        [JsonIgnore]
        public RelayCommand DeleteGapCommand { get; set; }
        #endregion

        #region construction

        public SegmentModel(JobModel jobModel, string guid)
        {
            InitnewSegment();
            Job = jobModel;
            GUID = guid;
            IsLast = false;
            EnableInterSegment = jobModel.EnableInterSegment;
        }

        public SegmentModel()
        {
            GUID = "";
            InitnewSegment();
        }

        #endregion

        public void InitnewSegment()
        {
            BrushStops = new SynchronizedObservableCollection<BrushStopModel>();
            LeftOffset = 0;
            MiddleOffset = 50;
            RightOffset = 100;
            IsOffsetChanged = false;
            Length = 5;
            _lastLength = 5;
            IsSelected = false;
            IsLast = false;
            _enableintersegment = false;
            AddGapCommand = new RelayCommand(x => AddGap());
            DeleteGapCommand = new RelayCommand(x => DeleteGap());
        }

        #region Public Methods

        /// <summary>
        /// Gets the segment brush.
        /// </summary>
        /// <returns></returns>
        public LinearGradientBrush GetSegmentBrush()
        {
            if (_brush == null || _brush.GradientStops.Count != BrushStops.Count || IsOffsetChanged)
            {
                GradientStopCollection stops = new GradientStopCollection();
                foreach (var stop in BrushStops.ToList().OrderBy(x => x.StopIndex).ToList())
                {
                    //TODO test if displayed is valid stop.IsValid
                   
                    Color color = stop.BestMatchColor;
                     stops.Add(new GradientStop( color, stop.OffsetPercent / 100d));
                }

                LinearGradientBrush brush = new LinearGradientBrush();
                brush.StartPoint = new Point(0, 0);
                brush.EndPoint = new Point(1, 0);

                brush.GradientStops = stops;

                _brush = brush;
                return brush;
            }
            else
            {
                for (int i = 0; i < BrushStops.Count; i++)
                {
                    //TODO test if displayed is valid stop.IsValid
                    Color color = BrushStops[i].BestMatchColor;
                    _brush.GradientStops[i].Color =  color;
                    _brush.GradientStops[i].Offset = BrushStops[i].OffsetPercent / 100d;
                }

                return _brush;
            }
        }

        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public SegmentModel Clone()
        {
            var cloned = new SegmentModel(Job, GUID);//(SegmentModel)Activator.CreateInstance(typeof(SegmentModel), this.Job);

            cloned.Name = Name;
            cloned.LeftOffset = LeftOffset;
            cloned.MiddleOffset = MiddleOffset;
            cloned.RightOffset = RightOffset;
            cloned.IsOffsetChanged = IsOffsetChanged;
            cloned.Length = Length;
            cloned.IsSelected = false;
            cloned.IsInterSegment = IsInterSegment;
            cloned.EnableInterSegment = EnableInterSegment;
            cloned.BrushStops = BrushStops.Select(x => x.Clone()).ToSynchronizedObservableCollection();
            cloned.BrushStops.ToList().ForEach(x => x.SegmentModel = cloned);
            
            cloned.SegmentIndex = SegmentIndex + 1;
            cloned.SegmentsGroupModel = SegmentsGroupModel;
            return cloned;
        }

        ISegmentModel ISegmentModel.Clone()
        {
            return Clone() as ISegmentModel;
        }

        public static SegmentModel CreateInterSegment(double length)
        {
            return new SegmentModel()
            {
                IsInterSegment = true,
                Length = length,
                Name = "Inter Segment",
                BrushStops = new SynchronizedObservableCollection<BrushStopModel>()
                            
            };
        }
        /// <summary>
        /// Creates the gradient brushes.
        /// </summary>
        public void CreateGradientBrushes(BrushStopModel firstBrush, BrushStopModel secondBrush)
        {
            BrushStops.Clear();
            BrushStopModel brushStop = firstBrush.Clone();
            brushStop.StopIndex = 1;
            brushStop.OffsetPercent = 0;
            brushStop.Position = BrushStopModel.PositionStatus.First;
            brushStop.IsOutOfGamut = false;
            BrushStops.Add(brushStop);

            BrushStopModel colorbrushStop = firstBrush;
            colorbrushStop.Position = BrushStopModel.PositionStatus.FirstColor;
            colorbrushStop.StopIndex = 2;
            colorbrushStop.OffsetPercent = 0;
            BrushStops.Add(colorbrushStop);
            
            BrushStopModel middleBrushStop = firstBrush.Clone();
            middleBrushStop.StopIndex = 3;
            middleBrushStop.Position = BrushStopModel.PositionStatus.Middle;
            middleBrushStop.OffsetPercent = 50;
            middleBrushStop.IsOutOfGamut = false;
            middleBrushStop.Color = BrushStopModel.GetRelativeRGB(firstBrush.Color, secondBrush.Color, 0, 1, 0.5);
            middleBrushStop.BestMatchColor = BrushStopModel.GetRelativeRGB(firstBrush.BestMatchColor, secondBrush.BestMatchColor, 0, 1, 0.5);
            BrushStops.Add(middleBrushStop);

            AddOrReplaceSecondBrush(secondBrush);
        }

        public void AddOrReplaceSecondBrush(BrushStopModel secondBrush)
        {
            RemoveSecondColorOfGradient();

            BrushStopModel secondbrushStop = secondBrush;
            secondbrushStop.StopIndex = 4;
            secondbrushStop.Position = BrushStopModel.PositionStatus.SecondColor;
            secondbrushStop.OffsetPercent = 100;
            BrushStops.Add(secondbrushStop);

            BrushStopModel lastSecondBrushStop = secondBrush.Clone();
            lastSecondBrushStop.StopIndex = 5;
            lastSecondBrushStop.OffsetPercent = 100;
            lastSecondBrushStop.IsOutOfGamut = false;
            lastSecondBrushStop.Position = BrushStopModel.PositionStatus.Last; ;
            BrushStops.Add(lastSecondBrushStop);
        }

        public void SetNewColor(BrushStopModel target, BrushStopModel source )
        {
            target.SetNewColor(source);
            target.IsOutOfGamut = source.IsOutOfGamut;
            if (target.Position == BrushStopModel.PositionStatus.FirstColor)
            {
                BrushStopModel first = BrushStops.Where(x => x.Position == BrushStopModel.PositionStatus.First).FirstOrDefault();
                if(first != null)
                {
                    first.SetNewColor(source);
                    first.IsOutOfGamut = source.IsOutOfGamut;
                } 
            }
            else if(target.Position == BrushStopModel.PositionStatus.SecondColor)
            {
                BrushStopModel last = BrushStops.Where(x => x.Position == BrushStopModel.PositionStatus.Last).FirstOrDefault();
                if(last != null)
                {
                    last.SetNewColor(source);
                    last.IsOutOfGamut = source.IsOutOfGamut;
                }
            }
            UpdateMiddleColorBrush();
            RaiseSegmentBrushChanged();
        }
        
        public void UpdateMiddleColorBrush()
        {
            if (MiddleBrushStop != null && SecondBrushStop != null && FirstBrushStop != null)
            {
                MiddleBrushStop.Color = BrushStopModel.GetRelativeRGB(FirstBrushStop.Color, SecondBrushStop.Color, 0, 1, 0.5);
                MiddleBrushStop.BestMatchColor = BrushStopModel.GetRelativeRGB(FirstBrushStop.BestMatchColor, SecondBrushStop.BestMatchColor, 0, 1, 0.5);
            }
        }
        
        public void RemoveSecondColorOfGradient()
        {
            if (BrushStops.Count < 5)
                return ;
           BrushStops.RemoveAt(4);
           BrushStops.RemoveAt(3);
        }

        public void ArrangeBrushStopsIndexes()
        {
            for (int i = 0; i < BrushStops.Count; i++)
            {
                BrushStops[i].StopIndex = i + 1;
            }
            ArrangeBrushStopsPosition();
        }

        public void ArrangeBrushStopsPosition()
        {
            if (BrushStops.Count == 1)
            {
                BrushStops.FirstOrDefault().Position = BrushStopModel.PositionStatus.FirstColor;
            }
            else if (BrushStops.Count > 1)
                BrushStops.ToList().ForEach(x => x.Position = (BrushStopModel.PositionStatus)x.StopIndex);
        }
        
        public void SwapBrushStops()
        {
            if (BrushStops.Count > 1)
            {
                int index = 0;
                int lastIndex = BrushStops.Count - 1;
                for (; index < lastIndex; index++, lastIndex--)
                {
                    var left_temp = BrushStops[index];
                    var right_stopIndex = BrushStops[lastIndex].StopIndex;
                    var right_offsetPercent = BrushStops[lastIndex].OffsetPercent;
                    
                    BrushStops[index] = BrushStops[lastIndex];
                    BrushStops[index].StopIndex = left_temp.StopIndex;
                    BrushStops[index].OffsetPercent = left_temp.OffsetPercent;
                    BrushStops[lastIndex] = left_temp;
                    BrushStops[lastIndex].StopIndex = right_stopIndex;
                    BrushStops[lastIndex].OffsetPercent = right_offsetPercent;
                }
                ArrangeBrushStopsIndexes();
                UpdateMiddleColorBrush();
            }
        }

        #endregion

        #region Brush Stops Collection Changed

        private void BrushStops_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (_brushStopCollectionChangedActionTimer == null)
            {
                _brushStopCollectionChangedActionTimer = new ActionTimer(TimeSpan.FromMilliseconds(100));
            }

            _brushStopCollectionChangedActionTimer.ResetReplace(() =>
            {
                foreach (var stop in BrushStops.ToList())
                {
                    stop.RaiseOffsetChanged();
                }
                RaiseSegmentBrushChanged();
            });
        }

        /// <summary>
        /// Raises the <see cref="SegmentBrush"/> property changed event.
        /// </summary>
        public void RaiseSegmentBrushChanged()
        {
            RaisePropertyChanged(nameof(SegmentBrush));
            RaisePropertyChanged(nameof(HasColors));
            RaisePropertyChanged(nameof(IsGradient));
            RaisePropertyChanged(nameof(FirstBrushStop));
            RaisePropertyChanged(nameof(SecondBrushStop));
            RaisePropertyChanged(nameof(HasOutOfGamutBrush));
            RaisePropertyChanged(nameof(Length));
        }
        #endregion

        #region Properties Changed

        /// <summary>
        /// Called when the Length has changed.
        protected void OnLengthChanged(double length)
        {
            if (Job != null && Job.Rml != null)
            {
                var gramPerlength = Job.Rml.GetGramPer1000mLength;
                var weight = (Length * gramPerlength)/( 1000 );//(kg)
                _weight = weight;
                RaisePropertyChanged(nameof(Weight));
                //if (_lastLength != length)
                {
                    BrushStops.ToList().ForEach(x => x.RaiseOffsetChanged());
                    //_lastLength = Length;
                    //RaisePropertyChanged(nameof(LengthWithFactor));
                    RaisePropertyChanged(nameof(LengthWithInterSegment));
                    RaisePropertyChanged(nameof(LeftOffsetLabel));
                    RaisePropertyChanged(nameof(MiddleOffsetLabel));
                    RaisePropertyChanged(nameof(RightOffsetLabel));
                }
            }
        }

        /// <summary>
        /// Called when [weight changed].
        /// </summary>
        private void OnWeightChanged()
        {
            if (Job != null && Job.Rml != null)
            {
                var gramPerlength = Job.Rml.GetGramPer1000mLength;
                var length = (Weight * 1000 )/ gramPerlength;//(m) weight in gr
                _length = length;
                RaisePropertyChanged(nameof(Length));

                BrushStops.ToList().ForEach(x => x.RaiseOffsetChanged());
                RaisePropertyChanged(nameof(LengthWithInterSegment));
                RaisePropertyChanged(nameof(LeftOffsetLabel));
                RaisePropertyChanged(nameof(MiddleOffsetLabel));
                RaisePropertyChanged(nameof(RightOffsetLabel));
                
            }
        }

        /// <summary>
        /// Called when the BrushStops has changed.
        /// </summary>
        /// <param name="brushstops"></param>
        protected void OnBrushStopsChanged(SynchronizedObservableCollection<BrushStopModel> brushstops)
            {
                if (brushstops != null)
                {
                    brushstops.CollectionChanged -= BrushStops_CollectionChanged;
                    brushstops.CollectionChanged += BrushStops_CollectionChanged;

                    foreach (var stop in brushstops.ToList())
                    {
                        stop.RaiseOffsetChanged();
                    }

                    RaiseSegmentBrushChanged();
                }
            }

            private void AddGap()
            {
                EnableInterSegment = true;
            }

            private void DeleteGap()
            {
                EnableInterSegment = false;
            }

            public void UpdateBrushStops()
            {
                foreach (var stop in BrushStops.Where(x => x.ColorSpace == BL.Enumerations.ColorSpaces.RGB || x.ColorSpace == BL.Enumerations.ColorSpaces.LAB).ToList())
                {
                    try
                    {
                        stop.OnBrushStopFieldValueChanged();
                        stop.InitColorsFromBestmatch();
                        //TODO ASK ROY!!!!!!
                        //output.ApplyOnBrushStopVolumesOnly(stop);
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, $"Error updating stop volumes after changing thread on segment {stop.SegmentModel.SegmentIndex}, stop {stop.StopIndex}.");
                    }
                }
            }

            public void UpdateWeightOnRMLChange( bool isWeightView)
            {
                if(isWeightView)
                {
                    OnWeightChanged();
                }
                else
                {
                    OnLengthChanged(Length);
                }
            }


            #endregion
        }
    }