package zall;

import zutil.db.DBConnection;
import zutil.log.LogUtil;
import zutil.ui.UserMessageManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import static zall.ZalleryConstants.*;

public abstract class ZalleryServlet extends HttpServlet {
    private static Logger logger = LogUtil.getLogger();
    protected ResourceBundle lang = PropertyResourceBundle.getBundle(LANG_BASENAME, LANG_DEFAULT);


    public final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        DBConnection db = null;
        try {
            UserMessageManager msgs = Zallery.getUserMessage(request.getSession());
            request.setAttribute(ZalleryConstants.SESSION_KEY_USER_MSG, msgs);
            doGet(request, response, db = Zallery.getDB());
            msgs.decrementViewCount();
        } catch (ServletException e) {
            throw e;
        } catch (Exception e) {
            throw new ServletException(e);
        } finally {
            if (db != null) db.close();
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws ServletException, SQLException, IOException {
    }

    public final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        DBConnection db = null;
        try {
            doPost(request, response, db = Zallery.getDB());
        } catch (ServletException e) {
            throw e;
        } catch (Exception e) {
            throw new ServletException(e);
        } finally {
            if (db != null) db.close();
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws ServletException, SQLException, IOException {
        doGet(request, response, db);
    }


    public void include(String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/" + url);
        if (dispatcher != null)
            dispatcher.include(request, response);
    }

    public void forward(String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/" + url);
        if (dispatcher != null)
            dispatcher.forward(request, response);
    }

    public static void redirect(String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (url.startsWith("http"))
            response.sendRedirect(url);
        else {
            if (!url.startsWith("/"))
                url = "/" + url;

            String path = request.getRequestURI();
            if (!path.endsWith("/"))
                path = path.substring(0, path.lastIndexOf("/"));

            response.sendRedirect(path + url);
        }
    }
}
