package zall.util;

import zall.Zallery;
import zall.bean.User;
import zutil.db.DBConnection;
import zutil.net.smtp.Email;
import zutil.net.smtp.SmtpClient;

import java.io.IOException;
import java.sql.SQLException;

/**
 *
 */
public class ZalleryEmail {
    private static String host;

    public static void setSMTPHost(String host){
        ZalleryEmail.host = host;
    }

    public static void sendVerificationEmail(User user) throws IOException {
        // Email
        Email email = new Email();
        email.setFrom("admin@koc.se", "Koc.se Admin");
        email.setTo(user.getEmail());
        email.setSubject("Registration at "+ Zallery.getWebsiteName());
        email.setContentType(Email.ContentType.HTML);
        email.setMessage("You receive this message because you have requested an account" +
                "<br>at "+Zallery.getWebsiteName()+". Please click the link to verify your email address: " +
                "<p><a href='"+Zallery.getWebsiteURL()+"?action=verfemail&id="+user.getId()+"&hash="+user.getEmailVerificationHash()+"'>"+Zallery.getWebsiteURL()+"?action=verfemail&id="+user.getId()+"&hash="+user.getEmailVerificationHash()+"</a>" +
                "<p> You will have to wait for an admin to activate your account after you have verified your email.");

        SmtpClient smtp = new SmtpClient(host);
        smtp.send(email);
        smtp.close();
    }

    public static void sendActivationEmail(User user) throws IOException {
        Email email = new Email();
        email.setFrom("admin@koc.se", "Koc.se Admin");
        email.setTo(user.getEmail());
        email.setSubject("Account activation at " + Zallery.getWebsiteName());
        email.setContentType(Email.ContentType.HTML);
        email.setMessage("Your account has now been activated by an admin. You can now login and use the site. " +
                "<a href='" + Zallery.getWebsiteURL() + "'>" + Zallery.getWebsiteURL() + "</a>");

        SmtpClient smtp = new SmtpClient(host);
        smtp.send(email);
        smtp.close();
    }

    public static void sendNewUserRegistrationToAdmin(User user, DBConnection db) throws SQLException, IOException{
        // Email the admin about new user
        Email email = new Email();
        email.setFrom("admin@koc.se", "Koc.se Admin");
        email.setSubject("New user activation request at "+Zallery.getWebsiteName());
        email.setContentType(Email.ContentType.HTML);
        email.setMessage("A new user has registered for an account at " +
                "<a href='"+Zallery.getWebsiteURL()+"'>"+Zallery.getWebsiteName()+"</a>:" +
                "<p>Email: <b>" + user.getEmail() + "</b>" +
                "<br>Name: <b>" + user.getName() + "</b>"
        );

        SmtpClient smtp = new SmtpClient(host);
        for(User admin : User.loadSuperUsers(db)){
            if( admin.isEmailVerified() ){
                email.setTo( admin.getEmail() );
                smtp.send(email);
            }
        }
        smtp.close();
    }
}
