package zall.action.user;

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

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

import zall.action.ZalleryAction;
import zall.bean.User;
import zutil.db.DBConnection;
import zutil.ui.UserMessageManager;
import zutil.ui.UserMessageManager.MessageLevel;
import zutil.ui.UserMessageManager.UserMessage;

public class RemoveUserAction extends ZalleryAction {

    public RemoveUserAction() {
        super("rmuser", true);
    }

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

        // Remove user
        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()) {
                target_user.delete(db);

                msgs.add(new UserMessage(MessageLevel.INFO, "User removed successfully."));
            }
            msgs.add(new UserMessage(MessageLevel.ERROR, "You are not authorize to delete that user!"));
        } else {
            msgs.add(new UserMessage(MessageLevel.ERROR, "No such user!"));
        }
    }

}
