-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoroscopeViewModel.cs
More file actions
218 lines (181 loc) · 6.22 KB
/
Copy pathHoroscopeViewModel.cs
File metadata and controls
218 lines (181 loc) · 6.22 KB
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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace _01LabChumachenko
{
class HoroscopeViewModel: INotifyPropertyChanged
{
#region Fields
private string _westHoroscope;
private string _eastHoroscope;
private string _age;
private DateTime? _birthday;
#endregion
#region Commands
private RelayCommand<object> _calculatedCommand;
private RelayCommand<object> _closeCommand;
#endregion
#region Properties
public string WestHoroscope
{
get => _westHoroscope;
set
{
_westHoroscope = value;
OnPropertyChanged();
}
}
public string EastHoroscope
{
get => _eastHoroscope;
set
{
_eastHoroscope = value;
OnPropertyChanged();
}
}
public string Age
{
get => _age;
set
{
_age = value;
OnPropertyChanged();
}
}
public DateTime? Birthday
{
get => _birthday;
set
{
_birthday = value;
OnPropertyChanged();
}
}
#endregion
#region Commands
public RelayCommand<object> CalculatedCommand
{
get =>
_calculatedCommand?? (_calculatedCommand = new RelayCommand<object>(
HoroscopeImplementation));
}
public RelayCommand<Object> CloseCommand
{
get
{
return _closeCommand ?? (_closeCommand = new RelayCommand<object>(o => Environment.Exit(0)));
}
}
private async void HoroscopeImplementation(object o)
{
if (_birthday != null)
{
DateTime birthday = (DateTime) _birthday;
if (!IsDateValid(birthday)) return;
var dayBirth = birthday.Day;
var monthBirth = birthday.Month;
LoaderManager.Instance.ShowLoader();
await Task.Run(() =>
{
Thread.Sleep(1000);
Age = ""+((DateTime.Today.DayOfYear >= birthday.DayOfYear)
? DateTime.Today.Year - birthday.Year
: DateTime.Today.Year - birthday.Year - 1);
EastHoroscope = Enum.GetName(typeof(EastHoroscopeList), birthday.Year % 12);
WestHoroscope = CreateWestHoroscope(dayBirth, monthBirth);
});
LoaderManager.Instance.HideLoader();
if (birthday.Day == DateTime.Today.Day
&& birthday.Month == DateTime.Today.Month)
MessageBox.Show("Happy Birthday To You!");
}
else
{
MessageBox.Show("Select date, please");
}
}
private bool IsDateValid(DateTime birthday)
{
if (DateTime.Compare(birthday, DateTime.Today) > 0)
{
MessageBox.Show("Wrong date");
return false;
}
if (DateTime.Today.Year - 135 > birthday.Year)
{
MessageBox.Show("Sorry, you cannot be older than 135 years. \n Please, enter correct information about your birthday. " +
"\n (If you are vampire, please compute your year of birthday plus twelve while entered information will be correct)");
return false;
}
return true;
}
private string CreateWestHoroscope(int dayBirth, int monthBirth)
{
if (dayBirth < 18) return Enum.GetName(typeof(WestHoroscopeList), monthBirth);
if (dayBirth > 23) return Enum.GetName(typeof(WestHoroscopeList), monthBirth);
switch (monthBirth)
{
case 2:
return (dayBirth == 18) ? Enum.GetName(typeof(WestHoroscopeList), monthBirth) : Enum.GetName(typeof(WestHoroscopeList), monthBirth + 1);
case 1:
case 3:
case 4:
return dayBirth == 20 ? Enum.GetName(typeof(WestHoroscopeList), monthBirth) : Enum.GetName(typeof(WestHoroscopeList), monthBirth + 1);
case 5:
case 6:
case 12:
return dayBirth == 21 ? Enum.GetName(typeof(WestHoroscopeList), monthBirth) : Enum.GetName(typeof(WestHoroscopeList), (monthBirth + 1) % 12);
case 7:
case 11:
case 9:
return dayBirth == 22 ? Enum.GetName(typeof(WestHoroscopeList), monthBirth) : Enum.GetName(typeof(WestHoroscopeList), monthBirth + 1);
case 8:
return dayBirth == 23 ? Enum.GetName(typeof(WestHoroscopeList), monthBirth) : Enum.GetName(typeof(WestHoroscopeList), monthBirth + 1);
default:
return "Something went wrong";
}
}
internal enum WestHoroscopeList
{
Copricorn = 1,
Aquarius,
Pisces,
Aries,
Taurus,
Gemini,
Cancer,
Leo,
Virgio,
Libra,
Scorpio,
Sagittarius
}
internal enum EastHoroscopeList
{
Monkey,
Cock,
Dog,
Pig,
Rat,
Bull,
Tiger,
Cat,
Dragon,
Snake,
Horse,
Goat
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}