using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.SearchModels;
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 BlacksmithWorkshop
{
	public partial class ViewMailForm : Form
	{
		private readonly ILogger _logger;
		private readonly IMessageInfoLogic _logic;
		private int currentPage = 1;
		private int pageLength = 3;
		public ViewMailForm(ILogger<ViewMailForm> logger, IMessageInfoLogic logic)
		{
			InitializeComponent();
			_logger = logger;
			_logic = logic;
		}
		private void ViewMailForm_Load(object sender, EventArgs e)
		{
			LoadData();
			numericUpDownPage.Value = pageLength;
			pageTextBox.Text = currentPage.ToString();
		}
		private void LoadData()
		{
			try
			{
				var list = _logic.ReadList(new MessageInfoSearchModel()
				{
					PageLength = pageLength,
					PageIndex = currentPage
				});
				if (list != null)
				{
					dataGridView.DataSource = list;
					dataGridView.Columns["ClientId"].Visible = false;
					dataGridView.Columns["MessageId"].Visible = false;
					dataGridView.Columns["ReplyMessageId"].Visible = false;
					dataGridView.Columns["Reply"].Visible = false;
					dataGridView.Columns["IsReply"].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 buttonOpen_Click(object sender, EventArgs e)
		{
			if (dataGridView.SelectedRows.Count <= 0)
				return;
			var service = Program.ServiceProvider?.GetService(typeof(FormLetter));
			if (service is FormLetter form)
			{
				string? messageId = dataGridView.SelectedRows[0].Cells["MessageId"].Value.ToString();
				if (messageId == null) return;
				form.messageId = messageId;
				if (!Convert.ToBoolean(dataGridView.SelectedRows[0].Cells["IsReaded"].Value))
				{
					_logic.Update(new MessageInfoBindingModel
					{
						MessageId = messageId,
						IsReaded = true,
						ReplyMessageId = dataGridView.SelectedRows[0].Cells["ReplyMessageId"].Value?.ToString()
					});
				}
				form.ShowDialog();
				LoadData();
			}
		}
		private void buttonPreveous_Click(object sender, EventArgs e)
		{
			currentPage = Math.Max(1, currentPage - 1);
			pageTextBox.Text = currentPage.ToString();
			LoadData();
		}
		private void buttonNext_Click(object sender, EventArgs e)
		{
			currentPage++;
			pageTextBox.Text = currentPage.ToString();
			LoadData();
		}
		private void numericUpDownPage_ValueChanged(object sender, EventArgs e)
		{
			pageLength = Math.Max(1, (int)numericUpDownPage.Value);
			LoadData();
		}
	}
}
