using Microsoft.Extensions.Logging; 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 Serilog; namespace Catamaran { public partial class FormCatamaranCollection : Form { private readonly CatamaransGenericStorage _storage; public FormCatamaranCollection() { InitializeComponent(); _storage = new CatamaransGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } private void ReloadObjects() { int index = SetslistBox.SelectedIndex; SetslistBox.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { SetslistBox.Items.Add(_storage.Keys[i]); } if (SetslistBox.Items.Count > 0 && (index == -1 || index >= SetslistBox.Items.Count)) { SetslistBox.SelectedIndex = 0; } else if (SetslistBox.Items.Count > 0 && index > -1 && index < SetslistBox.Items.Count) { SetslistBox.SelectedIndex = index; } } private void ButtonAddStorage_Click(object sender, EventArgs e) { string storname = setAddBox.Text; if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(setAddBox.Text); ReloadObjects(); } private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxCollection.Image = _storage[SetslistBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCats(); } private void ButtonDeleteStorage_Click(object sender, EventArgs e) { if (SetslistBox.SelectedIndex == -1) { return; } if (MessageBox.Show($"Удалить набор {SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _storage.DelSet(SetslistBox.SelectedItem.ToString() ?? string.Empty); ReloadObjects(); } } private void ButtonAddCatamaran_Click(object sender, EventArgs e) { if (SetslistBox.SelectedIndex == -1) { return; } var obj = _storage[SetslistBox.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } FormCatamaranConfig form = new(); form.Show(); Action? catamaranDelegate = new((c) => { try { bool q = obj + c; MessageBox.Show("Объект добавлен"); Log.Information($"Добавлен объект в коллекцию {SetslistBox.SelectedItem.ToString() ?? string.Empty}"); c.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height); pictureBoxCollection.Image = obj.ShowCats(); } catch (StorageOverflowException ex) { Log.Warning($"Коллекция {SetslistBox.SelectedItem.ToString() ?? string.Empty} переполнена"); MessageBox.Show(ex.Message); } }); form.AddEvent(catamaranDelegate); } private void ButtonRemoveCatamaran_Click(object sender, EventArgs e) { if (SetslistBox.SelectedIndex == -1) { return; } var obj = _storage[SetslistBox.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } try { int pos = Convert.ToInt32(maskedTextBox.Text); var q = obj - pos; MessageBox.Show("Объект удален"); Log.Information($"Удален объект из коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); pictureBoxCollection.Image = obj.ShowCats(); } catch (CatamaranNotFoundException ex) { Log.Warning($"Не получилось удалить объект из коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show(ex.Message); } catch (FormatException ex) { Log.Warning($"Было введено не число"); MessageBox.Show("Введите число"); } } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (SetslistBox.SelectedIndex == -1) { return; } var obj = _storage[SetslistBox.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } pictureBoxCollection.Image = obj.ShowCats(); } 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) { SetslistBox.Items.Add(collection); } ReloadObjects(); } catch (Exception ex) { Log.Warning("Не удалось загрузить"); MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }