package BOLO.services;


import java.io.ByteArrayOutputStream;

import javax.activation.DataSource;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import BOLO.model.Bolo;
import BOLO.model.BoloUser;

@Component
public class EmailServiceImpl {
    @Autowired
    public JavaMailSender emailSender;
    @Autowired 
    private BoloPdfGenerationService boloPdfGenerationService;

    public void sendSimpleMessage(String to, String subject, String text) {
       
        SimpleMailMessage message = new SimpleMailMessage(); 
        message.setTo(to); 
        message.setSubject(subject); 
        message.setText(text);
        emailSender.send(message);
       
    }

    public void sendCreateBoloNotification(String to,String link){
        String subject = "BOLO Alert: Confirm BOLO";
        String body = "Your BOLO was created but not confirmed. Click on the link below to confirm:\n";
        body += link;

        sendSimpleMessage(to, subject, body);

    }
    
    public void sendCreateUserNotification(String to,String link, BoloUser user, String password){                  
    	    	
        String subject = "NEW BOLO Account Has Been Created For " + user.getFirstname() + user.getLastname();
        String body = "Congratulations! An account has been made for you on our system! Please click on the link below to login to our system:\n";
        body += link;
        body = body +
        		"\n **The following information is deemed sensitive**:\r\n" + 
        		"\r\n" + 
        		"Your username is: \r\n" + user.getUsername() +
        		"\r\n" + 
        		"Your first time password is:\r\n" + password +
        		"\r\n" + 
        		"Please login to the BOLO System and follow the instructions to finish setting up your account";

        sendSimpleMessage(to, subject, body);

    }
    
    public void passwordResetNotification(String to,String link, BoloUser user, String password){                  
    	
        String subject = "Password Has Been Reset For: " + user.getFirstname() + user.getLastname();
        String body = "Your password has been reset by your administrator! Please click on the link below to login to our system:\r\n";
        body += link;
        body = body +
        		"\n **The following information is deemed sensitive**:\r\n" + 
        		"\r\n" + 
        		"Your username is: \r\n" + user.getUsername() +
        		"\r\n" + 
        		"Your first time password is:\r\n" + password +
        		"\r\n" + 
        		"Please login to the BOLO System and follow the instructions to finish setting up your account";

        sendSimpleMessage(to, subject, body);

    }

    public void boloCreationNotification(String to,Bolo bolo){
        String subject = "BOLO Alert:"+bolo.getCategory().getName();
        String content = "A new bolo has been posted. Login to view other BOLOs.";
        ByteArrayOutputStream outputStream = null;
        try {
            MimeMessage message = emailSender.createMimeMessage();
      
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
             
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);

            outputStream = new ByteArrayOutputStream();
            PdfGenaratorUtil.convertHtmlToPdf(boloPdfGenerationService.GetBoloAsPDF(bolo,false)
            , outputStream);
            byte[] bytes = outputStream.toByteArray();

            DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
            
             
            helper.addAttachment(bolo.getConfirmationToken()+".pdf", dataSource);

            emailSender.send(message);

        }catch(Exception ex) {
            ex.printStackTrace();
        } finally {
            //clean off
            if(null != outputStream) {
                try { outputStream.close(); outputStream = null; }
                catch(Exception ex) { }
            }
        }

    }

    

    
}