-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
402 lines (349 loc) · 14.8 KB
/
Form1.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
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
397
398
399
400
401
402
using Newtonsoft.Json.Linq;
using ProjetoPaisesC;
using System.Configuration;
using System.Diagnostics;
namespace ProjetoPaises_WinForms
{
public partial class Form1 : Form
{
private List<ClassCountry> countries;
private ProjetoPaisesC.ClassSQLtoC dbSQL;
private const string GitHubUsername = "EMSMoreno";
private Dictionary<string, ClassCountry> countryLookup = new Dictionary<string, ClassCountry>();
public Form1()
{
InitializeComponent();
InitializeUI("UIMode"); //Passa o value para depois termos acesso ao Dark Mode
string connectionString = "Data Source=countries.db;Version=3;";
dbSQL = new ClassSQLtoC(connectionString);
LoadCountries();
}
private async Task<List<ClassCountry>> FetchCountriesFromAPI()
{
var countriesList = new List<ClassCountry>();
try
{
using (var client = new HttpClient())
{
var response = await client.GetStringAsync("https://restcountries.com/v3.1/all");
var apiResponse = JArray.Parse(response);
foreach (var item in apiResponse)
{
// Usar JObject para deserializar os dados para a classe ClassCountry
var country = item.ToObject<ClassCountry>();
// Corrige os campos adicionais
country.Continent = country.Region; // Passa a região como continente, se aplicável
country.FlagDescription = country.Flags?.Alt; // Passa a descrição da bandeira
// Verifica se as Timezones são nulas e inicializa se necessário
if (country.Timezones == null)
{
country.Timezones = new List<string>();
}
// Verificar se Translations é nulo e inicializar se necessário
if (country.Translations == null)
{
country.Translations = new Dictionary<string, ClassCountryTranslation>();
}
// Adiciona as traduções, caso existam
var translationsObject = item["translations"];
if (translationsObject != null)
{
foreach (var translation in translationsObject.Children<JProperty>())
{
var languageCode = translation.Name; // Nome da propriedade é o código do idioma
var translationObject = translation.Value.ToObject<ClassCountryTranslation>();
country.Translations[languageCode] = translationObject;
}
}
countriesList.Add(country);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Erro ao buscar os países: {ex.Message}");
}
return countriesList;
}
private void UpdateCountryDetails(ClassCountry selectedCountry)
{
if (selectedCountry == null) return;
// Atualiza os campos de texto com os detalhes do país
txtName.Text = selectedCountry.Name.Common;
txtOfficialName.Text = selectedCountry.Name.Official;
if (selectedCountry.Currencies != null && selectedCountry.Currencies.Count > 0)
{
var currency = selectedCountry.Currencies.Values.FirstOrDefault();
txtCurrency.Text = currency != null ? currency.Name : "N/A";
txtSymbol.Text = currency != null ? currency.Symbol : "N/A";
}
else
{
txtCurrency.Text = "N/A";
txtSymbol.Text = "N/A";
}
txtCapital.Text = selectedCountry.Capital != null && selectedCountry.Capital.Count > 0 ? selectedCountry.Capital[0] : "N/A";
txtRegion.Text = selectedCountry.Region ?? "N/A";
txtSubregion.Text = selectedCountry.Subregion ?? "N/A";
txtPopulation.Text = selectedCountry.Population.ToString();
txtGiniIndex.Text = selectedCountry.Gini != null && selectedCountry.Gini.GiniValue.HasValue ? selectedCountry.Gini.GiniValue.Value.ToString() : "N/A";
txtTimezones.Text = selectedCountry.Timezones != null ? string.Join(", ", selectedCountry.Timezones) : "N/A";
txtCountryContinent.Text = selectedCountry.Continent ?? "N/A";
txtFlagDescription.Text = selectedCountry.FlagDescription ?? "N/A";
// Atualiza a imagem da bandeira se estiver disponível
if (!string.IsNullOrEmpty(selectedCountry.Flags?.Png))
{
try
{
pictureBoxFlag.Load(selectedCountry.Flags.Png);
}
catch (Exception ex)
{
MessageBox.Show($"Error while loading Country Flag: {ex.Message}");
pictureBoxFlag.Image = null; // Limpa a imagem se houver erro
}
}
else
{
pictureBoxFlag.Image = null; // Limpa a imagem se não houver URL
}
}
private async void LoadCountries()
{
progressBar.Value = 0;
try
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
progressBar.Value = 20;
countries = await FetchCountriesFromAPI();
MessageBox.Show($"Number of Loaded Countries: {countries.Count}");
dbSQL.SaveCountries(countries);
}
else
{
progressBar.Value = 50;
countries = dbSQL.GetCountries();
}
progressBar.Value = 100;
DisplayCountries(); // Atualiza a exibição dos países no ListBox
}
catch (Exception ex)
{
MessageBox.Show($"There is an error while loading Countries: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void listBoxCountries_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBoxCountries.SelectedItem != null)
{
// Encontra o país correspondente pelo Common Name
var selectedCountryName = listBoxCountries.SelectedItem.ToString();
var selectedCountry = countries.FirstOrDefault(c => c.Name.Common == selectedCountryName);
if (selectedCountry != null)
{
UpdateCountryDetails(selectedCountry);
}
}
}
private void DisplayCountries()
{
if (countries == null || countries.Count == 0)
{
listBoxCountries.DataSource = null;
listBoxCountries.Items.Clear(); // Limpa os itens da lista
return;
}
// Cria uma lista de nomes dos países
var countryNames = countries.Select(c => c.Name.Common).ToList();
// Atualiza o dicionário de mapeamento
countryLookup.Clear();
foreach (var country in countries)
{
if (!countryLookup.ContainsKey(country.Name.Common))
{
countryLookup[country.Name.Common] = country;
}
}
// Adiciona logs para verificar o conteúdo do dicionário
Debug.WriteLine("Contenu do countryLookup:");
foreach (var kvp in countryLookup)
{
Debug.WriteLine($"Name: {kvp.Key}, Country: {kvp.Value.Name.Common}");
}
// Associa a lista de nomes ao DataSource do ListBox
listBoxCountries.DataSource = countryNames;
}
#region Funções Extra
//Funcionalidade Dark Mode
private void InitializeUI(string key)
{
try
{
var uiMode = ConfigurationManager.AppSettings[key];
if (uiMode == "light")
{
btnDarkMode.Text = "Enable Light Mode";
this.ForeColor = Color.FromArgb(245, 247, 246);
this.BackColor = Color.FromArgb(3, 0, 10);
ConfigurationManager.AppSettings[key] = "dark";
}
else
{
btnDarkMode.Text = "Enable Dark Mode";
this.ForeColor = Color.FromArgb(3, 0, 10);
this.BackColor = Color.FromArgb(245, 247, 246);
ConfigurationManager.AppSettings[key] = "light";
}
}
catch
{
throw;
}
}
private void btnDarkMode_Click(object sender, EventArgs e)
{
InitializeUI("UIMode");
} // btn Dark Mode
private void btnSearchCountry_Click(object sender, EventArgs e)
{
string searchText = txtSearchCountry.Text.Trim().ToLower();
Debug.WriteLine($"Search Text: {searchText}");
if (string.IsNullOrEmpty(searchText))
{
DisplayCountries(); // Se a caixa de pesquisa estiver vazia, exiba todos os países
}
else
{
var filteredCountryNames = countryLookup
.Where(kvp => kvp.Key.ToLower().Contains(searchText))
.Select(kvp => kvp.Key)
.ToList();
Debug.WriteLine("Filtered Countries Names:");
foreach (var name in filteredCountryNames)
{
Debug.WriteLine(name);
}
// Verifica se a filtragem retornou algum resultado
if (filteredCountryNames.Count == 0)
{
MessageBox.Show("Country Not Found.", "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// Atualize a lista com os nomes dos países filtrados
listBoxCountries.DataSource = null; // Limpa o DataSource antes de atualizar
listBoxCountries.DataSource = filteredCountryNames;
}
} //btn para procurar país
private void btnCleanCountry_Click(object sender, EventArgs e)
{
txtSearchCountry.Text = string.Empty;
} // btn Limpar país procurado
//Github
private void ShowGithub(string username)
{
try
{
string url = $"https://github.com/{username}";
Process.Start(new ProcessStartInfo(url)
{
UseShellExecute = true
});
}
catch (Exception ex)
{
MessageBox.Show($"Erro ao abrir o perfil do GitHub: {ex.GetType().ToString()}\n{ex.Message}");
}
}
private void btnGithub_Click(object sender, EventArgs e)
{
ShowGithub(GitHubUsername);
} // btn abrir Github
//Função para traduzir Japonês
private void ShowTranslationPopup(ClassCountry country, string languageCode)
{
if (country.Translations != null && country.Translations.ContainsKey(languageCode))
{
var translation = country.Translations[languageCode];
string translatedName = translation.Common;
string message = $"Translated Name to Japanese: {translatedName}";
MessageBox.Show(message, "Country Name Translation", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Translation not available for this Country!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void btnJapanese_Click(object sender, EventArgs e)
{
if (listBoxCountries.SelectedItem != null)
{
var selectedCountryName = listBoxCountries.SelectedItem.ToString();
var selectedCountry = countries.FirstOrDefault(c => c.Name.Common == selectedCountryName);
if (selectedCountry != null)
{
ShowTranslationPopup(selectedCountry, "jpn");
}
else
{
MessageBox.Show("Selected a valid Country!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("Select a Country first!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} //btn Japonês
private void SortCountriesByNameAZ() // Ordenar Países A -> Z
{
if (countries != null && countries.Count > 0)
{
countries = countries.OrderBy(c => c.Name.Common).ToList();
DisplayCountries(); // Atualiza a exibição após a ordenação
}
}
private void SortCountriesByNameZA() // Ordenar Países Z -> A
{
if (countries != null && countries.Count > 0)
{
countries = countries.OrderByDescending(c => c.Name.Common).ToList();
DisplayCountries();// Atualiza a exibição após a ordenação
}
}
//Order By
private void btnSortAZ_Click(object sender, EventArgs e)
{
SortCountriesByNameAZ();
} //Ascending
private void btnSortZA_Click(object sender, EventArgs e)
{
SortCountriesByNameZA();
} //Descending
//Limpar Textboxes
private void ClearTextBoxes()
{
txtName.Text = "";
txtOfficialName.Text = "";
txtCurrency.Text = "";
txtSymbol.Text = "";
txtCapital.Text = "";
txtRegion.Text = "";
txtSubregion.Text = "";
txtPopulation.Text = "";
txtGiniIndex.Text = "";
txtFlagDescription.Text = "";
txtCountryContinent.Text = "";
txtTimezones.Text = "";
}
//Btn para limpar textboxes
private void btnCleanForm_Click(object sender, EventArgs e)
{
ClearTextBoxes();
}
//Btn para carregar países
private void btnLoadCountries_Click(object sender, EventArgs e)
{
LoadCountries();
}
#endregion
}
}