package cz.hsrs.servlet.provider;

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

import javax.naming.AuthenticationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.HttpHeaders;

import cz.hsrs.db.DBJsonUtils;
import cz.hsrs.db.util.UtilFactory;
import cz.hsrs.servlet.feeder.ServiceParameters;
import cz.hsrs.servlet.security.LoginUser;

/**
 * Servlet implementation class GroupService
 * @author jezekjan
 */
public class GroupService extends DBServlet{
    private static final long serialVersionUID = 1L;
    
    public static final String GET_SUPER_GROUPS = "GetSuperGroups";    
    public static final String GET_SUB_GROUPS = "GetSubGroups";    
    public static final String GET_GROUPS = "GetGroups";

    private UtilFactory db;
       
    /** @see HttpServlet#HttpServlet() */
    public GroupService() { super(); }

    public void init() throws ServletException {
        super.init();
        
        try {
            db = new UtilFactory();
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
    /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request, response);
        RequestParameters params = new RequestParameters(request);

        /* session authentication method */
        LoginUser loggedUser;
        try {
            loggedUser = getAuthenticatedLoginUser(request);
            String userName = loggedUser.getUserName();
            params.setUser(userName);
        } catch (AuthenticationException e1) {
            throw new ServletException("Authentication failure for request "+ request.getQueryString());
        }

        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");

        PrintWriter out = response.getWriter();
        try {
            switch (request.getParameter(ServiceParameters.OPERATION)) {
                case GET_GROUPS:
                    DBJsonUtils.writeJSON(out, db.groupUtil.getGroups(params.getUser()));
                    break;
                case GET_SUPER_GROUPS:
                    DBJsonUtils.writeJSON(out, db.groupUtil.getSuperGroups(params.getUser()));
                    break;
                case GET_SUB_GROUPS:
                    DBJsonUtils.writeJSON(out, db.groupUtil.getSubGroups(params.getGroupId()));
                    break;
                default:
                    throw new NullPointerException("No operation specified.");
            }
        } catch (SQLException e) {
            solveGetException(e, out);
        }
    }

    static class RequestParameters {
        private String user;
        private int groupId;

        RequestParameters(HttpServletRequest request) throws NullPointerException{
            Object id = request.getParameter("parent_group");
            this.groupId = id != null ? Integer.parseInt(id.toString()) : -1;
        }

        public String getUser() {
            return user;
        }

        public int getGroupId() {
            return groupId;
        }

        public void setGroupId(int groupId) {
            this.groupId = groupId;
        }

        public void setUser(String user) {
            this.user = user;
        }
    }
}