using Microsoft.AspNetCore.Identity.UI.Services;
using System.Net.Mail;
using System.Net;
using songrequests_blazor.Settings;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Identity;
using MimeKit.Text;
using MimeKit;

namespace songrequests_blazor.Services
{
    public class EmailSender : IEmailSender<IdentityUser>
    {

        protected readonly Mailserver _serverSettings;
        public EmailSender(IOptions<Mailserver> options)
        {
            _serverSettings = options.Value;
        }
        public Task SendConfirmationLinkAsync(IdentityUser user, string email, string confirmationLink)
        {
            return SendEmailAsync(email, "Your confirmation link", $"Link: {confirmationLink}");
        }


        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            //return SendWithSmtpClientAsync(email,subject, htmlMessage);
            return SendWithMailKitAsync(email,subject, htmlMessage);
        }

        private Task SendWithMailKitAsync(string email, string subject, string htmlMessage)
        {
            var msg = new MimeMessage();
            msg.From.Add(MailboxAddress.Parse(_serverSettings.FromEmail));
            msg.To.Add(MailboxAddress.Parse(email));
            msg.Subject = subject;
            msg.Body = new TextPart(TextFormat.Plain) { Text = htmlMessage};

            using var smtp = new MailKit.Net.Smtp.SmtpClient();
            smtp.Connect(_serverSettings.Host, _serverSettings.Port, _serverSettings.EnableSSL);
            smtp.Authenticate(_serverSettings.Username, _serverSettings.Password);
            smtp.Send(msg);
            smtp.Disconnect(true);
            return Task.CompletedTask;
            //return smtp.DisconnectAsync(true);
        }

        private Task SendWithSmtpClientAsync(string email, string subject, string htmlMessage)
        {
            SmtpClient client = new SmtpClient
            {
                Port = _serverSettings.Port,
                Host = _serverSettings.Host,
                EnableSsl = _serverSettings.EnableSSL,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = _serverSettings.Username == null,
                Credentials = _serverSettings.Username != null ? new NetworkCredential(_serverSettings.Username, _serverSettings.Password) : null

            };
            var msg = new MailMessage(_serverSettings.FromEmail, email, subject, htmlMessage);
            msg.IsBodyHtml = false;

            return client.SendMailAsync(msg);
        }

        public Task SendPasswordResetCodeAsync(IdentityUser user, string email, string resetCode)
        {
            return SendEmailAsync(email, "Your reset code", $"Reset Code: {resetCode}");
        }

        public Task SendPasswordResetLinkAsync(IdentityUser user, string email, string resetLink)
        {
            return SendEmailAsync(email, "Your password reset link", $"Reset link: {resetLink}");
        }
    }
}
