using MailKit.Net.Smtp;
using Microsoft.Extensions.Options;
using MimeKit;
using MTWorkHR.Core.Email;
using MTWorkHR.Core.Entities;
using MTWorkHR.Core.Global;
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTWorkHR.Infrastructure.EmailService
{
    public class MailSender : IMailSender
    {
        public AppSettingsConfiguration _configuration { get; }
        public MailSender(AppSettingsConfiguration configuration) 
        {
            _configuration = configuration;
        }
        public async Task<bool> SendEmailGrid(EmailMessage email)
        {
            var client = new SendGridClient(_configuration.MailSettings.ApiKey);
            var to = new EmailAddress(email.To);
            var from = new EmailAddress
            {
                Email = _configuration.MailSettings.FromAddress,
                Name = _configuration.MailSettings.FromName
            };
            email.Body = email.Body + "  /n" + email.url;
            var message = MailHelper.CreateSingleEmail(from, to, email.Subject, email.Body, email.Body);
            var response = await client.SendEmailAsync(message);

            return response.IsSuccessStatusCode;
        }

        public async Task<bool> SendEmail(EmailMessage email)
        {
            var mailTo = email.To;
            var settings = _configuration.MailSettings;
            if (settings.FromAddress == null || settings.FromAddress== "" || settings.Password == null || settings.TemplatePath == null || settings.Host == null || settings.Port == 0 || mailTo == null)
                return false;

            //string FilePath = _configuration.MailSettings.TemplatePath;
            //StreamReader str = new StreamReader(FilePath);
            //string MailText = str.ReadToEnd();
            //str.Close();

            //MailText = MailText.Replace("[NotificationType]", email.Subject).Replace("[Info]", email.Body).Replace("[Link]", email.url );

            var emailObj = new MimeMessage();
            emailObj.Sender = MailboxAddress.Parse(_configuration.MailSettings.FromAddress);
            emailObj.To.Add(MailboxAddress.Parse(mailTo));
            emailObj.Subject = email.Subject;

            var builder = new BodyBuilder();
            email.Body = email.Body + "  /n" + email.url;

            builder.HtmlBody = email.Body;
            emailObj.Body = builder.ToMessageBody();
            try
            {
                using var smtp = new SmtpClient();
                smtp.Connect(_configuration.MailSettings.Host, _configuration.MailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
                smtp.Authenticate(_configuration.MailSettings.FromAddress, _configuration.MailSettings.Password);
                await smtp.SendAsync(emailObj);
                smtp.Disconnect(true);
            }catch (Exception ex) { var ms = ex.Message; }
            return true;
        }
    }
}
