using WarmlyLocomotive.Exceptions; using WarmlyLocomotive.Generics; using Microsoft.Extensions.Logging; using System.Windows.Forms; using WarmlyLocomotive.DrawningObjects; using Serilog; namespace WarmlyLocomotive { public partial class FormWarmlyLocomotiveCollection : Form { private readonly WarmlyLocomotiveGenericStorage _storage; public FormWarmlyLocomotiveCollection() { InitializeComponent(); _storage = new WarmlyLocomotiveGenericStorage(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.Height); } private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) { listBoxStorages.SelectedIndex = 0; } else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) { listBoxStorages.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) { pictureBoxCollectionWarmlyLocomotive.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowWarmlyLocomotives(); } private void buttonDelObject_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; _storage.DelSet(name); ReloadObjects(); Log.Information($"Удален набор: {name}"); } } private void buttonAdd_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } FormWarmlyLocomotiveConfig form = new FormWarmlyLocomotiveConfig(); form.Show(); Action? warmlylocomotiveDelegate = new((warmlylocomotive) => { try { bool q = obj + warmlylocomotive; MessageBox.Show("Объект добавлен"); warmlylocomotive.ChangePictureBoxSize(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.Height); pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives(); Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); } catch (StorageOverflowException ex) { MessageBox.Show("Не удалось добавить объект"); Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); MessageBox.Show(ex.Message); } catch (ArgumentException) { Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); } }); Action? ColorDelegate = new((ship) => { MessageBox.Show(ship.ToString()); }); form.AddEvent(warmlylocomotiveDelegate); } private void buttonRemove_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.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 q = obj - pos; MessageBox.Show("Объект удален"); Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives(); } catch (WarmlyLocomotiveNotFoundException ex) { Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show(ex.Message); } catch (FormatException) { Log.Warning($"Было введено не число"); MessageBox.Show("Введите число"); } } private void buttonreFreshCollection_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives(); } 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) { listBoxStorages.Items.Add(collection); } ReloadObjects(); } catch (Exception ex) { Log.Warning("Не удалось загрузить"); MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void buttonSortByColor_Click(object sender, EventArgs e) => CompareWarmlyLocomotive(new WarmlyLocomotiveCompareByColor()); private void buttonSortByType_Click(object sender, EventArgs e) => CompareWarmlyLocomotive(new WarmlyLocomotiveCompareByType()); private void CompareWarmlyLocomotive(IComparer comparer) { if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } obj.Sort(comparer); pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives(); } } }