package Email;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.*;
import java.nio.file.Paths;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

public class emailSender {

    public static final HashMap<Integer, ArrayList<MessageToSend>> messagesToSend = new HashMap<>();
    //maps the priority levels to the corresponding emails to be sent - within each priority level

    public static Session objSession;

    private static final String strEmail = "cz3003timecrisis@gmail.com";
    private static final String strPwd = "3003liuyang";

    private static final int intMaxPriority = 3;

    static {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");

        objSession = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(strEmail, strPwd);
                    }
                });

        for (int i = 1; i <= intMaxPriority; i++) {
            messagesToSend.put(i, new ArrayList<>());
        }
    }

    public static void getEmailsToBeSent(JSONArray jsonArrayOfMails) throws IOException, ParseException {

        for (Object jsonArrayOfMail : jsonArrayOfMails) {
            JSONObject jsonObject = (JSONObject) jsonArrayOfMail;

            String from = jsonObject.get("from").toString();
            String recipients = jsonObject.get("recipients").toString();
            String subject = jsonObject.get("subject").toString();
            String messageText = jsonObject.get("messageText").toString();
            int priority = Integer.parseInt(jsonObject.get("priority").toString());

            MessageToSend m = new MessageToSend(from, recipients, subject, messageText, priority);
            messagesToSend.get(priority).add(m);
        }

//        System.out.println(messagesToSend);
    }

    public static void dispatchEmail() {

        for (Integer intKey : messagesToSend.keySet()) {
            for (int j = 0; j < messagesToSend.get(intKey).size(); j++) {

                synchronized (messagesToSend) {
                    MessageToSend m = messagesToSend.get(intKey).get(j);
                    String from = m.from;
                    String recipients = m.recipients;
                    String subject = m.subject;
                    String messageText = m.messageText;
                    int priority = m.priority;
                    try {
                        sendEmail(from, recipients, subject, messageText, priority);

                        messagesToSend.get(intKey).remove(j);
                    } catch (MessagingException ex) {
                    }
                }

            }
        }

    }

    public static void sendEmail(String from, String recipients, String subject, String messageText, int priority) throws MessagingException {

        Message message = new MimeMessage(objSession);
        message.setFrom(new InternetAddress(from));

        //Need to test the line below for multiple recipients:
        //InternetAddress[] toAddresses={new InternetAddress(strRecipient)};
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipients));

        message.setSubject(subject);
        message.setText(messageText);

        if (messageText.endsWith(".pdf")) {
            MimeBodyPart messageBodyPart = new MimeBodyPart();

            Multipart multipart = new MimeMultipart();

            messageBodyPart = new MimeBodyPart();
            String file = "path of file to be attached";
            String fileName = "report.pdf";
            DataSource source = new FileDataSource(messageText);

            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);

            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);
        }

        Transport.send(message);

    }

}

@@ -2774,32 +2774,45 @@
 %7D%0D%0A%0D%0A    public 
+synchronized 
 static void disp
@@ -2967,61 +2967,8 @@
 %0D%0A%0D%0A
-                synchronized (messagesToSend) %7B%0D%0A    
     
@@ -3041,36 +3041,32 @@
                 
-   
- 
 String from = m.
@@ -3072,20 +3072,16 @@
 .from;%0D%0A
-    
         
@@ -3131,36 +3131,32 @@
                 
-  
-  
 String subject =
@@ -3164,28 +3164,24 @@
 m.subject;%0D%0A
-    
             
@@ -3229,36 +3229,32 @@
                 
- 
-   
 int priority = m
@@ -3285,23 +3285,15 @@
     
-    
 try %7B%0D%0A
-    
     
@@ -3384,36 +3384,32 @@
                 
-   
- 
 messagesToSend.g
@@ -3447,20 +3447,16 @@
         
-    
 %7D catch 
@@ -3482,39 +3482,16 @@
  ex) %7B%0D%0A
-                    %7D%0D%0A
         
@@ -3493,34 +3493,32 @@
              %7D%0D%0A
-%0D%0A
             %7D%0D%0A 
@@ -3508,32 +3508,34 @@
 %0A            %7D%0D%0A
+%0D%0A
         %7D%0D%0A%0D%0A   

Add synchronized method