package com.itsol.quantrivanphong.access.register.bussiness;

import com.itsol.quantrivanphong.access.register.dto.EmailDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


@Component
public class EmailBussiness {

    @Autowired
    private JavaMailSender sender;




    public boolean sendEmail(EmailDTO emailDTO)  {
        boolean check;

//        MimeMessage message = sender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(message);
//
//
//        try {
//            helper.setTo(emailDTO.getRecipientEmail());
//            helper.setText(emailDTO.getMessage(),"text/html");
//            helper.setSubject(emailDTO.getSubject());
//            sender.send(message);
//            check = true;
//        } catch (Exception e) {
//            e.printStackTrace();
//            check = false;
//        }

        // Recipient's email ID needs to be mentioned.
        String to = emailDTO.getRecipientEmail();

        // Sender's email ID needs to be mentioned
        String from = "hieunv2496@gmail.com";
        final String username = "hieunv2496@gmail.com";//change accordingly
        final String password = "anhieu1996";//change accordingly




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

        // Get the Session object.
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            // Create license default MimeMessage object.
            Message message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));

            // Set Subject: header field
            message.setSubject(emailDTO.getSubject());

            // Send the actual HTML message, as big as you like
            message.setContent(
                    emailDTO.getMessage(),
                    "text/html");

            // Send message
            Transport.send(message);

            System.out.println("Sent message successfully....");

        } catch (MessagingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


        return true;
    }
}
