using Microsoft.Extensions.Logging;
using ShipyardContracts.BusinessLogicsContracts;
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 ShipyardView
{
    public partial class FormMails : Form
    {
        private readonly ILogger _logger;
        private int pageCount = 0;
        private readonly int countPerPage = 5;
        private readonly IMessageInfoLogic _logic;

        public FormMails(ILogger<FormMails> logger, IMessageInfoLogic logic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = logic;
            
        }
        private void LoadData(int page)
        {
            try
            {
                var list = _logic.ReadList(new() { Page = page, PageCount = countPerPage });
                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 FormMails_Load(object sender, EventArgs e)
        {
            LoadData(0);
        }

        private void ButtonBack_Click(object sender, EventArgs e)
        {
            pageCount = pageCount > 0 ? pageCount - 1 : 0;
            LoadData(pageCount);
        }

        private void ButtonForward_Click(object sender, EventArgs e)
        {
            pageCount++;
            LoadData(pageCount);
        }

        private void ButtonOpen_Click(object sender, EventArgs e)
        {

        }
    }
}

