package ar.edu.itba.paw.services;

import ar.edu.itba.paw.models.Appointment;
import ar.edu.itba.paw.models.Doctor;
import ar.edu.itba.paw.models.Patient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.Properties;

@Service
public class MailServiceImpl implements MailService {

    private static final Logger LOG = LoggerFactory.getLogger(MailServiceImpl.class);

    private static final DateTimeFormatter dateFmt = DateTimeFormatter.ofPattern("dd/MM/yy");

    private static final DateTimeFormatter timeFmt = DateTimeFormatter.ofPattern("HH:mm");

    @Autowired
    private MailSender mailSender;

    @Override
    public void sendAppointmentConfirmationToDoctor(final Appointment appointment, final Doctor doctor,
                                                    final Patient patient) {
        final SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom("no-reply@chopidoturnos.com");
        msg.setSubject("Turno reservado.");
        msg.setTo(doctor.getEmail());
        msg.setText("El paciente " + String.format("%s, %s", patient.getLastName(), patient.getName())
                + " reservó un turno para el día " + appointment.getDate().format(dateFmt)
                + " a las " + appointment.getDate().format(timeFmt) + " hs"
        );

        try {
            mailSender.send(msg);
        } catch (MailException ex) {
            LOG.error("Mail not sent.", ex);
        }
    }

    @Override
    public void sendAppointmentConfirmationToPatient(final Appointment appointment, final Doctor doctor,
                                                     final Patient patient) {
        // TODO
    }

    @Bean
    private static MailSender mailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        mailSender.setProtocol("smtp");
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(587);
        mailSender.setUsername("chopidoturnos@gmail.com");
        mailSender.setPassword("chopidoturnospaw2016");

        final Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        mailSender.setJavaMailProperties(properties);

        return mailSender;
    }
}

@@ -2062,15 +2062,593 @@
     
-// TODO
+final SimpleMailMessage msg = new SimpleMailMessage();%0A        msg.setFrom(%22chopidoturnos@gamil.com%22);%0A        msg.setSubject(%22Turno reservado.%22);%0A        msg.setTo(%22chopidoturnos@gamil.com%22);%0A        msg.setText(%22Usted reserv%C3%B3 un turno para el d%C3%ADa %22 + appointment.getDate().format(dateFmt)%0A                + %22 a las %22 + appointment.getDate().format(timeFmt) + %22 hs%22%0A                + String.format(%22%25s, %25s%22, doctor.getLastName(), doctor.getName())%0A        );%0A%0A        try %7B%0A            mailSender.send(msg);%0A        %7D catch (MailException ex) %7B%0A            // TODO log error%0A        %7D
 %0A   

add patient send mail