aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/ViewModels/LoginViewVM.cs
blob: 1ee741d8684fe86ebc8a4ab9204ff173bd7ab89b (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Common.Notifications;
using Tango.MachineService.Gateway;

namespace Tango.FSE.UI.ViewModels
{
    public class LoginViewVM : FSEViewModel
    {
        public enum LoginViews
        {
            Login,
            Logging
        }

        private LoginViews _selectedView;
        public LoginViews SelectedView
        {
            get { return _selectedView; }
            set { _selectedView = value; RaisePropertyChangedAuto(); }
        }

        private String _email;
        public String Email
        {
            get { return _email; }
            set { _email = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _password;
        public String Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private EnvironmentConfiguration _selectedEnvironment;
        public EnvironmentConfiguration SelectedEnvironment
        {
            get { return _selectedEnvironment; }
            set { _selectedEnvironment = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _status;
        public String Status
        {
            get { return _status; }
            set { _status = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand LoginCommand { get; set; }

        public LoginViewVM()
        {
            LoginCommand = new RelayCommand(Login, () => Email.IsNotNullOrEmpty() && Password.IsNotNullOrEmpty() && SelectedEnvironment != null);
        }

        public override void OnApplicationStarted()
        {
            Email = "roy@twine-s.com";
            Password = "1Creativity";
        }

        public override void OnNavigatedTo()
        {
            base.OnNavigatedTo();
            SelectedEnvironment = GatewayService.Environments.FirstOrDefault(x => x.MachineServiceAddress == Settings.MachineServiceAddress);

            if (SelectedEnvironment == null)
            {
                SelectedEnvironment = GatewayService.Environments.FirstOrDefault();
            }
        }

        private async void Login()
        {
            try
            {
                IsFree = false;
                SelectedView = LoginViews.Logging;
                var result = await AuthenticationProvider.Login(Email, Password, SelectedEnvironment);

                Status = "Loading static collections...";

                await Task.Factory.StartNew(() =>
                {
                    ObservablesStaticCollections.Instance.Initialize((msg) =>
                    {
                        Status = msg;
                    });
                });

                Status = "Starting application...";

                await Task.Delay(500);

                if (!result.Response.PasswordChangeRequired)
                {
                    await NavigationManager.NavigateTo(NavigationView.Home);
                }
            }
            catch (Exception ex)
            {
                IsFree = true;
                SelectedView = LoginViews.Login;
                await NotificationProvider.ShowError(ex.GetFirstIfAggregate().Message);
                //await NotificationProvider.ShowWarning(ex.Message);
                //await NotificationProvider.ShowSuccess(ex.Message);
                //await NotificationProvider.ShowInfo(ex.Message);
                //await NotificationProvider.ShowQuestion("Are you sure ?");
                //await NotificationProvider.ShowWarningQuestion("Are you really really sure ?", "YES, I'M", "SORRY");
            }
            finally
            {
                IsFree = true;
            }
        }
    }
}