package de.szut.casino.security.service;

import de.szut.casino.user.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmailService {
    private JavaMailSenderImpl mailSender;

    private MailConfig mailConfig;

    public EmailService(JavaMailSenderImpl mailSender, MailConfig mailConfig) {
        try {
            this.mailSender = mailSender;
            this.mailConfig = mailConfig;
            this.mailSender.setHost(mailConfig.host);
            this.mailSender.setPort(mailConfig.port);
            if (mailConfig.authenticationEnabled) {
                this.mailSender.setUsername(mailConfig.username);
                this.mailSender.setPassword(mailConfig.password);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void sendRegistrationEmail(UserEntity user) {
        String body = "Hallo " + user.getUsername() + ",\n\nA Willkommen bei Trustworthy Casino©! Wir freuen uns, Sie an Bord zu haben.\n\n";
        SimpleMailMessage mail = new SimpleMailMessage();

        mail.setFrom(mailConfig.fromAddress);
        mail.setSubject("Willkommen bei Trustworthy Casino©");
        mail.setTo(user.getEmail());
        mail.setText(body);

        sendEmail(mail);
    }

    private void sendEmail(SimpleMailMessage mail) {
        this.mailSender.send(mail);
    }
}
