using WarmlyLocomotive.Generics;
using WarmlyLocomotive.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;
namespace WarmlyLocomotive
{
public partial class FormWarmlyLocomotiveCollection : Form
{
///
/// Набор объектов
///
private readonly WarmlyLocomotivesGenericStorage _storage;
public FormWarmlyLocomotiveCollection()
{
InitializeComponent();
_storage = new WarmlyLocomotivesGenericStorage(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.Height);
}
///
/// Заполнение listBoxObjects
///
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]);
}
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 listBoxStorages_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)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
///
/// Добавление объекта в набор
///
///
///
private void buttonAdd_Click(object sender, EventArgs e)
{
var formWarmlyLocomotiveConfig = new FormWarmlyLocomotiveConfig();
formWarmlyLocomotiveConfig.AddEvent(Addwarmlylocomotive);
formWarmlyLocomotiveConfig.Show();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
WarmlyLocomotiveForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (obj + form.SelectedWarmlyLocomotive)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
///
/// Удаление объекта из набора
///
///
///
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;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
}
else
{
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 buttonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
}
}