using AircraftPlantBusinessLogic.MailWorker;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.ViewModels;
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;
namespace AircraftPlantView
{
///
/// Форма для отправки письма
///
public partial class FormMessage : Form
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для писем
///
private readonly IMessageInfoLogic _messageLogic;
///
/// Бизнес-логика для отправки писем
///
private readonly AbstractMailWorker _mailWorker;
///
/// Письмо
///
private MessageInfoViewModel? _message;
///
/// Идентификатор сообщения
///
public string MessageId { get; set; } = string.Empty;
///
/// Конструктор
///
///
///
///
public FormMessage(ILogger logger, IMessageInfoLogic messageLogic, AbstractMailWorker mailWorker)
{
InitializeComponent();
_logger = logger;
_messageLogic = messageLogic;
_mailWorker = mailWorker;
}
///
/// Загрузка данных
///
///
///
private void FormMessage_Load(object sender, EventArgs e)
{
try
{
_message = _messageLogic.ReadElement(new MessageInfoSearchModel
{
MessageId = MessageId
});
if (_message != null)
{
textBoxSubject.Text = _message.Subject;
textBoxBody.Text = _message.Reply;
if (!_message.IsChecked)
{
_messageLogic.Update(new MessageInfoBindingModel
{
MessageId = MessageId,
IsChecked = true,
Reply = _message.Reply
});
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения собщения");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Кнопка "Отправить"
///
///
///
private void buttonSend_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxBody.Text))
{
MessageBox.Show("Заполните ответ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mailWorker.MailSendAsync(new()
{
MailAddress = _message!.SenderName,
Subject = _message.Subject,
Text = textBoxBody.Text,
});
_messageLogic.Update(new MessageInfoBindingModel
{
MessageId = MessageId,
IsChecked = true,
Reply = textBoxBody.Text
});
MessageBox.Show("Отправка прошла успешно", "Сообщение", MessageBoxButtons.OK);
DialogResult = DialogResult.OK;
Close();
}
///
/// Кнопка "Отмена"
///
///
///
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}