package zall.action.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import zall.action.ZalleryAction;
import zall.bean.User;
import zall.manager.EmailManager;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import zutil.ui.UserMessageManager;
import zutil.ui.UserMessageManager.MessageLevel;
import zutil.ui.UserMessageManager.UserMessage;

public class SendVerificationEmailAction extends ZalleryAction {
    private static final Logger logger = LogUtil.getLogger();

    public SendVerificationEmailAction() {
        super("sendverfemail", true);
    }

    @Override
    public void handleRequest(DBConnection db, HttpServletRequest request, HttpServletResponse response, HttpSession session,
                              PrintWriter out, User user, UserMessageManager msgs) throws SQLException, IOException {

        User target_user = null;
        if (request.getParameter("id") != null)
            target_user = User.load(db, Long.parseLong(request.getParameter("id")));
        else {
            msgs.add(new UserMessage(MessageLevel.ERROR, "Missing parameters!"));
            return;
        }

        if (target_user != null) {
            if (user.isSuperUser()) {
                EmailManager.sendVerificationEmail(target_user);

                logger.info("Verification email sent successfully to: " + user.getEmail());
                msgs.add(new UserMessage(MessageLevel.INFO, "Verification email sent successfully."));
            } else
                msgs.add(new UserMessage(MessageLevel.ERROR, "You are not authorize send verification emails!"));
        } else {
            msgs.add(new UserMessage(MessageLevel.ERROR, "No such user!"));
        }
    }

}
