using Airbus_Base.DrawningObjects; using Airbus_Base.Generics; using Airbus_Base.MovementStrategy; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Airbus_Base.Exceptions; using Microsoft.Extensions.Logging; using System.Xml.Linq; using Serilog; namespace Airbus_Base { /// /// Форма для работы с набором объектов класса DrawningAirbus /// public partial class FormAirplaneCollection : Form { /// /// Набор объектов /// private readonly TheAirplaneGenericStorage _storage; /// /// Конструктор /// public FormAirplaneCollection() { InitializeComponent(); _storage = new TheAirplaneGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } /// /// Заполнение listBoxObjects /// private void ReloadObjects() { int index = listBoxObjects.SelectedIndex; listBoxObjects.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { listBoxObjects.Items.Add(_storage.Keys[i].Name); } if (listBoxObjects.Items.Count > 0 && (index == -1 || index >= listBoxObjects.Items.Count)) { listBoxObjects.SelectedIndex = 0; } else if (listBoxObjects.Items.Count > 0 && index > -1 && index < listBoxObjects.Items.Count) { listBoxObjects.SelectedIndex = index; } } /// /// Добавление набора в коллекцию /// /// /// private void ButtonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); Log.Information($"Добавлен набор: {textBoxStorageName.Text}"); } /// /// Выбор набора /// /// /// private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxCollection.Image = _storage[listBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowTheAirplanes(); } /// /// Удаление набора /// /// /// private void ButtonDelObject_Click(object sender, EventArgs e) { if (listBoxObjects.SelectedIndex == -1) { return; } if (MessageBox.Show($"Удалить объект{listBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { string name = (listBoxObjects.SelectedItem.ToString() ?? string.Empty); _storage.DelSet(name); ReloadObjects(); Log.Information($"Удален набор: {name}"); } } /// /// Добавление объекта в набор /// /// /// private void ButtonAddAirplane_Click(object sender, EventArgs e) { if (listBoxObjects.SelectedIndex == -1) { return; } var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } FormAirplaneConfig form = new(); form.Show(); Action? airplaneDelegate = new((airplane) => { try { bool isAdditionSuccessful = obj + airplane; MessageBox.Show("Объект добавлен"); airplane.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); pictureBoxCollection.Image = obj.ShowTheAirplanes(); Log.Information($"Добавлен объект в коллекцию {listBoxObjects.SelectedItem.ToString() ?? string.Empty}"); } catch (ArgumentException ex) { Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); } }); form.AddEvent(airplaneDelegate); } /// /// Удаление объекта из набора /// /// /// private void ButtonDeleteAirplane_Click(object sender, EventArgs e) { if (listBoxObjects.SelectedIndex == -1) { return; } var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } try { int pos = Convert.ToInt32(maskedTextBoxNumber.Text); var isAdditionSuccessful = obj - pos; MessageBox.Show("Объект удален"); Log.Information($"Удален объект из коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); pictureBoxCollection.Image = obj.ShowTheAirplanes(); } catch (AirplaneNotFoundException ex) { Log.Warning($"Не получилось удалить объект из коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show(ex.Message); } catch (FormatException) { Log.Warning($"Было введено не число"); MessageBox.Show("Введите число"); } } /// /// Обновление рисунка по набору /// /// /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (listBoxObjects.SelectedIndex == -1) { return; } var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } pictureBoxCollection.Image = obj.ShowTheAirplanes(); } /// /// Обработка нажатия "Сохранение" /// /// /// private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен"); } catch (Exception ex) { Log.Warning("Не удалось сохранить"); MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// /// Обработка нажатия "Загрузка" /// /// /// private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { try { _storage.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); Log.Information($"Файл {openFileDialog.FileName} успешно загружен"); foreach (var collection in _storage.Keys) { listBoxObjects.Items.Add(collection); } ReloadObjects(); } catch (Exception ex) { Log.Warning("Не удалось загрузить"); MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByType()); private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor()); private void CompareAirplanes(IComparer comparer) { if (listBoxObjects.SelectedIndex == -1) { return; } var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } obj.Sort(comparer); pictureBoxCollection.Image = obj.ShowTheAirplanes(); } } }