package ie.gmit.sw.controllers;

import ie.gmit.sw.domain.MailMsg;
import ie.gmit.sw.services.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

    private EmailService emailService;

    @Autowired
    public MainController(EmailService emailService) {
        this.emailService = emailService;
    }

    @PostMapping("/sendemail")
    public String sendEmail(@RequestBody MailMsg mailMessage){
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom(mailMessage.getFrom());
        mail.setTo((String[])mailMessage.getTo().toArray());
        mail.setSubject(mailMessage.getSubject());
        mail.setReplyTo(mailMessage.getReplyTo());
        mail.setCc((String[])mailMessage.getCc().toArray());
        mail.setBcc((String[])mailMessage.getBcc().toArray());
        mail.setSentDate(mailMessage.getSentDate());
        mail.setText(mailMessage.getText());
        try {
            emailService.sendEmail(mail);
        }catch (Exception e){
            return e.getMessage();
        }

        return new String("Email sent success!");

    }

}

@@ -399,16 +399,68 @@
 oller;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 @RestCon
@@ -866,28 +866,16 @@
     
-mail.setTo((
 String%5B%5D
 )mai
@@ -862,33 +862,38 @@
         String%5B%5D
-)
+ to = 
 mailMessage.getT
@@ -896,25 +896,70 @@
 getTo().
-toArray()
+stream().toArray(String%5B%5D::new);%0A        mail.setTo(to
 );%0A     
@@ -1067,28 +1067,61 @@
     
+if(
 mail
-.setCc((
+Message.getCc() != null)%7B%0A            
 String%5B%5D
 )mai
@@ -1108,33 +1108,38 @@
         String%5B%5D
-)
+ cc = 
 mailMessage.getC
@@ -1138,32 +1138,41 @@
 age.getCc().
+stream().
 toArray(
 ));%0A        
@@ -1163,41 +1163,124 @@
 ray(
-));%0A        mail.setBcc((
+String%5B%5D::new);%0A            mail.setCc(cc);%0A        %7D%0A        if(mailMessage.getBcc() != null)%7B%0A            
 String%5B%5D
 )mai
@@ -1275,17 +1275,23 @@
 String%5B%5D
-)
+ bcc = 
 mailMess
@@ -1303,24 +1303,33 @@
 etBcc().
+stream().
 toArray(
 ));%0A    
@@ -1320,19 +1320,71 @@
 toArray(
-));
+String%5B%5D::new);%0A            mail.setBcc(bcc);%0A        %7D
 %0A       

convert list to string array