aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs
blob: cb16c99e7e569d85d97bd3a813798a8663f215e0 (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
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;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Modules;
using Tango.MachineStudio.Common.Navigation;
using Tango.MachineStudio.Common.Threading;
using Tango.SharedUI.Controls;

namespace Tango.MachineStudio.UI.Navigation
{
    /// <summary>
    /// Represents the Machine Studio default <see cref="INavigationManager">Navigation Manager</see>.
    /// </summary>
    /// <seealso cref="Tango.MachineStudio.Common.Navigation.INavigationManager" />
    public class DefaultNavigationManager : ExtendedObject, INavigationManager
    {
        private event Action<Object, Object> NavigationCycleCompleted;

        private IDispatcherProvider _dispatcherProvider;
        private IStudioModuleLoader _moduleLoader;
        private Object _currentVM;
        private String _lastFullPath;
        private bool _preventHistory;
        private bool _navigating_back;

        private Stack<String> _navigationHistory;

        /// <summary>
        /// Gets the current view model.
        /// </summary>
        public StudioViewModel CurrentVM
        {
            get { return _currentVM as StudioViewModel; }
        }

        private IStudioModule _currentModule;
        /// <summary>
        /// Gets or sets the current module.
        /// </summary>
        public IStudioModule CurrentModule
        {
            get { return _currentModule; }
            private set { _currentModule = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Navigates to the previous view.
        /// </summary>
        public RelayCommand NavigateBackCommand { get; private set; }

        /// <summary>
        /// Navigates to the specified full path in command parameter.
        /// </summary>
        public RelayCommand<String> NavigateToCommand { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultNavigationManager"/> class.
        /// </summary>
        /// <param name="moduleLoader">The module loader.</param>
        public DefaultNavigationManager(IStudioModuleLoader moduleLoader, IDispatcherProvider dispatcherProvider)
        {
            _navigationHistory = new Stack<String>();
            _moduleLoader = moduleLoader;

            NavigateToCommand = new RelayCommand<string>(async (x) => await NavigateTo(x));
            NavigateBackCommand = new RelayCommand(async () => await NavigateBack());

            _dispatcherProvider = dispatcherProvider;
        }

        /// <summary>
        /// Navigates to the specified PPC view.
        /// </summary>
        /// <param name="view">The view.</param>
        public Task<bool> NavigateTo(NavigationView view, bool pushToHistory = true)
        {
            LogManager.Log($"Navigating to: {view.ToString()}...");

            _dispatcherProvider.Invoke(() =>
            {
                MainWindow.Instance.NavigationControl.NavigateTo(view.ToString());
            });

            return Task.FromResult(true);
        }

        /// <summary>
        /// Navigates to the specified PPC view with the specified receive object.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="obj"></param>
        /// <param name="pushToHistory"></param>
        /// <returns></returns>
        public Task<bool> NavigateWithObject<TPass>(NavigationView view, TPass obj, bool pushToHistory = true)
        {
            LogManager.Log($"Navigating to: {view.ToString()}, with object {typeof(TPass).Name}...");
            MainWindow.Instance.NavigationControl.NavigateTo(view.ToString());
            INavigationObjectReceiver<TPass> receiver = MainWindow.Instance.NavigationControl.Elements.FirstOrDefault(x => (x.GetType().Name == view.ToString() || NavigationControl.GetNavigationName(x) == view.ToString()) && x.DataContext is INavigationObjectReceiver<TPass>).DataContext as INavigationObjectReceiver<TPass>;

            if (receiver != null)
            {
                receiver.OnNavigatedToWithObject(obj);
            }

            return Task.FromResult(true);
        }

        /// <summary>
        /// Navigates to the specified module.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public Task<bool> NavigateTo<T>(bool pushToHistory = true) where T : IStudioModule
        {
            return NavigateTo(typeof(T));
        }

        /// <summary>
        /// Navigates to the specified module using the view path (e.g MainView.JobsView).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewPath">The view path.</param>
        public Task<bool> NavigateTo<T>(string viewPath, bool pushToHistory = true) where T : IStudioModule
        {
            return NavigateTo<T>(pushToHistory, viewPath.Split('.'));
        }

        /// <summary>
        /// Navigates to the specified module using the view path (e.g MainView,JobsView).
        /// This method makes it easy to do stuff like NavigateTo(nameof(MainView),nameof(JobsView));
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewPath">The view path.</param>
        public Task<bool> NavigateTo<T>(bool pushToHistory = true, params String[] viewPath) where T : IStudioModule
        {
            return NavigateTo(typeof(T), pushToHistory, viewPath);
        }

        /// <summary>
        /// Navigates to the specified module and view by full path (e.g Jobs.JobsView).
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        public async Task<bool> NavigateTo(String fullPath, bool pushToHistory = true)
        {
            String[] path = fullPath.Split('.');
            var module = _moduleLoader.UserModules.SingleOrDefault(x => x.GetType().Name == path[0] || x.Name == path[0]);

            if (path.Length == 1 && path[0] == CurrentModule.Name) return true;

            LogManager.Log($"Navigating to: {fullPath}...");

            var fromVM = _currentVM;

            if (_currentVM != null && _currentVM is INavigationBlocker)
            {
                if (_navigating_back)
                {
                    if (!await (_currentVM as INavigationBlocker).OnNavigateBackRequest())
                    {
                        return false;
                    }
                }
                else
                {
                    if (!await (_currentVM as INavigationBlocker).OnNavigateOutRequest())
                    {
                        return false;
                    }
                }
            }

            if (pushToHistory && _lastFullPath != null && !_preventHistory)
            {
                _navigationHistory.Push(_lastFullPath);
                RaisePropertyChanged(nameof(CanNavigateBack));
            }

            _lastFullPath = fullPath;

            MainWindow.Instance.NavigationControl.NavigateTo(NavigationView.MainView.ToString());
            var navigationControl = MachineStudio.UI.Views.MainView.Instance.NavigationControl;
            CurrentModule = module;
            var moduleView = navigationControl.NavigateTo(module.Name);

            _currentVM = moduleView.DataContext;

            if (path.Length > 1)
            {
                var moduleNavigation = moduleView.FindChildOffline<NavigationControl>();

                moduleNavigation.RegisterForLoadedOrNow(async (x, e) =>
                {
                    foreach (var view in path.Skip(1))
                    {
                        await Task.Delay(100);
                        var v = moduleNavigation.NavigateTo(view);

                        if (v != null)
                        {
                            _currentVM = v.DataContext;

                            if (view != path.Last())
                            {
                                moduleNavigation = v.FindChildOffline<NavigationControl>();
                            }
                        }
                        else
                        {
                            throw LogManager.Log(new ArgumentNullException("Could not navigate to " + fullPath));
                        }
                    }

                    NavigationCycleCompleted?.Invoke(fromVM, _currentVM);
                });
            }

            return true;
        }

        /// <summary>
        /// Navigates for result.
        /// </summary>
        /// <typeparam name="TModule">The type of the module.</typeparam>
        /// <typeparam name="TView">The type of the view.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="pushToHistory">if set to <c>true</c> [push to history].</param>
        /// <returns></returns>
        public Task<TResult> NavigateForResult<TModule, TView, TResult, TObject>(TObject obj, bool pushToHistory = true)
               where TModule : IStudioModule
        {
            TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult>();

            var fromVM = _currentVM;
            Object toVM = null;


            Action<Object, Object> handler = null;

            handler = (from, to) =>
            {
                if (toVM == null)
                {
                    toVM = to;
                    if (toVM is INavigationResultProvider<TResult, TObject>)
                    {
                        (toVM as INavigationResultProvider<TResult, TObject>).OnNavigationObjectReceived(obj);
                    }
                }
                else
                {
                    if (to == fromVM && from == toVM)
                    {
                        if (from is INavigationResultProvider<TResult, TObject>)
                        {
                            source.SetResult((from as INavigationResultProvider<TResult, TObject>).GetNavigationResult());
                        }
                    }

                    NavigationCycleCompleted -= handler;
                }
            };

            NavigationCycleCompleted += handler;

            NavigateTo<TModule>(typeof(TView).Name, pushToHistory);

            return source.Task;
        }

        /// <summary>
        /// Navigates to the specified module and view with the specified object.
        /// </summary>
        /// <typeparam name="TModule">The type of the module.</typeparam>
        /// <typeparam name="TView">The type of the view.</typeparam>
        /// <typeparam name="TPass">The type of the pass.</typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="pushToHistory">if set to <c>true</c> [push to history].</param>
        /// <returns></returns>
        public Task<bool> NavigateWithObject<TModule, TView, TPass>(TPass obj, bool pushToHistory = true) where TModule : IStudioModule
        {
            TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();

            Action<Object, Object> handler = null;

            handler = (from, to) =>
            {
                if (to is INavigationObjectReceiver<TPass>)
                {
                    (to as INavigationObjectReceiver<TPass>).OnNavigatedToWithObject(obj);
                }

                NavigationCycleCompleted -= handler;
            };

            NavigationCycleCompleted += handler;

            NavigateTo<TModule>(typeof(TView).Name, pushToHistory);

            return source.Task;
        }

        private Task<bool> NavigateTo(Type moduleType, bool pushToHistory = true, params String[] viewPath)
        {
            if (viewPath != null && viewPath.Length > 0)
            {
                return NavigateTo(moduleType.Name + "." + String.Join(".", viewPath), pushToHistory);
            }
            else
            {
                return NavigateTo(moduleType.Name, pushToHistory);
            }
        }

        /// <summary>
        /// Gets a value indicating whether the navigation system is able to navigate to the previous view.
        /// </summary>
        public bool CanNavigateBack
        {
            get { return _navigationHistory.Count > 0; }
        }

        /// <summary>
        /// Navigates to the previous view if <see cref="P:Tango.PPC.Common.Navigation.INavigationManager.CanNavigateBack" /> is true.
        /// </summary>
        public async Task<bool> NavigateBack()
        {
            LogManager.Log("Navigating back...");

            _navigating_back = true;

            String first = _navigationHistory.Pop();
            _preventHistory = true;


            if (await NavigateTo(first))
            {
                RaisePropertyChanged(nameof(CanNavigateBack));
                _preventHistory = false;
                _navigating_back = false;
                return true;
            }
            else
            {
                _navigationHistory.Push(first);
                _preventHistory = false;
                _navigating_back = false;
                RaisePropertyChanged(nameof(CanNavigateBack));
                return false;
            }
        }

        /// <summary>
        /// Clears the navigation back history.
        /// </summary>
        public void ClearHistory()
        {
            LogManager.Log("Navigation history cleared.");
            _navigationHistory.Clear();
            RaisePropertyChanged(nameof(CanNavigateBack));
        }

        /// <summary>
        /// Clears the navigation back history except the specified view type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void ClearHistoryExcept<T>()
        {
            LogManager.Log($"Navigation history cleared except for {typeof(T).Name}.");

            var history_list = _navigationHistory.ToList();
            history_list = history_list.Where(x => x.Contains(typeof(T).Name)).Distinct().ToList();
            _navigationHistory.Clear();

            foreach (var item in history_list)
            {
                _navigationHistory.Push(item);
            }

            RaisePropertyChanged(nameof(CanNavigateBack));
        }

        public void NavigateToModule<T>() where T : IStudioModule
        {
            var loader = TangoIOC.Default.GetInstance<IStudioModuleLoader>();
            var module = loader.UserModules.SingleOrDefault(x => x.GetType() == typeof(T));

            if (module != null)
            {
                TangoIOC.Default.GetInstance<ViewModels.MainViewVM>().StartModule(module);
            }
        }
    }
}
f='#n2473'>2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using Tango.Core.Commands;
using Tango.BL.Entities;
using Tango.Integration.Operation;
using Tango.Integration.ExternalBridge;
using Tango.Logging;
using Tango.MachineStudio.Common.Authentication;
using Tango.MachineStudio.Common.Controls;
using Tango.MachineStudio.Common.Diagnostics;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Common.StudioApplication;
using Tango.MachineStudio.Common.Video;
using Tango.MachineStudio.Developer.Navigation;
using Tango.MachineStudio.Developer.Views;
using Tango.Settings;
using Tango.SharedUI;
using Tango.Transport;
using Tango.BL;
using Microsoft.Win32;
using Tango.PMR.Embroidery;
using Tango.EmbroideryUI;
using System.IO;
using System.Windows;
using Tango.Core.Helpers;
using System.Speech.Synthesis;
using System.Media;
using Tango.MachineStudio.Common.EventLogging;
using Tango.MachineStudio.Common.Speech;
using System.Threading;
using Tango.SharedUI.Helpers;
using Tango.Core.DI;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Logging.ViewModels;
using Tango.MachineStudio.Logging.Views;
using Tango.AutoComplete.Editors;
using System.Data.Entity;
using System.Runtime.ExceptionServices;
using Tango.BL.Builders;
using Tango.MachineStudio.Common.Navigation;
using System.Diagnostics;
using Tango.Core.ExtensionMethods;
using Tango.ColorConversion;
using Tango.PMR.Exports;
using Microsoft.WindowsAPICodePack.Dialogs;
using Tango.BL.Enumerations;
using Tango.BL.DTO;
using Tango.BL.ActionLogs;

namespace Tango.MachineStudio.Developer.ViewModels
{
    /// <summary>
    /// Represents the developer module main view, view model.
    /// </summary>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    [TangoCreateWhenRegistered]
    public class MainViewVM : StudioViewModel
    {
        private static object _syncLock = new object();
        private const string EMB_FORMATS_EXPORT = "Baby Lock (PES)|*.pes|Tajima (DST)|*.dst|EXP|*.exp|PCS|*.pcs|HUS|*.hus|KSM|*.ksm";
        private const string EMB_FORMATS_IMPORT = "Embroidery Files|*.pes;*.hus;*.dst";

        private INotificationProvider _notification;
        private TimeSpan _runningJobEstimatedDuration;
        private DeveloperNavigationManager _navigation;
        private INavigationManager _msNavigation;
        private bool _blockInvalidateCommands;
        private ObservablesContext _machineDbContext;
        private ObservablesContext _activeJobDbContext;
        private IEventLogger _eventLogger;
        private ISpeechProvider _speech;
        private DataCapture.ViewModels.MainViewVM _dataCaptureVM;
        private bool _isRecording;
        private DeveloperModuleSettings _settings;
        private Thread _colorConversionThread;
        private bool _hiveOpened;
        private bool _color_changed_from_hive;
        private bool _dialog_shown;
        private bool _disable_gamut_check;
        private bool _rml_has_no_cct;
        private TaskItem _preparingTaskItem;
        private IColorConverter _converter;
        private string _current_job_string;
        private JobDTO _beforeSaveJobDTO;
        private IActionLogManager _actionLogManager;
        private RmlDTO _selectedRMLBeforeLiquidFactorsSaves;
        private List<Cct> _cctCache;

        #region Properties

        private List<ColorConversionSuggestion> _hiveSuggestions;
        /// <summary>
        /// Gets or sets the hive suggestions.
        /// </summary>
        public List<ColorConversionSuggestion> HiveSuggestions
        {
            get { return _hiveSuggestions; }
            set { _hiveSuggestions = value; RaisePropertyChangedAuto(); }
        }

        private ColorConversionSuggestion _selectedSuggestion;
        /// <summary>
        /// Gets or sets the selected suggestion.
        /// </summary>
        public ColorConversionSuggestion SelectedSuggestion
        {
            get { return _selectedSuggestion; }
            set { _selectedSuggestion = value; RaisePropertyChangedAuto(); OnSelectedSuggestionChanged(); }
        }

        private RunningJobStatus _runningJobStatus;
        /// <summary>
        /// Gets or sets the running job status.
        /// </summary>
        public RunningJobStatus RunningJobStatus
        {
            get { return _runningJobStatus; }
            set { _runningJobStatus = value; RaisePropertyChangedAuto(); }
        }

        private JobHandler _jobHandler;
        /// <summary>
        /// Gets or sets the current running job handler.
        /// </summary>
        public JobHandler JobHandler
        {
            get
            {
                return _jobHandler;
            }
            set
            {
                _jobHandler = value; RaisePropertyChangedAuto();
            }
        }

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

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

        private ObservableCollection<WindingMethod> _windingMethods;
        /// <summary>
        /// Gets or sets the winding methods.
        /// </summary>
        public ObservableCollection<WindingMethod> WindingMethods
        {
            get { return _windingMethods; }
            set { _windingMethods = value; RaisePropertyChangedAuto(); }
        }

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

        /// <summary>
        /// Gets or sets the application manager.
        /// </summary>
        public IStudioApplicationManager ApplicationManager { get; set; }

        /// <summary>
        /// Gets or sets the video capture provider.
        /// </summary>
        public IVideoCaptureProvider VideoCaptureProvider { get; set; }

        protected Machine _selectedMachine;
        /// <summary>
        /// Gets or sets the selected machine.
        /// </summary>
        public Machine SelectedMachine
        {
            get { return _selectedMachine; }
            set
            {
                if (value != null && _selectedMachine != value)
                {
                    _selectedMachine = value;
                    OnSelectedMachineChanged();
                    RaisePropertyChangedAuto();
                    InvalidateRelayCommands();

                    if (_selectedMachine != null)
                    {
                        _selectedMachine.Modified -= SelectedMachine_Modified;
                        _selectedMachine.Modified += SelectedMachine_Modified;
                    }
                }
            }
        }

        private bool _canWork;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is loading machine.
        /// </summary>
        public bool CanWork
        {
            get { return _canWork; }
            set { _canWork = value; RaisePropertyChangedAuto(); }
        }

        private List<LiquidTypesRml> _liquidTypesRmls;
        /// <summary>
        /// Gets or sets the liquid types RMLS.
        /// </summary>
        public List<LiquidTypesRml> LiquidTypesRmls
        {
            get { return _liquidTypesRmls; }
            set { _liquidTypesRmls = value; RaisePropertyChangedAuto(); }
        }

        private ProcessParametersTablesGroup _rmlProcessParametersTablesGroup;
        /// <summary>
        /// Gets or sets the RML process parameters table group (cloned).
        /// </summary>
        public ProcessParametersTablesGroup RmlProcessParametersTableGroup
        {
            get { return _rmlProcessParametersTablesGroup; }
            set
            { _rmlProcessParametersTablesGroup = value; RaisePropertyChangedAuto(); OnProcessParametersTableGroupChanged(); }
        }

        private ObservableCollection<ProcessParametersTablesGroup> _groupsHistory;
        /// <summary>
        /// Gets or sets the RML process parameters groups history.
        /// </summary>
        public ObservableCollection<ProcessParametersTablesGroup> GroupsHistory
        {
            get { return _groupsHistory; }
            set { _groupsHistory = value; RaisePropertyChangedAuto(); }
        }

        private ProcessParametersTablesGroup _selectedGroupHistory;
        /// <summary>
        /// Gets or sets the selected process parameters tables group history.
        /// </summary>
        public ProcessParametersTablesGroup SelectedGroupHistory
        {
            get { return _selectedGroupHistory; }
            set { _selectedGroupHistory = value; RaisePropertyChangedAuto(); OnSelectedGroupHistoryChanged(); }
        }

        private ProcessParametersTable _selectedProcessParametersTable;
        /// <summary>
        /// Gets or sets the selected process parameters table.
        /// </summary>
        public ProcessParametersTable SelectedProcessParametersTable
        {
            get { return _selectedProcessParametersTable; }
            set { _selectedProcessParametersTable = value; RaisePropertyChangedAuto(); OnSelectedParametersTableChanged(); }
        }

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

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

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

        private Segment _selectedSegment;
        /// <summary>
        /// Gets or sets the job selected segment.
        /// </summary>
        public Segment SelectedSegment
        {
            get { return _selectedSegment; }
            set { _selectedSegment = value; RaisePropertyChangedAuto(); OnSelectedSegmentChanged(); }
        }

        private ObservableCollection<Segment> _selectedSegments;
        /// <summary>
        /// Gets or sets the selected segments.
        /// </summary>
        public ObservableCollection<Segment> SelectedSegments
        {
            get { return _selectedSegments; }
            set { _selectedSegments = value; RaisePropertyChangedAuto(); }
        }

        private BrushStop _selectedBrushStop;
        /// <summary>
        /// Gets or sets the selected segment selected brush stop.
        /// </summary>
        public BrushStop SelectedBrushStop
        {
            get { return _selectedBrushStop; }
            set { _selectedBrushStop = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<BrushStop> _selectedBrushStops;
        /// <summary>
        /// Gets or sets the selected brush stops.
        /// </summary>
        public ObservableCollection<BrushStop> SelectedBrushStops
        {
            get { return _selectedBrushStops; }
            set { _selectedBrushStops = value; RaisePropertyChangedAuto(); }
        }

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

        private bool _isSideBarOpened;
        /// <summary>
        /// Gets or sets a value indicating whether the configuration panels are opened.
        /// </summary>
        public bool IsSideBarOpened
        {
            get { return _isSideBarOpened; }
            set { _isSideBarOpened = value; RaisePropertyChangedAuto(); }
        }

        private TimeSpan _estimatedDuration;
        /// <summary>
        /// Gets or sets the estimated duration for the selected job.
        /// </summary>
        public TimeSpan EstimatedDuration
        {
            get { return _estimatedDuration; }
            set { _estimatedDuration = value; RaisePropertyChangedAuto(); }
        }

        private bool _isJobRunning;
        /// <summary>
        /// Gets or sets a value indicating whether a job is currently running.
        /// </summary>
        public bool IsJobRunning
        {
            get { return _isJobRunning; }
            set { _isJobRunning = value; RaisePropertyChangedAuto(); }
        }

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

        private bool _isJobCompleted;
        /// <summary>
        /// Gets or sets a value indicating whether the running job has completed successfully.
        /// </summary>
        public bool IsJobCompleted
        {
            get { return _isJobCompleted; }
            set { _isJobCompleted = value; RaisePropertyChangedAuto(); }
        }

        private bool _isJobFailed;
        /// <summary>
        /// Gets or sets a value indicating whether the running job has failed.
        /// </summary>
        public bool IsJobFailed
        {
            get { return _isJobFailed; }
            set { _isJobFailed = value; RaisePropertyChangedAuto(); }
        }

        private bool _showJobStatus;
        /// <summary>
        /// Gets or sets a value indicating whether to show all the relevant job status areas.
        /// </summary>
        public bool ShowJobStatus
        {
            get { return _showJobStatus; }
            set { _showJobStatus = value; RaisePropertyChangedAuto(); }
        }

        private bool _isJobCanceled;
        /// <summary>
        /// Gets or sets a value indicating whether the last running job was canceled.
        /// </summary>
        public bool IsJobCanceled
        {
            get { return _isJobCanceled; }
            set { _isJobCanceled = value; RaisePropertyChangedAuto(); }
        }

        private IMachineOperator _machineOperator;
        /// <summary>
        /// Gets or sets the machine operator.
        /// </summary>
        public IMachineOperator MachineOperator
        {
            get { return _machineOperator; }
            set { _machineOperator = value; RaisePropertyChangedAuto(); }
        }

        private List<Segment> _runningJobSegments;
        /// <summary>
        /// Gets or sets the running job segments.
        /// </summary>
        public List<Segment> RunningJobSegments
        {
            get { return _runningJobSegments; }
            set { _runningJobSegments = value; RaisePropertyChangedAuto(); }
        }

        private ICollectionView _jobsCollectionView;
        /// <summary>
        /// Gets or sets the jobs collection view.
        /// </summary>
        public ICollectionView JobsCollectionView
        {
            get { return _jobsCollectionView; }
            set
            {
                _jobsCollectionView = value;
                BindingOperations.EnableCollectionSynchronization(_jobsCollectionView, _syncLock);

                RaisePropertyChangedAuto();
            }
        }

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

        private ICollectionView _brushStopsCollectionView;
        /// <summary>
        /// Gets or sets the brush stops collection view.
        /// </summary>
        public ICollectionView BrushStopsCollectionView
        {
            get { return _brushStopsCollectionView; }
            set
            {
                _brushStopsCollectionView = value;
                BindingOperations.EnableCollectionSynchronization(_brushStopsCollectionView, _syncLock);
                RaisePropertyChangedAuto();
            }
        }

        private String _jobFilter;
        /// <summary>
        /// Gets or sets the job filter.
        /// </summary>
        public String JobFilter
        {
            get { return _jobFilter; }
            set { _jobFilter = value; RaisePropertyChangedAuto(); OnJobFilterChanged(); }
        }

        private ObservableCollection<MachinesEvent> _jobEvents;
        /// <summary>
        /// Gets or sets the running job events.
        /// </summary>
        public ObservableCollection<MachinesEvent> JobEvents
        {
            get { return _jobEvents; }
            set { _jobEvents = value; RaisePropertyChangedAuto(); }
        }

        private MachinesEvent _selectedJobEvent;
        /// <summary>
        /// Gets or sets the selected job event.
        /// </summary>
        public MachinesEvent SelectedJobEvent
        {
            get { return _selectedJobEvent; }
            set { _selectedJobEvent = value; RaisePropertyChangedAuto(); OnSelectedJobEventChanged(); }
        }

        /// <summary>
        /// Gets or sets the machines providers.
        /// </summary>
        public ISuggestionProvider MachinesProvider { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the job details view is visible.
        /// </summary>
        public bool IsJobVisible { get; set; }

        private bool _enableColorConversion;
        /// <summary>
        /// Gets or sets a value indicating whether to enable color conversion processes.
        /// </summary>
        public bool EnableColorConversion
        {
            get { return _enableColorConversion; }
            set { _enableColorConversion = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets the authentication provider.
        /// </summary>
        public IAuthenticationProvider AuthenticationProvider { get; set; }

        /// <summary>
        /// Gets or sets the module settings.
        /// </summary>
        public DeveloperModuleSettings Settings
        {
            get { return _settings; }
            set { _settings = value; RaisePropertyChangedAuto(); }
        }

        private bool _autoProcessSelection;
        /// <summary>
        /// Gets or sets a value indicating whether [automatic process selection].
        /// </summary>
        public bool AutoProcessSelection
        {
            get { return _autoProcessSelection; }
            set
            {
                _autoProcessSelection = value;
                RaisePropertyChangedAuto();
                Settings.AutoProcessSelection = _autoProcessSelection;
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the edit machine command.
        /// </summary>
        public RelayCommand EditMachineCommand { get; set; }

        /// <summary>
        /// Gets or sets the edit RML command.
        /// </summary>
        public RelayCommand EditRMLCommand { get; set; }

        /// <summary>
        /// Gets or sets the toggle side bar command.
        /// </summary>
        public RelayCommand ToggleSideBarCommand { get; set; }

        /// <summary>
        /// Gets or sets the save process parameters command.
        /// </summary>
        public RelayCommand SaveProcessParametersCommand { get; set; }

        /// <summary>
        /// Gets or sets the save liquid factors command.
        /// </summary>
        public RelayCommand SaveLiquidFactorsCommand { get; set; }

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

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

        /// <summary>
        /// Gets or sets the add job command.
        /// </summary>
        public RelayCommand AddJobCommand { get; set; }

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

        /// <summary>
        /// Gets or sets the add brush stop command.
        /// </summary>
        public RelayCommand AddBrushStopCommand { get; set; }

        /// <summary>
        /// Gets or sets the remove brush stop command.
        /// </summary>
        public RelayCommand RemoveBrushStopCommand { get; set; }

        /// <summary>
        /// Gets or sets the save job command.
        /// </summary>
        public RelayCommand SaveJobCommand { get; set; }

        /// <summary>
        /// Gets or sets the discard job command.
        /// </summary>
        public RelayCommand DiscardJobCommand { get; set; }

        /// <summary>
        /// Gets or sets the start job command.
        /// </summary>
        public RelayCommand StartJobCommand { get; set; }

        /// <summary>
        /// Gets or sets the start job and record command.
        /// </summary>
        public RelayCommand StartJobAndRecordCommand { get; set; }

        /// <summary>
        /// Gets or sets the stop job command.
        /// </summary>
        public RelayCommand StopJobCommand { get; set; }

        /// <summary>
        /// Gets or sets the close job completion status command.
        /// </summary>
        public RelayCommand CloseJobCompletionStatusCommand { get; set; }

        /// <summary>
        /// Gets or sets the load job command.
        /// </summary>
        public RelayCommand LoadJobCommand { get; set; }

        /// <summary>
        /// Gets or sets the duplicate job command.
        /// </summary>
        public RelayCommand DuplicateJobCommand { get; set; }

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

        /// <summary>
        /// Gets or sets the duplicate brush stop command.
        /// </summary>
        public RelayCommand DuplicateBrushStopCommand { get; set; }

        /// <summary>
        /// Gets or sets the push process parameters command.
        /// </summary>
        public RelayCommand PushProcessParametersCommand { get; set; }

        /// <summary>
        /// Gets or sets the import embroidery file command.
        /// </summary>
        public RelayCommand ImportEmbroideryFileCommand { get; set; }

        /// <summary>
        /// Gets or sets the display job embroidery file command.
        /// </summary>
        public RelayCommand<Job> DisplayJobEmbroideryFileCommand { get; set; }

        /// <summary>
        /// Gets or sets the reload machines command.
        /// </summary>
        public RelayCommand ReloadMachinesCommand { get; set; }

        /// <summary>
        /// Gets or sets the back to job command.
        /// </summary>
        public RelayCommand BackToJobCommand { get; set; }

        /// <summary>
        /// Gets or sets to running job command.
        /// </summary>
        public RelayCommand ToRunningJobCommand { get; set; }

        /// <summary>
        /// Gets or sets the reset process parameters command.
        /// </summary>
        public RelayCommand ResetProcessParametersCommand { get; set; }

        /// <summary>
        /// Gets or sets the import job file command.
        /// </summary>
        public RelayCommand ImportJobFileCommand { get; set; }

        /// <summary>
        /// Gets or sets the export job file command.
        /// </summary>
        public RelayCommand ExportJobFileCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewVM"/> class.
        /// </summary>
        /// <param name="applicationManager">The application manager.</param>
        /// <param name="notificationProvider">The notification provider.</param>
        public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notificationProvider, IDiagnosticsFrameProvider diagnosticsFrameProvider, IVideoCaptureProvider videoCaptureProvider, DeveloperNavigationManager navigation, INavigationManager navigationManager, IAuthenticationProvider authentication, IEventLogger eventLogger, ISpeechProvider speech, IActionLogManager actionLogManager)
        {
            _converter = new DefaultColorConverter();

            _cctCache = new List<Cct>();

            CanWork = true;
            EnableColorConversion = true;

            AuthenticationProvider = authentication;

            _actionLogManager = actionLogManager;
            _notification = notificationProvider;
            _speech = speech;
            _navigation = navigation;
            _msNavigation = navigationManager;
            ApplicationManager = applicationManager;
            VideoCaptureProvider = videoCaptureProvider;
            _eventLogger = eventLogger;

            LogManager.Log("Initializing relay commands...");

            TangoIOC.Default.GetInstanceWhenAvailable<DataCapture.ViewModels.MainViewVM>((vm) =>
            {
                _dataCaptureVM = vm;
                _dataCaptureVM.RelayCommandsInvalidated += (_, __) => StartJobAndRecordCommand.RaiseCanExecuteChanged();
            });

            //Initialize Commands...
            EditMachineCommand = new RelayCommand(EditMachine, () => SelectedMachine != null && CanWork);
            EditRMLCommand = new RelayCommand(EditRML, () => SelectedRML != null && CanWork);
            ToggleSideBarCommand = new RelayCommand(() => IsSideBarOpened = !IsSideBarOpened);
            SaveLiquidFactorsCommand = new RelayCommand(SaveLiquidFactors, () => SelectedRML != null && CanWork);
            AddSegmentCommand = new RelayCommand(AddSegment, () => ActiveJob != null && CanWork);
            RemoveSegmentCommand = new RelayCommand(RemoveSelectedSegments, () => SelectedSegment != null && CanWork);
            AddJobCommand = new RelayCommand(AddJob, () => SelectedMachine != null && CanWork);
            RemoveJobCommand = new RelayCommand(RemoveSelectedJobs, () => SelectedMachineJob != null && CanWork);
            AddBrushStopCommand = new RelayCommand(AddBrushStop, () => SelectedSegment != null && CanWork);
            RemoveBrushStopCommand = new RelayCommand(RemoveSelectedBrushStops, () => SelectedBrushStop != null && CanWork);
            SaveJobCommand = new RelayCommand(SaveActiveJob, () => SelectedMachine != null && CanWork);
            DiscardJobCommand = new RelayCommand(BackToJobs, () => SelectedMachine != null && CanWork);
            StartJobCommand = new RelayCommand(() => StartJob(), () => ActiveJob != null && CanWork && !IsJobRunning && MachineOperator != null);
            StartJobAndRecordCommand = new RelayCommand(StartJobAndRecord, () => _dataCaptureVM != null && !_dataCaptureVM.Recorder.IsRecording && !_dataCaptureVM.Player.IsPlaying && ActiveJob != null && !IsJobRunning && MachineOperator != null && CanWork);
            StopJobCommand = new RelayCommand(StopJob, () => IsJobRunning && CanWork);
            CloseJobCompletionStatusCommand = new RelayCommand(CloseJobCompletionStatusBar);
            LoadJobCommand = new RelayCommand(() => LoadSelectedJob(), () => SelectedMachineJob != null && CanWork);
            DuplicateJobCommand = new RelayCommand(DuplicateSelectedJobs, () => SelectedMachineJob != null && CanWork);
            DuplicateSegmentCommand = new RelayCommand(DuplicateSelectedSegments, () => SelectedSegment != null && CanWork);
            DuplicateBrushStopCommand = new RelayCommand(DuplicateSelectedBrushStops, () => SelectedBrushStop != null && CanWork);
            SaveProcessParametersCommand = new RelayCommand(SaveProcessParameters, () => SelectedRML != null && CanWork && SelectedRML.ProcessParametersTablesGroups.Count > 0);
            PushProcessParametersCommand = new RelayCommand(PushProcessParameters, () => SelectedRML != null && CanWork && SelectedRML.ProcessParametersTablesGroups.Count > 0 && SelectedProcessParametersTable != null && MachineOperator != null);
            ImportEmbroideryFileCommand = new RelayCommand(ImportEmbroideryFile, () => SelectedMachine != null && CanWork);
            DisplayJobEmbroideryFileCommand = new RelayCommand<Job>(DisplayJobEmbroideryFile, () => CanWork);
            ReloadMachinesCommand = new RelayCommand(() => LoadMachine(), () => CanWork && SelectedMachine != null);
            ResetProcessParametersCommand = new RelayCommand(ResetProcessParameters, () => CanWork && MachineOperator != null);
            ImportJobFileCommand = new RelayCommand(ImportJobFile, () => SelectedMachine != null && CanWork);
            ExportJobFileCommand = new RelayCommand(ExportJobFile, () => SelectedMachine != null && SelectedMachineJob != null && CanWork);

            ApplicationManager.ConnectedMachineChanged += ApplicationManager_ConnectedMachineChanged;

            _eventLogger.NewLog += _eventLogger_NewLog;


            MachinesProvider = new SuggestionProvider((filter) =>
            {
                try
                {
                    return _machineDbContext.Machines.Where(x => x.SerialNumber.StartsWith(filter)).ToList();
                }
                catch
                {
                    return null;
                }
            });

            BackToJobCommand = new RelayCommand(BackToJob);
            ToRunningJobCommand = new RelayCommand(ToRunningJob);
        }

        #endregion

        #region Application Ready

        public override void OnApplicationReady()
        {
            Settings = SettingsManager.Default.GetOrCreate<DeveloperModuleSettings>();

            AutoProcessSelection = Settings.AutoProcessSelection;

            SelectedJobs = new ObservableCollection<Job>();
            JobEvents = new ObservableCollection<MachinesEvent>();

            LogManager.Log("Initializing machine Db context...");
            _machineDbContext = ObservablesContext.CreateDefault();

            if (_settings.LastSelectedMachineGuid != null)
            {
                LogManager.Log("Setting last selected machine from settings...");
                SelectedMachine = _machineDbContext.Machines.SingleOrDefault(x => x.Guid == _settings.LastSelectedMachineGuid);
            }

            if (_settings.LastSelectedJobGuid != null && SelectedMachine != null)
            {
                LogManager.Log("Setting last selected job from settings...");
                SelectedMachineJob = SelectedMachine.Jobs.SingleOrDefault(x => x.Guid == _settings.LastSelectedJobGuid);
            }

            _colorConversionThread = new Thread(ColorConversionThreadMethod);
            _colorConversionThread.IsBackground = true;
            _colorConversionThread.Start();
        }

        #endregion

        #region Color Conversion

        [HandleProcessCorruptedStateExceptions]
        private void ColorConversionThreadMethod()
        {
            while (true)
            {
                if (!_rml_has_no_cct && EnableColorConversion && !_disable_gamut_check && IsJobVisible && IsVisible && ActiveJob != null && ActiveJob.Segments != null && SelectedProcessParametersTable != null)
                {
                    try
                    {
                        var stops = ActiveJob.Segments.SelectMany(x => x.BrushStops).Where(x => !x.Corrected && !x.OutOfGamutChecked).ToList();

                        foreach (var stop in stops)
                        {
                            if (stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.Volume.ToInt32())
                            {
                                try
                                {
                                    var output = _converter.Convert(stop, false, true);

                                    stop.Red = output.SingleCoordinates.Red;
                                    stop.Green = output.SingleCoordinates.Green;
                                    stop.Blue = output.SingleCoordinates.Blue;
                                    stop.Corrected = true;
                                    stop.IsOutOfGamut = false;
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine(ex.ToString());
                                }
                            }
                            else if (stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.RGB.ToInt32() || stop.ColorSpace.Code == BL.Enumerations.ColorSpaces.LAB.ToInt32())
                            {
                                try
                                {
                                    var output = _converter.Convert(stop, false, true);
                                    output.ApplyOnBrushStopLiquidVolumes(stop, SelectedProcessParametersTable);
                                    stop.OutOfGamutChecked = true;
                                }
                                catch { }
                            }
                        }
                    }
                    catch { }
                }

                if (AutoProcessSelection && IsJobVisible && IsVisible && ActiveJob != null && ActiveJob.Segments != null && !_rml_has_no_cct && !_disable_gamut_check)
                {
                    try
                    {
                        var recommendedProcess = _converter.GetRecommendedProcessParameters(ActiveJob, RmlProcessParametersTableGroup);

                        if (recommendedProcess != null && recommendedProcess != SelectedProcessParametersTable)
                        {
                            SelectedProcessParametersTable = recommendedProcess;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error resolving recommended process parameters.");
                    }
                }

                Thread.Sleep(500);
            }
        }

        public void OnHivePopupOpened()
        {
            if (SelectedBrushStop != null)
            {
                _hiveOpened = true;
                try
                {
                    HiveSuggestions = _converter.Convert(SelectedBrushStop, true, true).CreateHiveSuggestions();
                }
                catch (Exception ex)
                {
                    _hiveOpened = false;
                    LogManager.Log(ex);
                    _notification.ShowError($"Error occurred while trying to convert the source color.\n{ex.Message}");
                }
            }
        }

        private void OnSelectedSuggestionChanged()
        {
            if (SelectedSuggestion != null && SelectedBrushStop != null && _hiveOpened)
            {
                _color_changed_from_hive = true;
                SelectedBrushStop.Color = SelectedSuggestion.Color;
                SelectedBrushStop.Corrected = true;
                SelectedBrushStop.IsOutOfGamut = false;

                var coords = SelectedSuggestion.Coordinates;

                foreach (var liquid in coords.OutputLiquids)
                {
                    var liquidVolume = SelectedBrushStop.LiquidVolumes.SingleOrDefault(x => x.IdsPack.LiquidType.Code == liquid.LiquidType.ToInt32());

                    if (liquidVolume != null)
                    {
                        liquidVolume.Volume = liquid.Volume;
                    }
                }

                _color_changed_from_hive = false;
            }
        }

        public void OnSelectedBrushColorChanged(Color color)
        {
            if (!_color_changed_from_hive && _hiveOpened)
            {
                SelectedBrushStop.Corrected = false;
                HiveSuggestions = _converter.Convert(SelectedBrushStop, true, true).CreateHiveSuggestions();
            }
        }

        public void OnHivePopupClosed()
        {
            _hiveOpened = false;
        }

        /// <summary>
        /// Called when the brush stop field value has been changed (This called from the view!).
        /// </summary>
        /// <param name="brushStop">The brush stop.</param>
        public void OnBrushStopFieldValueChanged(BrushStop brushStop)
        {
            brushStop.Corrected = false;
            brushStop.OutOfGamutChecked = false;
        }

        #endregion

        #region Event Handlers

        private void _eventLogger_NewLog(object sender, MachinesEvent e)
        {
            if (IsJobRunning)
            {
                InvokeUI(() =>
                {
                    JobEvents.Insert(0, e);
                });
            }
        }

        /// <summary>
        /// Handles the application manager connected machine changes event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="machine">The machine.</param>
        private void ApplicationManager_ConnectedMachineChanged(object sender, IExternalBridgeClient machine)
        {
            MachineOperator = machine;

            if (MachineOperator != null)
            {
                MachineOperator.MachineEventsStateProvider.EventsChanged -= MachineEventsStateProvider_EventsChanged;
                MachineOperator.MachineEventsStateProvider.EventsChanged += MachineEventsStateProvider_EventsChanged;

                MachineOperator.MachineEventsStateProvider.NewEvents -= MachineEventsStateProvider_NewEvents;
                MachineOperator.MachineEventsStateProvider.NewEvents += MachineEventsStateProvider_NewEvents;

                MachineOperator.ResumingJob -= MachineOperator_ResumingJob;
                MachineOperator.ResumingJob += MachineOperator_ResumingJob;

                MachineOperator.PreparingJobProgress -= MachineOperator_PreparingJobProgress;
                MachineOperator.PreparingJobProgress += MachineOperator_PreparingJobProgress;
            }
        }

        private void MachineOperator_PreparingJobProgress(object sender, PreparingJobProgressEventArgs e)
        {
            var percent = (e.Progress / e.Total * 100d);

            if (_preparingTaskItem != null)
            {
                _preparingTaskItem.Message = $"Preparing job for printing {(e.Progress / e.Total * 100d).ToString("0.0")}%...";
            }

            if (_preparingTaskItem == null && percent == 0)
            {
                _preparingTaskItem = _notification.PushTaskItem("Preparing job for printing...");
            }
            else if (percent == 100 && _preparingTaskItem != null)
            {
                _preparingTaskItem.Pop();
                _preparingTaskItem = null;
            }
        }

        private void MachineOperator_ResumingJob(object sender, ResumingJobEventArgs e)
        {
            if (_notification.ShowQuestion("Machine studio has detected a job in progress. Would you like to try and continue from there you were?"))
            {
                var job = _machineDbContext.Jobs.SingleOrDefault(x => x.Guid == e.JobGuid);

                if (job != null)
                {
                    _msNavigation.NavigateToModule<DeveloperModule>();
                    SelectedMachine = _machineDbContext.Machines.SingleOrDefault(x => x.Guid == job.MachineGuid);
                    SelectedMachineJob = SelectedMachine.Jobs.SingleOrDefault(x => x.Guid == job.Guid);
                    LoadSelectedJob(() =>
                    {
                        StartJob(e.Approve);
                    });
                }
                else
                {
                    LogManager.Log($"Could not resume job. The running job with guid '{e.JobGuid}' was not found.");
                    _notification.ShowError("Could not resume job. The running job was not found.");
                }
            }
        }

        private void MachineEventsStateProvider_NewEvents(object sender, IEnumerable<MachinesEvent> events)
        {
            HandleNewHardwareEvents(events);
        }

        private void MachineEventsStateProvider_EventsChanged(object sender, IEnumerable<MachinesEvent> changedEvents)
        {
            InvokeUI(StartJobCommand.RaiseCanExecuteChanged);
            InvokeUI(StartJobAndRecordCommand.RaiseCanExecuteChanged);
        }

        /// <summary>
        /// Handles the Saved event of the SelectedMachine.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void SelectedMachine_Modified(object sender, ObservableModifiedEventArgs e)
        {
            if (e.IsOtherContext)
            {
                InvokeUI(() =>
                {
                    SelectedMachine.Reload(_machineDbContext);
                    InvalidateLiquidFactorsAndProcessTables();

                    if (SelectedSegment != null)
                    {
                        OnSelectedSegmentChanged();
                    }
                });
            }
        }

        /// <summary>
        /// Handles the LengthChanged event of the SelectedJob.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ActiveJob_LengthChanged(object sender, EventArgs e)
        {
            UpdateEstimatedDuration();
        }

        /// <summary>
        /// Handles the DyeingSpeedMinInkUptakeChanged event of the SelectedProcessParametersTable.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void SelectedProcessParametersTable_DyeingSpeedMinInkUptakeChanged(object sender, EventArgs e)
        {
            if (SelectedSegment != null)
            {
                foreach (var liquidVolume in SelectedSegment.BrushStops.SelectMany(x => x.LiquidVolumes))
                {
                    liquidVolume.Invalidate();
                }
            }

            UpdateEstimatedDuration();
        }

        #endregion

        #region Hardware Events

        private void HandleNewHardwareEvents(IEnumerable<MachinesEvent> events)
        {
            if (IsJobRunning)
            {
                _speech.SpeakError(events.Last().EventType.Name);

                //if (events.ToList().Exists(x => x.Actions.Contains(BL.Enumerations.EventTypeActions.StopJob)))
                //{
                //    if (JobHandler != null)
                //    {
                //        InvokeUI(StopJob);
                //    }
                //}
            }
        }

        #endregion

        #region Properties Changes

        /// <summary>
        /// Called when the selected parameters table has changed.
        /// </summary>
        protected virtual void OnSelectedParametersTableChanged()
        {
            if (SelectedProcessParametersTable != null)
            {
                LogManager.Log("Selected process parameters table changed.");
                SelectedProcessParametersTable.DyeingSpeedMinInkUptakeChanged -= SelectedProcessParametersTable_DyeingSpeedMinInkUptakeChanged;
                SelectedProcessParametersTable.DyeingSpeedMinInkUptakeChanged += SelectedProcessParametersTable_DyeingSpeedMinInkUptakeChanged;

                foreach (var segment in ActiveJob.Segments)
                {
                    SetSegmentBrushStopsLiquidVolumes(segment);
                }

                UpdateEstimatedDuration();
            }
        }

        /// <summary>
        /// Called when the process parameters table group has been changed
        /// </summary>
        protected virtual void OnProcessParametersTableGroupChanged()
        {
            if (RmlProcessParametersTableGroup != null && RmlProcessParametersTableGroup.ProcessParametersTables.Count > 0)
            {
                LogManager.Log("Process parameters group changed...");

                InvokeUI(() =>
                {
                    SelectedProcessParametersTable = RmlProcessParametersTableGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).FirstOrDefault();
                    UpdateEstimatedDuration();
                });
            }
        }

        /// <summary>
        /// Called when the selected segment has been changed
        /// </summary>
        protected virtual void OnSelectedSegmentChanged()
        {
            if (SelectedSegment != null)
            {
                LogManager.Log("Selected segment changed...");
                SetSegmentBrushStopsLiquidVolumes(SelectedSegment);
                SelectedBrushStop = SelectedSegment.BrushStops.FirstOrDefault();

                BrushStopsCollectionView = CollectionViewSource.GetDefaultView(SelectedSegment.BrushStops);
                BrushStopsCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrushStop.StopIndex), ListSortDirection.Ascending));
            }
        }

        /// <summary>
        /// Called when the selected group history has been changed
        /// </summary>
        protected virtual void OnSelectedGroupHistoryChanged()
        {
            if (SelectedGroupHistory != null)
            {
                LogManager.Log(String.Format("Parameters group {0} selected from history.", SelectedGroupHistory.Name));
                RmlProcessParametersTableGroup = SelectedGroupHistory.Clone();
            }
        }

        /// <summary>
        /// Called when the machine has been changed
        /// </summary>
        protected virtual void OnSelectedMachineChanged()
        {
            if (SelectedMachine != null)
            {
                LogManager.Log(String.Format("Machine {0} changed.", SelectedMachine.SerialNumber));
                LoadMachine();
            }
        }

        /// <summary>
        /// Called when the job filtering has changed.
        /// </summary>
        protected virtual void OnJobFilterChanged()
        {
            String filter = JobFilter.ToLower();

            if (JobsCollectionView == null)
                return;

            JobsCollectionView.Filter = (job) =>
            {
                try
                {
                    Job j = job as Job;
                    return String.IsNullOrWhiteSpace(filter)
                    ||
                    j.Name.ToLower().Contains(filter) //Job name
                    ||
                    (j.User != null && j.User.Contact.FirstName.ToLower().Contains(filter)) // User first name
                    ||
                    j.Length.ToString().Contains(filter); //Job length
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error filtering jobs.");
                    return true;
                }
            };
        }

        #endregion

        #region Drag & Drop

        /// <summary>
        /// Switch the segment position in the job.
        /// </summary>
        /// <param name="dragged">The dragged.</param>
        /// <param name="dropped">The dropped.</param>
        public void OnDropSegment(Segment dragged, Segment dropped)
        {
            LogManager.Log(String.Format("Segment {0} Dropped on segment {1}", dragged.SegmentIndex, dropped.SegmentIndex));

            dragged.SegmentIndex = dropped.SegmentIndex;
            dropped.SegmentIndex++;

            int index = 1;

            foreach (var segment in ActiveJob.Segments.OrderBy(x => x.SegmentIndex))
            {
                segment.SegmentIndex = index++;
            }

            SegmentsCollectionView.Refresh();
        }

        /// <summary>
        /// Switch the brush stop position in the segment.
        /// </summary>
        /// <param name="dragged">The dragged stop.</param>
        /// <param name="dropped">The dropped stop.</param>
        public void OnDropBrushStop(BrushStop dragged, BrushStop dropped)
        {
            LogManager.Log(String.Format("BrushStop {0} Dropped on BrushStop {1}", dragged.StopIndex, dropped.StopIndex));

            dragged.SetStopIndexNoRaise(dropped.StopIndex);
            dropped.SetStopIndexNoRaise(dropped.StopIndex + 1);
            ArrangeBrushStopsIndices();
        }

        #endregion

        #region Running Job Management

        private void OnSelectedJobEventChanged()
        {
            if (SelectedJobEvent != null && SelectedJobEvent.Type != BL.Enumerations.EventTypes.APPLICATION_STARTED && !_dialog_shown)
            {
                _dialog_shown = true;
                _notification.ShowModalDialog<EventDetailsViewVM, EventDetailsView>(new EventDetailsViewVM(SelectedJobEvent), (x) =>
                {

                }, () =>
                {
                    _dialog_shown = false;
                });
            }
        }

        /// <summary>
        /// Closes the job completion status bar.
        /// </summary>
        private void CloseJobCompletionStatusBar()
        {
            LogManager.Log("Closing job completion status bar...");
            _navigation.NavigateTo(DeveloperNavigationView.JobView);
            IsJobCompleted = false;
            IsJobFailed = false;
            IsJobCanceled = false;
            ShowJobStatus = false;
            RunningJob = null;
        }

        /// <summary>
        /// Stops the job.
        /// </summary>
        private void StopJob()
        {
            LogManager.Log("Stopping job...");
            IsJobRunning = false;
            IsJobCanceled = true;
            JobHandler.Cancel();
        }

        /// <summary>
        /// Fails the job.
        /// </summary>
        private void SetJobFailed()
        {
            if (IsJobRunning)
            {
                LogManager.Log("Setting job failed state...");
                IsJobRunning = false;
                IsJobFailed = true;

                _speech.SpeakError("Job Failed!");
            }
        }

        /// <summary>
        /// Completes the job.
        /// </summary>
        private void SetJobCompleted()
        {
            LogManager.Log("Setting job completed state...");
            IsJobRunning = false;
            IsJobCompleted = true;
            _speech.SpeakInfo("Job Completed!");
        }

        /// <summary>
        /// Starts the job.
        /// </summary>
        private async void StartJob(Func<JobHandler> resumeFunc = null)
        {
            SettingsManager.Default.Save();

            LogManager.Log(String.Format("Starting job {0}...", ActiveJob.Name));
            if (MachineOperator == null || MachineOperator.State != TransportComponentState.Connected)
            {
                _notification.ShowError("No machine connected. Could not execute the specified job.");
                return;
            }

            if (SelectedProcessParametersTable == null)
            {
                _notification.ShowError("No process parameters table selected. Could not execute the specified job.");
                return;
            }

            foreach (var stop in ActiveJob.Segments.SelectMany(x => x.BrushStops).Where(x => x.LiquidVolumes == null))
            {
                stop.SetLiquidVolumes(SelectedMachine.Configuration, SelectedRML, SelectedProcessParametersTable);
            }

            if (AutoProcessSelection)
            {
                LogManager.Log("Auto process parameters selection enabled. Trying to resolve the recommended process parameters...");
                try
                {
                    var recommendedProcess = _converter.GetRecommendedProcessParameters(ActiveJob, RmlProcessParametersTableGroup);

                    if (recommendedProcess != null && recommendedProcess != SelectedProcessParametersTable)
                    {
                        SelectedProcessParametersTable = recommendedProcess;
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error resolving recommended process parameters.");
                    _notification.ShowError("An error occurred while trying to resolve the recommended process parameters.Please try to disable the auto selection.");
                    return;
                }
            }

            JobEvents.Clear();
            IsJobFailed = false;
            IsJobCanceled = false;
            IsJobCompleted = false;
            RunningJob = ActiveJob;
            _runningJobEstimatedDuration = EstimatedDuration;

            RunningJobSegments = RunningJob.EffectiveSegments.ToList();

            try
            {
                IsFree = false;
                LogManager.Log("Sending job to machine operator...");

                MachineOperator.GradientGenerationConfiguration.IsEnabled = Settings.EnableGradientGeneration;
                MachineOperator.GradientGenerationConfiguration.ResolutionCM = Settings.GradientResolutionCM;

                if (resumeFunc == null)
                {
                    JobHandler = await MachineOperator.Print(ActiveJob, SelectedProcessParametersTable);
                }
                else
                {
                    JobHandler = resumeFunc();
                }

                _navigation.NavigateTo(DeveloperNavigationView.RunningJobView);
                IsJobRunning = true;
                ShowJobStatus = true;

                JobHandler.StatusChanged += (x, status) =>
                {
                    if (IsJobRunning)
                    {
                        RunningJobStatus = status;

                        if (status.Message != null)
                        {
                            // TODO: Write to db when shlomo is not sending test messages anymore.
                            _eventLogger.Log(BL.Enumerations.EventTypes.JOB_STATUS, status.Message, false);
                        }
                    }
                };

                JobHandler.SegmentStarted += (x, segment) =>
                {
                    if (!segment.IsInterSegment)
                    {
                        _speech.SpeakInfo(String.Format("Segment {0} Started.", segment.SegmentIndex));
                    }
                    else
                    {
                        _speech.SpeakInfo(String.Format("Inter Segment Started."));
                    }
                };

                JobHandler.UnitCompleted += (x, unit) =>
                {
                    _speech.SpeakInfo(String.Format("{0} Units Completed.", unit + 1));
                };

                JobHandler.Failed += (x, ex) =>
                {
                    LogManager.Log(ex, String.Format("Job {0} has failed.", RunningJob.Name));
                    SetJobFailed();

                    InvokeUI(() =>
                    {
                        _notification.ShowError("Job failed. " + ex.FlattenMessage());
                        StopRecordingIfInProgress();
                    });
                };

                JobHandler.Finalizing += (_, __) =>
                {
                    _speech.SpeakInfo("Finalizing job...");
                    LogManager.Log(String.Format("Finalizing job {0}.", RunningJob.Name));
                };

                JobHandler.Completed += (x, e) =>
                {
                    LogManager.Log(String.Format("Job {0} has completed.", RunningJob.Name));
                    SetJobCompleted();
                    StopRecordingIfInProgress();
                };

                JobHandler.Canceled += (x, y) =>
                {
                    if (_preparingTaskItem != null)
                    {
                        _preparingTaskItem.Pop();
                        _preparingTaskItem = null;
                    }

                    LogManager.Log(String.Format("Job {0} has been canceled.", RunningJob.Name));
                    StopRecordingIfInProgress();
                    //Finally Canceled..
                };
            }
            catch (InsufficientLiquidQuantityException ex)
            {
                _notification.ShowModalDialog<InsufficientLiquidQuantityViewVM, InsufficientLiquidQuantityView>(new InsufficientLiquidQuantityViewVM(ex), (x) =>
                {

                    MachineOperator.EnableJobLiquidQuantityValidation = false;
                    StartJob();

                }, () => { });
            }
            catch (Exception ex)
            {
                LogManager.Log(ex);
                _notification.ShowError("An error occurred while starting the job. " + Environment.NewLine + ex.Message);
                SetJobFailed();
                StopRecordingIfInProgress();
            }
            finally
            {
                IsFree = true;
            }
        }

        /// <summary>
        /// Starts the job and record using the data capture module.
        /// </summary>
        private void StartJobAndRecord()
        {
            _isRecording = true;
            _dataCaptureVM.StartDiagnosticsRecording();
            StartJob();
        }

        /// <summary>
        /// Stops the recording if in progress.
        /// </summary>
        private void StopRecordingIfInProgress()
        {
            if (_isRecording)
            {
                _isRecording = false;
                InvokeUI(() => _dataCaptureVM.StopRecorderOrPlayer());
            }
        }

        private void BackToJob()
        {
            _navigation.NavigateTo(DeveloperNavigationView.JobView);
        }

        private void ToRunningJob()
        {
            _navigation.NavigateTo(DeveloperNavigationView.RunningJobView);
        }

        #endregion

        #region RML

        /// <summary>
        /// Saves the liquid factors.
        /// </summary>
        private async void SaveLiquidFactors()
        {
            if (SelectedRML != null)
            {
                CanWork = false;

                using (_notification.PushTaskItem("Saving Liquid Factors..."))
                {
                    LogManager.Log(String.Format("Saving liquid factors for RML {0}...", SelectedRML.Name));
                    await SelectedRML.SaveAsync(_activeJobDbContext);
                    var rmlAfterChange = RmlDTO.FromObservable(SelectedRML);
                    _actionLogManager.InsertLog(ActionLogType.RmlSaved, AuthenticationProvider.CurrentUser, SelectedRML.Name, _selectedRMLBeforeLiquidFactorsSaves, rmlAfterChange, "RML liquid factors changed from Machine Studio Research module.");
                    _selectedRMLBeforeLiquidFactorsSaves = rmlAfterChange;
                    LiquidTypesRmls = ActiveJob.Machine.Configuration.GetSupportedIdsPacks(SelectedRML).Select(x => x.LiquidType).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList();


                    foreach (var segment in ActiveJob.Segments)
                    {
                        SetSegmentBrushStopsLiquidVolumes(segment);
                    }
                }

                CanWork = true;
            }
        }

        /// <summary>
        /// Navigates to the DB Module in order to edit the selected RML.
        /// </summary>
        private void EditRML()
        {
            LogManager.Log(String.Format("Requesting DB module for RML {0} editing...", SelectedRML.Name));
        }

        /// <summary>
        /// Invalidates the liquid factors and process parameters tables.
        /// </summary>
        private void InvalidateLiquidFactorsAndProcessTables()
        {
            if (SelectedRML != null && SelectedMachine != null)
            {
                LogManager.Log("Invalidating liquid factors, process parameters and process group history...");

                _selectedRML = new RmlBuilder(_activeJobDbContext).Set(SelectedRML).WithAllParametersGroup().WithCAT(SelectedMachine.Guid).WithCctCache(_cctCache).WithCCT().WithGbdAndLub().WithLiquidFactors().WithSpools().Build();

                _selectedRMLBeforeLiquidFactorsSaves = RmlDTO.FromObservable(_selectedRML);

                if (_selectedRML.Cct == null)
                {
                    InvokeUI(() =>
                    {
                        _rml_has_no_cct = true;
                        _notification.ShowWarning(LogManager.Log($"No color conversion table defined for the selected RML '{_selectedRML.Name}'. Color conversion is disabled.", LogCategory.Warning));
                    });
                }
                else
                {
                    _rml_has_no_cct = false;
                }

                LiquidTypesRmls = ActiveJob.Machine.Configuration.GetSupportedIdsPacks(SelectedRML).Select(x => x.LiquidType).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList();

                RmlProcessParametersTableGroup = SelectedRML.ProcessParametersTablesGroups.ToList().SingleOrDefault(x => x.Active);

                var selectedHistory = RmlProcessParametersTableGroup;

                if (RmlProcessParametersTableGroup != null)
                {
                    RmlProcessParametersTableGroup = RmlProcessParametersTableGroup.Clone();
                    RmlProcessParametersTableGroup.ProcessParametersTables = RmlProcessParametersTableGroup.ProcessParametersTables.OrderBy(x => x.TableIndex).ToSynchronizedObservableCollection();
                }


                GroupsHistory = SelectedRML.ProcessParametersTablesGroups.OrderByDescending(x => x.SaveDate).OrderBy(x => !x.Active).ToObservableCollection();
                _selectedGroupHistory = selectedHistory;

                InvokeUI(() =>
                {
                    RaisePropertyChanged(nameof(SelectedGroupHistory));
                    RaisePropertyChanged(nameof(RmlProcessParametersTableGroup));
                });

                ActiveJob.Rml = SelectedRML;
            }
        }

        private async void OnSelectedRMLChanged()
        {
            if (SelectedRML != null && SelectedMachine != null)
            {
                using (_notification.PushTaskItem("Loading RML..."))
                {
                    await Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            IsFree = false;
                            InvalidateLiquidFactorsAndProcessTables();
                        }
                        catch
                        { }
                        finally
                        {
                            IsFree = true;
                        }
                    });
                }
            }
        }

        #endregion

        #region Color Space

        public void OnBrushStopColorSpaceChanged(BrushStop stop)
        {
            if (stop != null && stop.ColorSpace != null && stop.BrushColorSpace != BL.Enumerations.ColorSpaces.Volume)
            {
                var lubricant = stop.LiquidVolumes.SingleOrDefault(x => x.LiquidType == LiquidTypes.Lubricant);

                if (lubricant != null)
                {
                    lubricant.Volume = 100;
                }
            }
        }

        #endregion

        #region Process Parameters Management

        /// <summary>
        /// Uploads the selected process parameters table.
        /// </summary>
        private async void PushProcessParameters()
        {
            using (_notification.PushTaskItem("Uploading Process Parameters..."))
            {
                try
                {
                    LogManager.Log($"Uploading process parameters table {SelectedProcessParametersTable.Name}...");
                    await MachineOperator.UploadProcessParameters(SelectedProcessParametersTable);
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, $"Failed to upload process parameters table {SelectedProcessParametersTable.Name}");
                    _notification.ShowError("Failed to upload the selected process parameters." + Environment.NewLine + ex.Message);
                }
            }
        }

        /// <summary>
        /// Saves the process parameters group.
        /// </summary>
        private async void SaveProcessParameters()
        {
            var response = _notification.ShowTextInput("Enter Group Name", "Group Name");

            if (response == null) return;

            CanWork = false;

            using (_notification.PushTaskItem("Saving Parameters Group..."))
            {
                var processGroupBefore = ProcessParametersTablesGroupDTO.FromObservable(SelectedRML.GetActiveProcessGroup());

                using (var db = ObservablesContext.CreateDefault())
                {
                    var active_groups = db.ProcessParametersTablesGroups.Where(x => x.RmlGuid == SelectedRML.Guid && x.Active).ToList();

                    foreach (var g in active_groups)
                    {
                        g.Active = false;
                    }

                    await db.SaveChangesAsync();
                }

                LogManager.Log(String.Format("Saving process parameters group under the name {0}...", response));
                ProcessParametersTablesGroup group = new ProcessParametersTablesGroup();

                List<ProcessParametersTable> tables = new List<ProcessParametersTable>();

                int index = 0;

                foreach (var table in RmlProcessParametersTableGroup.ProcessParametersTables)
                {
                    var newTable = table.Clone();
                    newTable.TableIndex = index++;
                    newTable.ProcessParametersTablesGroup = group;
                    tables.Add(newTable);
                }

                group.Active = true;
                group.ProcessParametersTables = tables.ToSynchronizedObservableCollection();
                group.Rml = SelectedRML;
                group.Name = response;
                group.SaveDate = DateTime.UtcNow;

                foreach (var g in SelectedRML.ProcessParametersTablesGroups)
                {
                    g.Active = false;
                }

                SelectedRML.ProcessParametersTablesGroups.Add(group);
                await SelectedRML.SaveAsync(_activeJobDbContext);

                _actionLogManager.InsertLog(ActionLogType.RmlActiveProcessParametersChanged, AuthenticationProvider.CurrentUser, SelectedRML.Name, processGroupBefore, ProcessParametersTablesGroupDTO.FromObservable(SelectedRML.GetActiveProcessGroup()), "RML Active process parameters changed from Machine Studio Research module.");

                InvalidateLiquidFactorsAndProcessTables();
            }

            CanWork = true;
        }

        /// <summary>
        /// Resets the process parameters.
        /// </summary>
        private async void ResetProcessParameters()
        {
            if (_notification.ShowQuestion("This will reset the process parameters. Are you sure?"))
            {
                using (_notification.PushTaskItem("Resetting process parameters..."))
                {
                    try
                    {
                        await ApplicationManager.ConnectedMachine.UploadProcessParameters(new ProcessParametersTable());
                        _notification.ShowInfo("Heaters are turned off.");
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error resetting process parameters.");
                        _notification.ShowError("Error resetting process parameters." + Environment.NewLine + ex.Message);
                    }
                }
            }
        }

        #endregion

        #region Active Job Management

        /// <summary>
        /// Loads the selected job.
        /// </summary>
        private async void LoadSelectedJob(Action onCompleted = null)
        {
            if (SelectedMachineJob != null)
            {
                CanWork = false;

                using (_notification.PushTaskItem("Loading job details..."))
                {
                    try
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            _disable_gamut_check = true;

                            LogManager.Log(String.Format("Loading job {0}...", SelectedMachineJob.Name));
                            SelectedSegments = new ObservableCollection<Segment>();
                            SelectedBrushStops = new ObservableCollection<BrushStop>();
                            SelectedRML = null;
                            SelectedSegment = null;
                            SelectedGroupHistory = null;
                            SelectedBrushStop = null;
                            SelectedProcessParametersTable = null;
                            RmlProcessParametersTableGroup = null;

                            _blockInvalidateCommands = false;

                            LogManager.Log("Creating active job DB context...");
                            _activeJobDbContext = ObservablesContext.CreateDefault();

                            LogManager.Log("Initializing available color spaces, RMLs & Winding methods...");

                            //var processParamsGroups = _activeJobDbContext.ProcessParametersTablesGroups.ToList();
                            //var processParams = _activeJobDbContext.ProcessParametersTables.ToList();

                            ColorSpaces = _activeJobDbContext.ColorSpaces.ToObservableCollection();
                            Rmls = _activeJobDbContext.Rmls.OrderBy(i => i.Name).ToObservableCollection();
                            WindingMethods = _activeJobDbContext.WindingMethods.ToObservableCollection();
                            SpoolTypes = _activeJobDbContext.SpoolTypes.ToObservableCollection();

                            LogManager.Log("Loading machine spools...");
                            _activeJobDbContext.Spools.Where(x => x.MachineGuid == SelectedMachine.Guid).Load();

                            LogManager.Log("Setting active job...");
                            ActiveJob = new JobBuilder(_activeJobDbContext).Set(SelectedMachineJob.Guid).WithUser().WithSegments().WithBrushStops().WithConfiguration().WithRML(_cctCache).Build();

                            //_activeJobDbContext.Ccts.Where(x => x.RmlGuid == ActiveJob.RmlGuid).ToList();
                            //_activeJobDbContext.Cats.Where(x => x.RmlGuid == ActiveJob.RmlGuid).ToList();
                            //_activeJobDbContext.Machines.SingleOrDefault(x => x.Guid == ActiveJob.MachineGuid);
                            //_activeJobDbContext.Configurations.SingleOrDefault(x => x.Guid == ActiveJob.Machine.ConfigurationGuid);

                            //_activeJobDbContext.LiquidTypesRmls.ToList();

                            //_activeJobDbContext.IdsPackFormulas.ToList();
                            //_activeJobDbContext.LiquidTypes.ToList();
                            //_activeJobDbContext.MidTankTypes.ToList();
                            //_activeJobDbContext.DispenserTypes.ToList();

                            //_activeJobDbContext.IdsPacks.Where(x => x.ConfigurationGuid == ActiveJob.Machine.ConfigurationGuid).ToList();

                            _beforeSaveJobDTO = JobDTO.FromObservable(ActiveJob);


                            LogManager.Log("Setting selected segment...");
                            _selectedSegment = ActiveJob.OrderedSegments.FirstOrDefault();

                            ActiveJob.LengthChanged -= ActiveJob_LengthChanged;
                            ActiveJob.LengthChanged += ActiveJob_LengthChanged;

                            _selectedRML = ActiveJob.Rml;
                            InvalidateLiquidFactorsAndProcessTables();
                            RaisePropertyChanged(nameof(SelectedRML));

                            UpdateEstimatedDuration();

                            _blockInvalidateCommands = false;
                            InvalidateRelayCommands();

                            _disable_gamut_check = false;

                            _settings.LastSelectedMachineGuid = SelectedMachine != null ? SelectedMachine.Guid : null;
                            _settings.LastSelectedJobGuid = SelectedMachineJob != null ? SelectedMachineJob.Guid : null;

                            _settings.Save();
                        });

                        SegmentsCollectionView = CollectionViewSource.GetDefaultView(ActiveJob.Segments);
                        SegmentsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Segment.SegmentIndex), ListSortDirection.Ascending));

                        foreach (var segment in ActiveJob.Segments)
                        {
                            SetSegmentBrushStopsLiquidVolumes(segment);
                        }

                        SelectedSegment = _selectedSegment;

                        if (ActiveJob != null)
                        {
                            _current_job_string = ActiveJob.ToJobFileWhenLoaded().ToString();
                        }

                        UIHelper.DoEvents();
                        _navigation.NavigateTo(DeveloperNavigationView.JobView);
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error loading job.");
                        _notification.ShowError($"An error occurred while trying to load the selected job.\n{ex.FlattenMessage()}");
                    }
                    finally
                    {
                        CanWork = true;
                    }
                }

                CanWork = true;

                onCompleted?.Invoke();
            }
        }

        /// <summary>
        /// Saves the active job.
        /// </summary>
        private async void SaveActiveJob()
        {
            if (ActiveJob != null)
            {
                CanWork = false;

                try
                {
                    using (_notification.PushTaskItem("Saving job details..."))
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            LogManager.Log(String.Format("Saving the active job {0}...", ActiveJob.Name));
                            ActiveJob.LastUpdated = DateTime.UtcNow;
                            ActiveJob.Version = 1;
                            ActiveJob.IsSynchronized = false;
                            ActiveJob.Rml = SelectedRML;
                            ActiveJob.EstimatedDurationMili = (int)EstimatedDuration.TotalMilliseconds;
                            ActiveJob.MarkModified(_activeJobDbContext);
                            _activeJobDbContext.SaveChanges();

                            var afterJobDTO = JobDTO.FromObservable(ActiveJob);
                            _actionLogManager.InsertLog(ActionLogType.JobSaved, AuthenticationProvider.CurrentUser, _beforeSaveJobDTO.Name, _beforeSaveJobDTO, afterJobDTO, "Job saved from research module in Machine Studio.");
                            _beforeSaveJobDTO = afterJobDTO;

                            _machineDbContext.Entry(SelectedMachineJob).Reload();

                            _machineDbContext.Entry(SelectedMachineJob).Collection(x => x.Segments).Load();

                            foreach (var segment in SelectedMachineJob.Segments.ToList())
                            {
                                _machineDbContext.Entry(segment).Collection(x => x.BrushStops).Load();

                                foreach (var brushStop in segment.BrushStops.ToList())
                                {
                                    _machineDbContext.Entry(brushStop).Reload();
                                }

                                _machineDbContext.Entry(segment).Reload();
                            }

                            InvokeUI(() =>
                            {
                                SelectedMachineJob.Segments = SelectedMachineJob.Segments;
                            });

                            SelectedMachineJob.RaisePropertyChanged(x => x.Length);
                            SelectedMachineJob.RaisePropertyChanged(x => x.EstimatedDurationMili);

                            var settings = SettingsManager.Default.GetOrCreate<DeveloperModuleSettings>();
                            settings.DefaultJobRmlGuid = ActiveJob.RmlGuid;
                            settings.Save();

                            if (ActiveJob != null)
                            {
                                _current_job_string = ActiveJob.ToJobFileWhenLoaded().ToString();
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error saving active job.");
                    _notification.ShowError($"An error occurred while trying to save the current job.\n{ex.FlattenMessage()}");
                }
                finally
                {
                    CanWork = true;
                }
            }
        }

        private void BackToJobs()
        {
            LogManager.Log("User request for 'back to jobs'...");
            LogManager.Log("Comparing active job with selected job...");

            bool jobModified = ActiveJob.ToJobFileWhenLoaded().ToString() != _current_job_string;

            if (jobModified)
            {
                LogManager.Log("Selected job has been modified. Invoking confirmation dialog...");
                if (_notification.ShowQuestion("This will discard the current job changes. Are you sure?"))
                {
                    LogManager.Log("Disposing active job db context...");
                    _activeJobDbContext.Dispose();
                    _navigation.NavigateTo(DeveloperNavigationView.MachineJobSelectionView);
                }
            }
            else
            {
                LogManager.Log("Disposing active job db context...");
                _activeJobDbContext.Dispose();
                _navigation.NavigateTo(DeveloperNavigationView.MachineJobSelectionView);
            }
        }

        #endregion

        #region Private Methods

        private async void LoadMachine()
        {
            try
            {
                LogManager.Log("Loading selected machine...");

                CanWork = false;

                using (_notification.PushTaskItem("Loading selected machine..."))
                {
                    await _machineDbContext.Jobs.Where(x => x.MachineGuid == SelectedMachine.Guid).Include(x => x.User).Include(x => x.User.Contact).LoadAsync();

                    foreach (var job in SelectedMachine.Jobs)
                    {
                        await job.Reload(_machineDbContext);
                    }

                    await _machineDbContext.ColorSpaces.LoadAsync();

                    await Task.Factory.StartNew(() =>
                    {
                        _machineDbContext.Adapter.GetConfiguration(x => x.Guid == SelectedMachine.ConfigurationGuid);
                    });

                    RaisePropertyChanged(nameof(SelectedMachine));

                    JobsCollectionView = CollectionViewSource.GetDefaultView(SelectedMachine.Jobs);
                    JobsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Job.LastUpdated), ListSortDirection.Descending));
                }

                CanWork = true;

                foreach (var job in SelectedMachine.Jobs.OrderByDescending(x => x.LastUpdated))
                {
                    if (!CanWork) break;
                    job.Segments.EnableCrossThreadOperations();
                    await _machineDbContext.Segments.Where(x => x.JobGuid == job.Guid).Include(x => x.BrushStops).OrderBy(x => x.SegmentIndex).LoadAsync();
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex);
                _notification.ShowError("An error occurred while trying to load the selected machine.");
                CanWork = true;
            }
        }

        private void UpdateEstimatedDuration()
        {
            if (ActiveJob != null && SelectedProcessParametersTable != null && SelectedProcessParametersTable.DyeingSpeed > 0)
            {
                EstimatedDuration = ActiveJob.GetEstimatedDuration(SelectedProcessParametersTable);
            }
        }

        private void SetSegmentBrushStopsLiquidVolumes(Segment segment)
        {
            if (!DesignMode && segment != null)
            {
                LogManager.Log("Setting segment brush stops liquid volumes...");
                foreach (var stop in segment.BrushStops)
                {
                    stop.SetLiquidVolumes(ActiveJob.Machine.Configuration, SelectedRML, SelectedProcessParametersTable);
                }
            }
        }

        /// <summary>
        /// Navigates to the Machine Designer Module in order to edit the selected machine.
        /// </summary>
        private void EditMachine()
        {
            LogManager.Log(String.Format("Requesting machine designer module for machine {0} editing...", SelectedMachine.SerialNumber));
        }

        #endregion

        #region Add / Remove / Duplicate Jobs, Segments & Brush Stops

        /// <summary>
        /// Arranges the segments indices.
        /// </summary>
        private void ArrangeSegmentsIndices()
        {
            int index = 1;

            foreach (var segment in ActiveJob.Segments.OrderBy(x => x.SegmentIndex))
            {
                segment.SegmentIndex = index++;
            }

            SegmentsCollectionView.Refresh();
        }

        /// <summary>
        /// Arranges the brush stops indices.
        /// </summary>
        private void ArrangeBrushStopsIndices()
        {
            int index = 0;

            foreach (var stop in SelectedSegment.BrushStops.OrderBy(x => x.StopIndex))
            {
                stop.SetStopIndexNoRaise(index++);
            }

            if (SelectedSegment.BrushStops.Count > 1)
            {
                SelectedSegment.BrushStops.OrderBy(x => x.StopIndex).First().OffsetPercent = 0;
                SelectedSegment.BrushStops.OrderBy(x => x.StopIndex).Last().OffsetPercent = 100;
            }

            foreach (var stop in SelectedSegment.BrushStops.OrderBy(x => x.StopIndex))
            {
                stop.RaiseStopIndex();
                stop.RaiseOffsetChanged();
            }

            BrushStopsCollectionView.Refresh();
        }

        /// <summary>
        /// Removes the selected segments.
        /// </summary>
        private void RemoveSelectedSegments()
        {
            if (ActiveJob != null && SelectedSegment != null)
            {
                if (_notification.ShowQuestion("Are you sure you want to delete the selected segments?"))
                {
                    LogManager.Log(String.Format("Removing {0} segments...", SelectedSegments.Count));

                    SelectedSegments.ToList().ForEach(x =>
                    {
                        if (ActiveJob.Segments.Count == 1)
                        {
                            _notification.ShowInfo("A job must contain at least one segment.");
                            return;
                        }

                        x.Delete(_activeJobDbContext);
                    });

                    ArrangeSegmentsIndices();
                }
            }
        }

        /// <summary>
        /// Adds a new segment.
        /// </summary>
        private void AddSegment()
        {
            if (ActiveJob != null)
            {
                LogManager.Log($"Adding new segment to job {ActiveJob.Name}...");
                Segment seg = new Segment();
                seg.Job = ActiveJob;
                seg.Name = "SEGMENT";
                seg.Length = 10;

                if (ActiveJob.Segments.Count > 0)
                {
                    seg.SegmentIndex = ActiveJob.Segments.Max(x => x.SegmentIndex) + 1;
                }
                else
                {
                    seg.SegmentIndex = 1;
                }

                ActiveJob.Segments.Add(seg);
                SelectedSegment = seg;
                AddBrushStop();
                SetSegmentBrushStopsLiquidVolumes(SelectedSegment);
                ArrangeSegmentsIndices();
            }
        }

        /// <summary>
        /// Removes the selected jobs.
        /// </summary>
        private async void RemoveSelectedJobs()
        {
            if (SelectedMachine != null && SelectedMachineJob != null)
            {
                if (_notification.ShowQuestion("Are you sure you want to delete the selected jobs?"))
                {
                    try
                    {
                        IsFree = false;

                        var jobsToReport = SelectedJobs.Select(x => JobDTO.FromObservable(x)).ToList();

                        LogManager.Log(String.Format("Removing {0} jobs...", SelectedJobs.Count));
                        SelectedJobs.ToList().ForEach(x =>
                        {
                            x.Delete(_machineDbContext);
                        });

                        using (_notification.PushTaskItem("Removing selected jobs..."))
                        {
                            LogManager.Log("Saving selected machine to database...");
                            await SelectedMachine.SaveAsync(_machineDbContext);
                        }

                        foreach (var job in jobsToReport)
                        {
                            _actionLogManager.InsertLog(ActionLogType.JobDeleted, AuthenticationProvider.CurrentUser, job.Name, job, "Job deleted using Machine Studio.", true);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error removing the selected job.");
                        _notification.ShowError($"Error removing the selected job.\n{ex.FlattenMessage()}");
                    }
                    finally
                    {
                        IsFree = true;
                    }
                }
            }
        }

        /// <summary>
        /// Adds a new job to the selected machine.
        /// </summary>
        private async void AddJob()
        {
            if (SelectedMachine != null)
            {
                String jobName = _notification.ShowTextInput("Please provide a job name", "Name");

                if (!String.IsNullOrWhiteSpace(jobName))
                {
                    LogManager.Log(String.Format("Adding new job {0}...", jobName));

                    var settings = SettingsManager.Default.GetOrCreate<DeveloperModuleSettings>();

                    Job newJob = new Job();
                    newJob.LastUpdated = DateTime.UtcNow;
                    newJob.JobSource = JobSource.Remote;
                    newJob.Name = jobName;
                    newJob.CreationDate = DateTime.UtcNow;
                    newJob.UserGuid = AuthenticationProvider.CurrentUser.Guid;

                    if (String.IsNullOrWhiteSpace(settings.DefaultJobRmlGuid))
                    {
                        newJob.Rml = _machineDbContext.Rmls.FirstOrDefault();
                    }
                    else
                    {
                        var rml = _machineDbContext.Rmls.SingleOrDefault(x => x.Guid == settings.DefaultJobRmlGuid);
                        if (rml != null)
                        {
                            newJob.Rml = rml;
                        }
                        else
                        {
                            newJob.Rml = _machineDbContext.Rmls.FirstOrDefault();
                        }
                    }

                    newJob.WindingMethod = _machineDbContext.WindingMethods.FirstOrDefault();
                    newJob.SpoolType = _machineDbContext.SpoolTypes.FirstOrDefault(x => x.Code == (int)BL.Enumerations.SpoolTypes.StandardSpool);
                    newJob.ColorSpace = _machineDbContext.ColorSpaces.FirstOrDefault();
                    newJob.Machine = SelectedMachine;



                    SelectedMachine.Jobs.Add(newJob);
                    var segment = newJob.AddSolidSegment();
                    segment.BrushStops[0].SetAllDispensingStepDivisions(BL.Dispensing.DispenserStepDivisions.D8);

                    LogManager.Log("Saving selected machine to database...");
                    await SelectedMachine.SaveAsync(_machineDbContext);
                    _actionLogManager.InsertLog(ActionLogType.JobCreated, AuthenticationProvider.CurrentUser, newJob.Name, newJob, "Job created using Machine Studio.");
                    SelectedMachineJob = newJob;
                    LoadSelectedJob();
                }
            }
        }

        /// <summary>
        /// Removes the selected brush stop.
        /// </summary>
        private void RemoveSelectedBrushStops()
        {
            if (SelectedBrushStop != null && SelectedSegment != null)
            {
                if (_notification.ShowQuestion("Are you sure you want to delete the selected brush stops?"))
                {
                    LogManager.Log(String.Format("Removing {0} brush stops...", SelectedBrushStops.Count));

                    SelectedBrushStops.ToList().ForEach(x =>
                    {
                        if (SelectedSegment.BrushStops.Count == 1)
                        {
                            _notification.ShowInfo("A job segment must contain at least one brush stop.");
                            return;
                        }
                        SelectedSegment.BrushStops.Remove(x);
                        var existingBrushStop = _activeJobDbContext.BrushStops.FirstOrDefault(y => y.Guid == x.Guid);
                        if (existingBrushStop != null)
                        {
                            _activeJobDbContext.BrushStops.Remove(existingBrushStop);
                        }


                    });

                    ArrangeBrushStopsIndices();
                }
            }
        }

        /// <summary>
        /// Adds a new brush stop to the selected segment.
        /// </summary>
        private void AddBrushStop()
        {
            if (SelectedSegment != null)
            {
                LogManager.Log($"Adding new brush stop to segment '{SelectedSegment.SegmentIndex}'...");

                var stop = new BrushStop();

                if (SelectedSegment.BrushStops.Count > 0)
                {
                    stop.StopIndex = SelectedSegment.BrushStops.Max(x => x.StopIndex) + 1;
                }
                else
                {
                    stop.StopIndex = 1;
                }

                stop.OffsetPercent = 100;
                stop.Segment = SelectedSegment;
                stop.ColorSpace = ColorSpaces.FirstOrDefault();
                stop.Color = Colors.Black;
                stop.SetAllDispensingStepDivisions(BL.Dispensing.DispenserStepDivisions.D8);
                stop.SetLiquidVolumes(SelectedMachine.Configuration, SelectedRML, SelectedProcessParametersTable);
                SelectedSegment.BrushStops.Add(stop);
                // _activeJobDbContext.BrushStops.Add(stop);
                SelectedSegment.BrushStops.ToList().ForEach(x => x.RaiseOffsetChanged());
                ArrangeBrushStopsIndices();
            }
        }

        /// <summary>
        /// Duplicates the selected brush stops.
        /// </summary>
        private void DuplicateSelectedBrushStops()
        {
            LogManager.Log($"Duplicating {SelectedBrushStops.Count} brush stops...");

            foreach (var stop in SelectedBrushStops.OrderBy(x => x.StopIndex))
            {
                var cloned = stop.Clone();
                cloned.StopIndex = SelectedSegment.BrushStops.Max(x => x.StopIndex) + 1;
                cloned.SetLiquidVolumes(ActiveJob.Machine.Configuration, SelectedRML, SelectedProcessParametersTable);
                SelectedSegment.BrushStops.Add(cloned);
            }

            ArrangeBrushStopsIndices();
        }

        /// <summary>
        /// Duplicates the selected segments.
        /// </summary>
        private void DuplicateSelectedSegments()
        {
            LogManager.Log($"Duplicating {SelectedSegments.Count} segments...");

            int start_index = SelectedSegments.Max(x => x.SegmentIndex);

            ActiveJob.Segments.Where(x => x.SegmentIndex > start_index).ToList().ForEach(x => x.SegmentIndex = x.SegmentIndex + SelectedSegments.Count);

            foreach (var segment in SelectedSegments.OrderBy(x => x.SegmentIndex))
            {
                var cloned = segment.Clone();
                cloned.SegmentIndex = start_index++;
                ActiveJob.Segments.Add(cloned);
                SelectedSegment = cloned;
            }

            ArrangeSegmentsIndices();
        }

        /// <summary>
        /// Duplicates the selected jobs.
        /// </summary>
        private async void DuplicateSelectedJobs()
        {
            if (SelectedMachineJob != null)
            {
                using (_notification.PushTaskItem("Cloning selected jobs..."))
                {
                    CanWork = false;

                    LogManager.Log($"Duplicating {SelectedJobs.Count} jobs...");

                    int index = SelectedMachine.Jobs.Max(x => x.JobIndex);

                    foreach (var job in SelectedJobs)
                    {
                        var cloned = job.Clone();
                        cloned.JobIndex = ++index;
                        SelectedMachine.Jobs.Add(cloned);
                    }

                    LogManager.Log("Saving selected machine to database...");
                    await SelectedMachine.SaveAsync(_machineDbContext);

                    foreach (var job in SelectedJobs)
                    {
                        _actionLogManager.InsertLog(ActionLogType.JobCreated, AuthenticationProvider.CurrentUser, job.Name, job, "Job cloned using Machine Studio.");
                    }

                    CanWork = true;
                }
            }
        }

        #endregion

        #region Embroidery

        /// <summary>
        /// Imports embroidery file.
        /// </summary>
        private void ImportEmbroideryFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select embroidery file";
            dlg.Filter = EMB_FORMATS_IMPORT;
            if (dlg.ShowDialogCenter())
            {
                var view = new EmbroideryImportView();

                _notification.ShowModalDialog<EmbroideryImportViewVM, EmbroideryImportView>(
                    new EmbroideryImportViewVM() { FileName = dlg.FileName }, view,
                    (vm) =>
                    {
                        String jobName = _notification.ShowTextInput("Please provide a job name", "Name");

                        if (jobName != null)
                        {
                            AddJobFromEmbroideryFile(jobName, vm, dlg.FileName, view.EmbroideryImageBytes);
                        }
                    },
                    () =>
                    {

                    });
            }
        }

        private async void AddJobFromEmbroideryFile(String jobName, EmbroideryImportViewVM vm, String fileName, byte[] imageBytes)
        {
            LogManager.Log(String.Format("Adding new job from embroidery file {0}...", jobName));

            Job job = new Job();
            job.Name = jobName;
            job.Name = jobName;
            job.CreationDate = DateTime.UtcNow;
            job.UserGuid = AuthenticationProvider.CurrentUser.Guid;
            job.Rml = _machineDbContext.Rmls.FirstOrDefault();
            job.WindingMethod = _machineDbContext.WindingMethods.FirstOrDefault();
            job.SpoolType = _machineDbContext.SpoolTypes.FirstOrDefault();
            job.Machine = SelectedMachine;

            job.EmbroideryFileName = Path.GetFileName(fileName);
            job.EmbroideryFileData = File.ReadAllBytes(fileName);
            job.EmbroideryJpeg = imageBytes;
            job.HasEmbroideryFile = true;

            foreach (var path in vm.Paths.Skip(1))
            {
                Segment segment = new Segment();

                double baseLength = path.Length / 1000d;
                double embThicknessLength = (vm.EmbroideryMaterialThickness * path.StitchCount) / 1000d;
                double stabilizerThicknessLength = (vm.StabilizerThickness * path.StitchCount) / 1000d;
                double totalLength = (baseLength + embThicknessLength) * vm.SelectedEmbroideryMaterial.Coefficient;

                if (vm.HasStabilizer)
                {
                    totalLength += (stabilizerThicknessLength * vm.SelectedStabilizer.Coefficient);
                }

                segment.Length = totalLength;
                segment.Name = "Embroidery Segment";
                segment.SegmentIndex = vm.Paths.IndexOf(path);

                if (path.Brush is SolidColorBrush)
                {
                    var brush = (path.Brush as SolidColorBrush);

                    segment.BrushStops.Add(new BrushStop()
                    {
                        Red = brush.Color.R,
                        Green = brush.Color.G,
                        Blue = brush.Color.B,
                        ColorSpace = _machineDbContext.ColorSpaces.ToList().SingleOrDefault(x => x.Code == BL.Enumerations.ColorSpaces.RGB.ToInt32()),
                    });
                }
                else
                {
                    var brush = (path.Brush as LinearGradientBrush);

                    foreach (var stop in brush.GradientStops)
                    {
                        segment.BrushStops.Add(new BrushStop()
                        {
                            StopIndex = brush.GradientStops.IndexOf(stop),
                            Red = stop.Color.R,
                            Green = stop.Color.G,
                            Blue = stop.Color.B,
                            OffsetPercent = stop.Offset * 100d,
                            ColorSpace = _machineDbContext.ColorSpaces.ToList().SingleOrDefault(x => x.Code == BL.Enumerations.ColorSpaces.RGB.ToInt32()),
                        });
                    }
                }

                job.Segments.Add(segment);
            }

            SelectedMachine.Jobs.Add(job);
            LogManager.Log("Saving selected machine to database...");
            await SelectedMachine.SaveAsync(_machineDbContext);
            SelectedMachineJob = job;
            LoadSelectedJob();
        }

        private void DisplayJobEmbroideryFile(Job job)
        {
            _notification.ShowModalDialog<EmbroideryDisplayViewVM, EmbroideryDisplayView>(new EmbroideryDisplayViewVM(job), (vm) =>
            {

                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Title = "Select embroidery file location and format";
                dlg.Filter = EMB_FORMATS_EXPORT;
                dlg.FileName = job.EmbroideryFileName;
                if (dlg.ShowDialogCenter())
                {
                    try
                    {
                        var tempDir = TemporaryManager.CreateFolder();
                        String filePath = Path.Combine(tempDir.Path, job.EmbroideryFileName);
                        File.WriteAllBytes(filePath, job.EmbroideryFileData);
                        EmbroideryFileConverter.ConvertEmbroideryFile(filePath, dlg.FileName);
                        _notification.ShowInfo("Embroidery file exported successfully.");
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "An error has occurred while trying to export the attached embroidery file.");
                        _notification.ShowError("An error has occurred while trying to export the attached embroidery file.");
                    }
                }

            }, () => { });
        }

        #endregion

        #region Job Import/Export

        private async void ExportJobFile()
        {
            if (SelectedJobs != null && SelectedJobs.Count > 1)
            {
                CommonOpenFileDialog dlg = new CommonOpenFileDialog();
                dlg.Title = "Select a folder to place all job files.";
                dlg.IsFolderPicker = true;

                if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
                {
                    foreach (var job in SelectedJobs)
                    {
                        using (_notification.PushTaskItem($"Exporting job '{job.Name}'..."))
                        {
                            try
                            {
                                LogManager.Log($"Exporting job file {job.Name}");

                                var jobFile = await job.ToJobFile();
                                File.WriteAllBytes(Path.Combine(dlg.FileName, job.Name + ".job"), jobFile.ToBytes());
                            }
                            catch (Exception ex)
                            {
                                LogManager.Log(ex, "Error exporting job file.");
                                _notification.ShowError($"An error occurred while trying to export job '{job.Name}'.\n{ex.FlattenMessage()}");
                            }
                        }
                    }
                }
            }
            else
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Title = "Export Job File";
                dlg.Filter = "Twine Job Files|*.job";
                dlg.DefaultExt = ".job";
                dlg.FileName = SelectedMachineJob.Name;
                if (dlg.ShowDialog().Value)
                {
                    using (_notification.PushTaskItem($"Exporting job '{SelectedMachineJob.Name}'..."))
                    {
                        try
                        {
                            LogManager.Log($"Exporting job file {SelectedMachineJob.Name}");

                            var jobFile = await SelectedMachineJob.ToJobFile();
                            File.WriteAllBytes(dlg.FileName, jobFile.ToBytes());
                        }
                        catch (Exception ex)
                        {
                            LogManager.Log(ex, "Error exporting job file.");
                            _notification.ShowError($"An error occurred while trying to export job '{SelectedMachineJob.Name}'.\n{ex.FlattenMessage()}");
                        }
                    }
                }
            }
        }

        private async void ImportJobFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Import Job Files";
            dlg.Filter = "Twine Job Files|*.job";
            dlg.Multiselect = true;
            if (dlg.ShowDialog().Value)
            {
                using (_notification.PushTaskItem($"Importing job files..."))
                {
                    try
                    {
                        IsFree = false;

                        LogManager.Log($"Importing job files...");

                        List<Job> jobsToReport = new List<Job>();

                        foreach (var file in dlg.FileNames)
                        {
                            var bytes = File.ReadAllBytes(file);
                            var jobFile = JobFile.Parser.ParseFrom(bytes);
                            var job = await Job.FromJobFile(jobFile, SelectedMachine.Guid, AuthenticationProvider.CurrentUser.Guid);
                            job.JobSource = JobSource.Remote;

                            _machineDbContext.Jobs.Add(job);
                            jobsToReport.Add(job);
                        }

                        await _machineDbContext.SaveChangesAsync();

                        foreach (var job in jobsToReport)
                        {
                            _actionLogManager.InsertLog(ActionLogType.JobImported, AuthenticationProvider.CurrentUser, job.Name, job, "Job imported using Machine Studio.");
                        }

                        IsFree = true;

                        _notification.ShowInfo($"Jobs imported successfully.");
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Error importing job file.");
                        _notification.ShowError($"An error occurred while trying to import the selected job file.\n{ex.FlattenMessage()}");
                    }
                    finally
                    {
                        IsFree = true;
                    }
                }
            }
        }

        #endregion

        #region Override Methods

        protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
        {
            base.RaisePropertyChangedAuto(caller);

            if (!_blockInvalidateCommands)
            {
                InvalidateRelayCommands();
            }
        }

        #endregion

        #region IStudioViewModel

        public override Task<bool> OnShutdownRequest()
        {
            if (IsJobRunning)
            {
                InvokeUI(() =>
                {
                    _notification.ShowWarning("Please stop the currently running job before closing the developer module.");
                });

                return Task.FromResult(false);
            }

            return Task.FromResult(true);
        }

        public override void OnShuttingDown()
        {

        }

        #endregion
    }
}