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 Hydroplane.Exceptions;
using Hydroplane.DrawningObjects;
using Hydroplane.Generics;
using Hydroplane.MovementStrategy;
namespace Hydroplane
{
public partial class FormHydroplaneCollection : Form
{
private readonly PlanesGenericStorage _storage;
private readonly ILogger _logger;
///
/// Конструктор
///
public FormHydroplaneCollection(ILogger logger)
{
InitializeComponent();
_storage = new PlanesGenericStorage(DrawPlane.Width, DrawPlane.Height);
_logger = logger;
}
///
/// Заполнение listBoxObjects
///
private void ReloadObjects()
{
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
CollectionListBox.Items.Add(_storage.Keys[i]);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
}
///
/// Добавление набора в коллекцию
///
///
///
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetTextBox.Text))
{
MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
_logger.LogInformation($"Added set: {SetTextBox.Text}");
}
///
/// Выбор набора
///
///
///
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawPlane.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
///
/// Удаление набора
///
///
///
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
string name = CollectionListBox.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show($"Delete Object {name}?", "Deleting",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(name);
ReloadObjects();
_logger.LogInformation($"Deleted set: {name}");
}
}
private void ButtonAddPlane_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormPlaneConfig formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(AddPlane);
formPlaneConfig.Show();
}
private void AddPlane(DrawningPlane plane)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
_logger.LogWarning("Добавление пустого объекта");
return;
}
try
{
_ = obj + plane;
MessageBox.Show("Объект добавлен");
DrawPlane.Image = obj.ShowPlanes();
_logger.LogInformation($"plane added in set {CollectionListBox.SelectedItem.ToString()}");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"plane not added in set {CollectionListBox.SelectedItem.ToString()}");
}
}
///
/// Удаление объекта из набора
///
///
///
private void ButtonRemovePlane_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
try
{
int pos = Convert.ToInt32(PlaneTextBox.Text);
if (obj - pos != null)
{
MessageBox.Show("Object deleted");
DrawPlane.Image = obj.ShowPlanes();
_logger.LogInformation($"plane deleted in set {CollectionListBox.SelectedItem.ToString()}");
}
else
{
MessageBox.Show("Object not deleted");
_logger.LogWarning($"plane not deleted in set {CollectionListBox.SelectedItem.ToString()}");
}
}
catch (PlaneNotFoundException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"PlaneNotFound: {ex.Message} in set {CollectionListBox.SelectedItem.ToString()}");
}
catch (Exception ex)
{
MessageBox.Show("Not input");
_logger.LogWarning("Not input");
}
}
///
/// Обновление рисунка по набору
///
///
///
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
DrawPlane.Image = obj.ShowPlanes();
}
private void SaveToolStripMenu_Click(object sender, EventArgs e)
{
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
_storage.SaveData(SaveFileDialog.FileName);
MessageBox.Show("Saving complete", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"save in file {SaveFileDialog.FileName}");
}
catch (Exception ex)
{
MessageBox.Show($"Not saved: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Save to file {SaveFileDialog.FileName} not complete");
}
}
}
private void LoadToolStripMenu_Click(object sender, EventArgs args)
{
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
_storage.LoadData(OpenFileDialog.FileName);
MessageBox.Show("Load complete", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadObjects();
_logger.LogInformation($"load from file {OpenFileDialog.FileName}");
}
catch (Exception ex)
{
MessageBox.Show($"Not loaded: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"load from file {OpenFileDialog.FileName} not complete");
}
}
}
}
}