aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs
blob: 2013de3c899012953c9d8c6361b7ef8c2573664d (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
using GalaSoft.MvvmLight.Ioc;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.DAL.Observables;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Common.StudioApplication;
using Tango.SharedUI;

namespace Tango.MachineStudio.Developer.ViewModels
{
    public class MainViewVM : ViewModel
    {
        private INotificationProvider _notification;

        public IStudioApplicationManager ApplicationManager { get; set; }

        public ObservablesEntitiesAdapter Adapter { get; set; }

        private Machine _selectedMachine;

        public Machine SelectedMachine
        {
            get { return _selectedMachine; }
            set { _selectedMachine = value; RaisePropertyChangedAuto(); OnMachineChanged(); InvalidateRelayCommands(); }
        }

        private List<LiquidTypesRml> _liquidTypesRmls;

        public List<LiquidTypesRml> LiquidTypesRmls
        {
            get { return _liquidTypesRmls; }
            set { _liquidTypesRmls = value; RaisePropertyChangedAuto(); }
        }

        private ProcessParametersTablesGroup _rmlProcessParametersTablesGroup;

        public ProcessParametersTablesGroup RmlProcessParametersTableGroup
        {
            get { return _rmlProcessParametersTablesGroup; }
            set { _rmlProcessParametersTablesGroup = value; RaisePropertyChangedAuto(); }
        }

        private ProcessParametersTablesGroup _selectedGroupHistory;

        public ProcessParametersTablesGroup SelectedGroupHistory
        {
            get { return _selectedGroupHistory; }
            set { _selectedGroupHistory = value; RaisePropertyChangedAuto(); OnSelectedGroupHistoryChanged(); }
        }

        private void OnSelectedGroupHistoryChanged()
        {
            if (SelectedGroupHistory != null)
            {
                RmlProcessParametersTableGroup = SelectedGroupHistory.CloneGroup();
            }
        }

        private ObservableCollection<ProcessParametersTablesGroup> _groupsHistory;

        public ObservableCollection<ProcessParametersTablesGroup> GroupsHistory
        {
            get { return _groupsHistory; }
            set { _groupsHistory = value; RaisePropertyChangedAuto(); }
        }

        private Job _selectedJob;

        public Job SelectedJob
        {
            get { return _selectedJob; }
            set { _selectedJob = value; RaisePropertyChangedAuto(); }
        }


        private Rml _selectedRML;

        public Rml SelectedRML
        {
            get { return _selectedRML; }
            set { _selectedRML = value; RaisePropertyChangedAuto(); InvalidateLiquidFactorsAndProcessTables(); InvalidateRelayCommands(); }
        }

        private bool _isSideBarOpened;

        public bool IsSideBarOpened
        {
            get { return _isSideBarOpened; }
            set { _isSideBarOpened = value; RaisePropertyChangedAuto(); }
        }


        public RelayCommand EditMachineCommand { get; set; }

        public RelayCommand EditRMLCommand { get; set; }

        public RelayCommand ToggleSideBarCommand { get; set; }

        public RelayCommand SaveProcessParametersCommand { get; set; }

        public RelayCommand SaveLiquidFactorsCommand { get; set; }

        public MainViewVM()
        {
            IsSideBarOpened = true;
        }

        [PreferredConstructor]
        public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notificationProvider)
        {
            _notification = notificationProvider;
            Adapter = ObservablesEntitiesAdapter.Instance;
            EditMachineCommand = new RelayCommand(EditMachine, (x) => SelectedMachine != null);
            ApplicationManager = applicationManager;
            EditRMLCommand = new RelayCommand(EditRML, (x) => SelectedRML != null);
            ToggleSideBarCommand = new RelayCommand(() => IsSideBarOpened = !IsSideBarOpened);
            SaveProcessParametersCommand = new RelayCommand(SaveProcessParameters);
            SaveLiquidFactorsCommand = new RelayCommand(SaveLiquidFactors);
            IsSideBarOpened = true;
        }

        private async void SaveLiquidFactors()
        {
            if (SelectedRML != null)
            {
                using (_notification.PushTaskItem("Saving Liquid Factors..."))
                {
                    String machineGuid = SelectedMachine.Guid;
                    String rmlGuid = SelectedRML.Guid;

                    await SelectedRML.SaveAsync();

                    SelectedMachine = Adapter.Machines.SingleOrDefault(x => x.Guid == machineGuid);
                    SelectedRML = Adapter.Rmls.SingleOrDefault(x => x.Guid == rmlGuid);
                }
            }
        }

        private void EditRML()
        {
            ApplicationManager.RequestModule("Data Base", SelectedRML);
        }

        private void EditMachine()
        {
            ApplicationManager.RequestModule("Machine Designer", SelectedMachine);
        }

        private void OnMachineChanged()
        {
            InvalidateLiquidFactorsAndProcessTables();
        }

        private async void SaveProcessParameters()
        {
            var response = _notification.ShowTextInput("Enter Group Name", "Group Name");

            if (response == null) return;

            using (_notification.PushTaskItem("Saving Parameters Group..."))
            {
                ProcessParametersTablesGroup group = new ProcessParametersTablesGroup();

                List<ProcessParametersTable> tables = new List<ProcessParametersTable>();
                foreach (var table in RmlProcessParametersTableGroup.ProcessParametersTables)
                {
                    var newTable = table.CloneEntity();
                    newTable.ProcessParametersTablesGroups = group;
                    tables.Add(newTable);
                }

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

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

                String machineGuid = SelectedMachine.Guid;
                String rmlGuid = SelectedRML.Guid;

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

                SelectedMachine = Adapter.Machines.SingleOrDefault(x => x.Guid == machineGuid);
                SelectedRML = Adapter.Rmls.SingleOrDefault(x => x.Guid == rmlGuid);
            }
        }

        private void InvalidateLiquidFactorsAndProcessTables()
        {
            if (SelectedRML != null && SelectedMachine != null)
            {
                LiquidTypesRmls = SelectedMachine.Configuration.IdsPacks.OrderBy(x => x.PackIndex).Select(x => x.LiquidTypes).SelectMany(x => x.LiquidTypesRmls).Where(x => x.Rml.Guid == SelectedRML.Guid).ToList();
                RmlProcessParametersTableGroup = SelectedRML.ProcessParametersTablesGroups.SingleOrDefault(x => x.Active);

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

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