package zall.page;

import zall.Zallery;
import zall.ZalleryServlet;
import zall.bean.Folder;
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.List;
import java.util.logging.Logger;


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

    public static final String URI = "/gallery";
    public static final String JSP_FILE = "page_gallery.jsp";


    public void doGet(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws SQLException, IOException, ServletException {
        User user = AuthenticationManager.getUserSession(request.getSession());
        Folder folder;

        if (request.getParameter("folder") != null)
            folder = Folder.load(db, Long.parseLong(request.getParameter("folder")));
        else {
            folder = Folder.loadRoot(db, user);
            // Setup new root folder
            if (folder == null) {
                folder = Folder.genRoot();
                folder.save(db);
            }
        }

        List<Media> list = Media.load(db, folder);
        List<Folder> subFolders = Folder.loadSubFolders(db, folder, user);

        //session.setAttribute("user", user);
        request.setAttribute("folder", folder);
        request.setAttribute("subfolders", subFolders);
        request.setAttribute("media", list);

        include(JSP_FILE, request, response);
    }

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

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

        Folder folder = Folder.load(db, id);

        switch (request.getParameter("action")) {

            // -----------------------------------------
            // Toggle private mode
            // -----------------------------------------

            case "private":
                if (folder != null) {
                    if (AuthenticationManager.canEdit(user, folder)) {
                        folder.setPrivate(!folder.isPrivate());
                        folder.save(db);
                        if (folder.isPrivate())
                            msgs.add(new UserMessage(MessageLevel.SUCCESS, lang.getString("folder.modify.is.private")));
                        else
                            msgs.add(new UserMessage(MessageLevel.SUCCESS, lang.getString("folder.modify.is.public")));
                    } else
                        msgs.add(new UserMessage(MessageLevel.ERROR, lang.getString("folder.modify.not.allowed")));
                } else
                    msgs.add(new UserMessage(MessageLevel.ERROR, lang.getString("folder.not.found")));
                break;
        }

        doGet(request, response, db);
    }
}
