using BarContracts.BindingModels;
using BarContracts.BusinessLogicContracts;
using MailKit.Net.Pop3;
using MailKit.Security;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace BarBusinessLogic.MailWorker
{
    public class MailKitWorker : AbstractMailWorker
    {
        public MailKitWorker(ILogger<MailKitWorker> Logger, IMessageInfoLogic MessageInfoLogic)
            : base(Logger, MessageInfoLogic) { }

        protected override async Task SendMailImpl(MailSendInfoBindingModel Info)
        {
            using var MailMessage = new MailMessage();
            using var SmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort);

            try
            {
                MailMessage.From = new MailAddress(_mailLogin);
                MailMessage.To.Add(new MailAddress(Info.MailAddress));
                MailMessage.Subject = Info.Subject;
                MailMessage.Body = Info.Text;
                MailMessage.SubjectEncoding = Encoding.UTF8;
                MailMessage.BodyEncoding = Encoding.UTF8;

                SmtpClient.UseDefaultCredentials = false;
                SmtpClient.EnableSsl = true;
                SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                SmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword);

                await Task.Run(() => SmtpClient.Send(MailMessage));
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected override async Task<List<MessageInfoBindingModel>> ReceiveMailImpl()
        {
            var Result = new List<MessageInfoBindingModel>();

            using var Client = new Pop3Client();

            await Task.Run(() =>
            {
                try
                {
                    Client.Connect(_popHost, _popPort, SecureSocketOptions.SslOnConnect);
                    Client.Authenticate(_mailLogin, _mailPassword);

                    for (int i = 0; i < Client.Count; i++)
                    {
                        var Message = Client.GetMessage(i);
                        

                        foreach (var Mail in Message.From.Mailboxes)
                        {
                            //if (Mail.Address.Contains("o.filippova"))
                               Result.Add(new MessageInfoBindingModel
                                {
                                    DateDelivery = Message.Date.DateTime,
                                    MessageId = Message.MessageId,
                                    SenderName = Mail.Address,
                                    Subject = Message.Subject,
                                    Body = Message.TextBody
                                });
                        }
                    }
                }
                catch (AuthenticationException)
                { }
                finally
                {
                    Client.Disconnect(true);
                }
            });

            return Result;
        }
    }
}
