/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Ziver Koc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package zall.manager;

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;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import static zall.ZalleryConstants.LANG_BASENAME;

/**
 *
 */
public class EmailManager {
    private static ResourceBundle lang = PropertyResourceBundle.getBundle(LANG_BASENAME, Locale.getDefault());

    public static void sendVerificationEmail(User user) throws IOException {
        String verifyUrl = Zallery.WEBSITE_URL + "?verfemail=" + user.generateEmailVerificationHash() + "&id=" + user.getId();

        Email email = new Email();
        email.setFrom(Zallery.ADMIN_EMAIL, Zallery.ADMIN_EMAIL_NICE);
        email.setTo(user.getEmail());
        email.setSubject(lang.getString("email.registration.title") + Zallery.WEBSITE_NAME);
        email.setContentType(Email.ContentType.HTML);
        email.setMessage(
                lang.getString("email.registration.body1") + " " + Zallery.WEBSITE_NAME + "." +
                "<br>" + lang.getString("email.registration.body2") +
                "<p><a href='" + verifyUrl + "'>" + verifyUrl + "</a>" +
                "<p>" + lang.getString("email.registration.body3"));

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

    public static void sendActivationEmail(User user) throws IOException {
        Email email = new Email();
        email.setFrom(Zallery.ADMIN_EMAIL, Zallery.ADMIN_EMAIL_NICE);
        email.setTo(user.getEmail());
        email.setSubject(lang.getString("email.activation.title") + " " + Zallery.WEBSITE_NAME);
        email.setContentType(Email.ContentType.HTML);
        email.setMessage(
                lang.getString("email.activation.body") +
                "<br><a href='" + Zallery.WEBSITE_URL + "'>" + Zallery.WEBSITE_URL + "</a>");

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

    public static void sendNewUserRegistrationToAdmin(User user, DBConnection db) throws SQLException, IOException {
        Email email = new Email();
        email.setFrom(Zallery.ADMIN_EMAIL, Zallery.ADMIN_EMAIL_NICE);
        email.setSubject(lang.getString("email.activation.request.title") + " " + Zallery.WEBSITE_NAME);
        email.setContentType(Email.ContentType.HTML);
        email.setMessage(
                lang.getString("email.activation.request.body") +
                " <a href='" + Zallery.WEBSITE_URL + "'>" + Zallery.WEBSITE_NAME + "</a>:" +
                "<p>Email: <b>" + user.getEmail() + "</b>" +
                "<br>Name: <b>" + user.getName() + "</b>"
        );

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