using BlacksmithWorkshopBusinessLogic.BusinessLogic;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
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 FormMails : Form
    {
        private readonly ILogger _logger;

        private readonly IMessageInfoLogic _messageLogic;

        private int numberPaginationPage;

        private int paginationSize;

        public FormMails(ILogger<FormMails> logger, IMessageInfoLogic messageLogic)
        {
            InitializeComponent();

            _logger = logger;
            _messageLogic = messageLogic;

            paginationSize = 10;
        }

        private void FormMails_Load(object sender, EventArgs e)
        {
            numberPaginationPage = 1;

            LoadData();
        }

        private void LoadData()
        {
            _logger.LogInformation("Загрузка писем");

            try
            {
                var list = _messageLogic.ReadList(null);

                //проверка на то, чтобы не стали выводить несуществующие элементы списка 
                if ((numberPaginationPage - 1) * 10 + paginationSize > list.Count)
                {
                    paginationSize = list.Count - (numberPaginationPage - 1) * 10;
                }

                //получение писем, согласно номеру страницы пагинации
                var paginationList = list.GetRange((numberPaginationPage - 1) * 10, paginationSize);

                if (list != null)
                {
                    dataGridView.DataSource = paginationList;
                    dataGridView.Columns["MessageId"].Visible = false;
                    dataGridView.Columns["ClientId"].Visible = false;
                    dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }

                _logger.LogInformation("Успешная загрузка писем");

                paginationSize = 10;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка загрузки писем");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonForward_Click(object sender, EventArgs e)
        {
            if (dataGridView.Rows.Count == paginationSize)
            {
                numberPaginationPage += 1;
            }

            LoadData();
        }

        private void ButtonBack_Click(object sender, EventArgs e)
        {
            if (numberPaginationPage > 1)
            {
                numberPaginationPage -= 1;
            }

            LoadData();
        }

        private void ButtonAnswer_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count == 1)
            {
                var service = Program.ServiceProvider?.GetService(typeof(FormAnswerMail));

                if (service is FormAnswerMail form)
                {
                    form.MessageId = dataGridView.SelectedRows[0].Cells["MessageId"].Value.ToString();

                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
            }
        }
    }
}
