using MimeKit;
using MailKit.Net.Smtp;
using MailKit.Security;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Reflection;
using System.Linq;

namespace UserManagement.Helper
{
    public class EmailHelper
    {
        public static async Task<bool> SendEmail(SendEmailSpecification sendEmailSpecification)
        {
            try
            {
                var message = new MimeMessage();

                // Sender information
                message.From.Add(new MailboxAddress(sendEmailSpecification.UserName ?? sendEmailSpecification.UserName, sendEmailSpecification.FromAddress));

                // Recipient information
                message.To.Add(new MailboxAddress("Recipient Name", sendEmailSpecification.ToAddress));

                // Subject
                message.Subject = sendEmailSpecification.Subject;

                if (sendEmailSpecification.Attechments.Count > 0)
                {

                    var body = new TextPart("html")
                    {
                        Text = sendEmailSpecification.Body
                    };

                    // Create a multipart email to hold both the body and attachment

                    var multipart = new Multipart("mixed")
                    {
                        body // Add the body part
                    };
                    foreach (var file in sendEmailSpecification.Attechments)
                    {
                        string fileData = file.Src.Split(',').LastOrDefault();
                        byte[] bytes = Convert.FromBase64String(fileData);

                        var ms = new MemoryStream(bytes);
                        var attachment = new MimePart("application", "octet-stream")
                        {
                            Content = new MimeContent(ms),
                            ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                            ContentTransferEncoding = ContentEncoding.Base64,
                            FileName = file.Name,
                        };

                        multipart.Add(attachment);
                    }

                    message.Body = multipart;
                }
                else
                {
                    message.Body = new TextPart("html")
                    {
                        Text = sendEmailSpecification.Body
                    };
                }

                // Set the email body to the multipart content


                // SMTP server configuration
                using (var client = new SmtpClient())
                {
                    await client.ConnectAsync(sendEmailSpecification.Host, sendEmailSpecification.Port, SecureSocketOptions.Auto); // SMTP server and port
                    await client.AuthenticateAsync(sendEmailSpecification.UserName, sendEmailSpecification.Password); // SMTP credentials

                    // Send the email
                    client.Timeout = 30000;
                    await client.SendAsync(message);
                    await client.DisconnectAsync(true); // Disconnect from the server
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}



