package zall.page;

import zall.Zallery;
import zall.ZalleryServlet;
import zall.bean.Comment;
import zall.bean.Media;
import zall.bean.User;
import zall.manager.AuthenticationManager;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import zutil.ui.UserMessageManager;
import zutil.ui.UserMessageManager.MessageLevel;
import zutil.ui.UserMessageManager.UserMessage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

@WebServlet(MediaServlet.URI)
public class MediaServlet extends ZalleryServlet {
    private static Logger logger = LogUtil.getLogger();

    public static final String URI = "/media";
    private static final String JSP_FILE = "page_media.jsp";


    public void doGet(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws ServletException, SQLException, IOException {
        UserMessageManager msgs = Zallery.getUserMessage(request.getSession());

        if (request.getParameter("id") == null && request.getParameter("type") == null) {
            msgs.add(new UserMessage(MessageLevel.ERROR, lang.getString("parameter.not.found")));
            return;
        }
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            Media media = Media.load(db, request.getParameter("type"), id);

            request.setAttribute("media", media);
        } catch (NumberFormatException e) {
            logger.log(Level.FINE, "", e);
        }

        include(JSP_FILE, request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws SQLException, ServletException, IOException {
        UserMessageManager msgs = Zallery.getUserMessage(request.getSession());
        User user = AuthenticationManager.getUserSession(request.getSession());
        long id = Long.parseLong(request.getParameter("id"));

        if (request.getParameter("id") == null || request.getParameter("type") == null) {
            msgs.add(new UserMessage(MessageLevel.ERROR, lang.getString("parameter.not.found")));
            return;
        }

        Media media = Media.load(db, request.getParameter("type"), id);

        switch (request.getParameter("action")) {
            // -----------------------------------------
            // Comment
            // -----------------------------------------

            case "comment":
                Comment cm = new Comment();
                cm.setUser(user);
                cm.setMessage(request.getParameter("msg"));
                media.addComment(cm);
                media.save(db);

                msgs.add(new UserMessage(MessageLevel.INFO, lang.getString("comment.created")));
                break;

            // -----------------------------------------
            // Modify media
            // -----------------------------------------

            case "modify":
                if (media != null) {
                    if (AuthenticationManager.canEdit(user, media)) {
                        media.setTitle(request.getParameter("title"));
                        media.setDescription(request.getParameter("description"));
                        media.save(db);

                        msgs.add(new UserMessage(MessageLevel.SUCCESS, lang.getString("modify.successfully")));
                    } else
                        msgs.add(new UserMessage(MessageLevel.ERROR, lang.getString("media.modify.not.allowed")));
                } else
                    msgs.add(new UserMessage(MessageLevel.ERROR, lang.getString("media.not.found")));

                break;
        }

        doGet(request, response, db);
    }

}