using BarContracts.BusinessLogicContracts;
using Microsoft.Extensions.Logging;

namespace BarView.Forms
{
	public partial class FormMail : Form
	{
		private readonly ILogger _logger;
		private readonly IMessageInfoLogic _messageLogic;

		public FormMail(ILogger<FormMail> Logger, IMessageInfoLogic MessageLogic)
		{
			InitializeComponent();
			
			_logger = Logger;
			_messageLogic = MessageLogic;
		}

		private void LoadData()
		{
			try
			{
				var List = _messageLogic.ReadList(null);
				
				if (List != null)
				{
					DataGridView.DataSource = List;
					DataGridView.Columns["MessageId"].Visible = false;
					DataGridView.Columns["ClientId"].Visible = false;
					DataGridView.Columns["Body"].AutoSizeMode =	DataGridViewAutoSizeColumnMode.Fill;
				}
				
				_logger.LogInformation("Загрузка почтовых собщений");
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Ошибка загрузки почтовых сообщений");
				MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void FormMail_Load(object sender, EventArgs e)
		{
			LoadData();
		}
	}
}
