-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
166 lines (150 loc) · 7.42 KB
/
MainWindow.xaml.cs
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
using ClassLibrary;
using ClassLibrary.Services;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using ClassLibrary.Repo;
using System.ComponentModel;
using ClassLibrary.Models;
using System.Windows.Controls.Primitives;
namespace WpfApp
{
public partial class MainWindow : Window
{
private readonly IWorldCupService worldCupService = new WorldCupService(new RestApiRepo());
private readonly List<PlayerWindow> playerWindows = new();
public MainWindow()
{
InitializeComponent();
if (!UserSettings.SettingsExist(length: 3) && new SettingsWindow(true).ShowDialog() == false)
Environment.Exit(0);
ApplySettings(true);
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
if (new SettingsWindow().ShowDialog() == true)
ApplySettings();
}
private void ApplySettings(bool isStartup = false)
{
var settings = UserSettings.LoadSettings().Select(int.Parse).ToArray();
(var language, var championship, var resolution) = (settings[0], settings[1], settings[2]);
var prevChampionshipPath = UserSettings.ChampionshipPath;
UserSettings.ChampionshipPath = championship == 0 ? "men" : "women";
CultureInfo culture = new(language == 0 ? "en" : "hr");
Thread.CurrentThread.CurrentCulture = culture; // Globalizacija (vrijeme, datum, valuta)
Thread.CurrentThread.CurrentUICulture = culture; // Lokalizacija (prijevodi)
UpdateLanguageDictionary();
var enTitle = $"{(championship == 0 ? "Men's" : "Women's")} World Cup";
var hrTitle = $"{(championship == 0 ? "Muško" : "Žensko")} Svjetsko prvenstvo";
Title = $"FIFA {(language == 0 ? enTitle : hrTitle)} {(championship == 0 ? "2018" : "2019")}";
WindowState = resolution >= SettingsWindow.resolutions.Count
? WindowState.Maximized : WindowState.Normal;
if (resolution < SettingsWindow.resolutions.Count)
(Width, Height) = SettingsWindow.resolutions[resolution];
if (!isStartup && prevChampionshipPath != UserSettings.ChampionshipPath)
Window_Loaded(null, null);
}
private async void Window_Loaded(object? sender, RoutedEventArgs? e)
{
var teams = await worldCupService.GetTeams();
firstTeamComboBox.ItemsSource = teams.Select(t => $"{t.Country} ({t.FifaCode})");
secondTeamComboBox.ItemsSource = null;
var fileName = $"favorite-{UserSettings.ChampionshipPath}-team.txt";
if (UserSettings.SettingsExist(fileName))
firstTeamComboBox.Text = UserSettings.LoadSettings(fileName)[0];
else
{
firstTeamComboBox.SelectedIndex = -1;
secondTeamComboBox.IsEnabled = false;
}
}
private async void firstTeamComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ClearPlayers();
resultLabel.Content = "vs";
secondTeamComboBox.SelectedIndex = -1;
if (firstTeamComboBox.SelectedIndex == -1) return;
var countryCode = GetComboBoxCountryCode(firstTeamComboBox);
var matches = await GetMatches(firstTeamComboBox);
secondTeamComboBox.ItemsSource = matches
.Select(m => m.HomeTeam.Code == countryCode ? m.AwayTeam : m.HomeTeam)
.Select(t => $"{t.Country} ({t.Code})");
secondTeamComboBox.IsEnabled = true;
}
private async void secondTeamComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (secondTeamComboBox.SelectedIndex == -1) return;
var matches = await GetMatches(secondTeamComboBox);
var match = matches.First(m =>
m.HomeTeam.Code == GetComboBoxCountryCode(firstTeamComboBox) &&
m.AwayTeam.Code == GetComboBoxCountryCode(secondTeamComboBox) ||
m.HomeTeam.Code == GetComboBoxCountryCode(secondTeamComboBox) &&
m.AwayTeam.Code == GetComboBoxCountryCode(firstTeamComboBox));
var (firstTeam, secondTeam) = TeamsSwapped(match) ?
(match.AwayTeam, match.HomeTeam) : (match.HomeTeam, match.AwayTeam);
resultLabel.Content = $"{firstTeam.Goals} : {secondTeam.Goals}";
LoadPlayers(match);
}
private void LoadPlayers(Match match)
{
ClearPlayers();
foreach (var (teamStatistics, teamEvents, namePrefix) in new[] {
(match.HomeTeamStatistics, match.HomeTeamEvents, TeamsSwapped(match) ? "second" : "first"),
(match.AwayTeamStatistics, match.AwayTeamEvents, TeamsSwapped(match) ? "first" : "second")
})
foreach (var player in teamStatistics.StartingEleven)
{
var playerUserControl = new PlayerUserControl(player);
var uniformGrid = (UniformGrid)FindName(namePrefix + "Team" + player.Position);
uniformGrid.Children.Add(playerUserControl);
var playerEvents = teamEvents.Where(e => e.Player == player.Name).ToList();
playerUserControl.MouseDown += (sender, e) =>
{
playerWindows.Add(new PlayerWindow(player, playerEvents));
playerWindows.Last().Show();
};
}
}
private void ClearPlayers()
{
foreach (var playerWindow in playerWindows)
playerWindow.Close();
foreach (UniformGrid uniformGrid in ((UniformGrid)FindName("fieldGrid")).Children)
uniformGrid.Children.Clear();
}
private bool TeamsSwapped(Match match)
=> match.HomeTeam.Code == GetComboBoxCountryCode(secondTeamComboBox);
private static string GetComboBoxCountryCode(ComboBox comboBox)
=> comboBox.SelectedItem.ToString()!.Split('(', ')')[1];
private Task<List<Match>> GetMatches(ComboBox comboBox)
=> worldCupService.GetMatches(GetComboBoxCountryCode(comboBox));
private static void UpdateLanguageDictionary()
{
string name = CultureInfo.CurrentUICulture.Name == "en" ? "" : ".hr";
Application.Current.Resources.MergedDictionaries.Add(new()
{
Source = new Uri($"..\\Resources\\Resources{name}.xaml", UriKind.Relative)
});
}
private void firstTeamButton_Click(object sender, RoutedEventArgs e)
{
new TeamWindow(worldCupService, firstTeamComboBox.SelectedItem.ToString()!).Show();
}
private void secondTeamButton_Click(object sender, RoutedEventArgs e)
{
new TeamWindow(worldCupService, secondTeamComboBox.SelectedItem.ToString()!).Show();
}
private void Window_Closing(object sender, CancelEventArgs e)
{
if (AppSettings.ConfirmDialogsEnabled)
e.Cancel = MessageBox.Show(FindResource("exitConfirm") as string, FindResource("exit") as string,
MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK;
}
}
}