package _VisualDVM.Repository.Server;
import Common.Utils.Pair;
import Common.Utils.Utils_;
import _VisualDVM.Constants;
import _VisualDVM.Repository.EmailMessage;
import _VisualDVM.Utils;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Properties;
public class EmailChecker extends RepositoryServerClient {
    RepositoryServer server;
    public EmailChecker(RepositoryServer server_in) {
        server = server_in;
    }
    @Override
    protected RepositoryServer getServer() {
        return server;
    }
    @Override
    protected int getSleepMillis() {
        return 5000;
    }
    @Override
    protected void perform() throws Exception {
        Pair<String, EmailMessage> messagePair = (Pair<String, EmailMessage>) ServerCommand(ServerCode.GetFirstEmailToSend);
        if (messagePair != null) {
            SendEmail(messagePair.getKey(), messagePair.getValue());
        }
    }
    public void SendEmail(String address_in, EmailMessage message_in) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", Constants.SMTPHost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", String.valueOf(Constants.SMTPPort));
        props.put("mail.smtp.socketFactory.port", String.valueOf(Constants.MailSocketPort));
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.connectiontimeout", String.valueOf(15000));
        props.put("mail.smtp.timeout", String.valueOf(15000));
        props.put("mail.smtp.writetimeout", String.valueOf(15000));
        //------------------------------
        LinkedHashMap<String, File> innerFiles = new LinkedHashMap<>();
        for (String aName : message_in.files.keySet()) {
            File f = Utils.getTempFileName(aName);
            Utils_.bytesToFile(message_in.files.get(aName), f);
            innerFiles.put(aName, f);
        }
        //------------------------------
        Session session = Session.getDefaultInstance(props,
                new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                Constants.MailAddress,
                                Constants.MailPassword);
                    }
                });
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(Constants.MailAddress));
        message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_in));
        message.setSubject(message_in.subject);
        Multipart multipart = new MimeMultipart();
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(message_in.text);
        multipart.addBodyPart(textBodyPart);
        for (String aName : innerFiles.keySet()) {
            MimeBodyPart attachmentBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(innerFiles.get(aName));
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            attachmentBodyPart.setFileName(aName);
            multipart.addBodyPart(attachmentBodyPart);
        }
        message.setContent(multipart);
        Transport.send(message);
    }
}

