/**
 * 
 */
package cz.hsrs.servlet.provider;

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

import javax.naming.AuthenticationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cz.hsrs.db.DBJsonUtils;
import cz.hsrs.db.model.AlertEvent;
import cz.hsrs.db.util.UtilFactory;
import cz.hsrs.servlet.feeder.ServiceParameters;

/**
 * Servlet handling request for alerts
 * @author mkepka
 */
public class AlertService extends DBServlet{
    private static final long serialVersionUID = 1L;
    
    public static final String GET_ALERTS = "GetAlerts";
    public static final String GET_ALERT_EVENTS_BY_TIME = "GetAlertEventsByTime";
    
    private UtilFactory db;
    
    public void init() throws ServletException {
        super.init();
        try {
            db = new UtilFactory();
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request, response);

        RequestParameters params = new RequestParameters(request);
        
        /* session authentication method */
        try {
            getAuthenticatedLoginUser(request);
        } catch (AuthenticationException e1) {
            throw new ServletException("Authentication failure for request "+ request.getQueryString());
        }
        
        response.addHeader("Access-Control-Allow-Origin", "*");
        PrintWriter out = response.getWriter();
        try {
        	/* GetAlerts request */
            if (request.getParameter(ServiceParameters.OPERATION).equals(GET_ALERTS)) {
                DBJsonUtils.writeJSON(out, db.alertUtil.getAlerts(params.getUnitId()));
            }
            /* GetAlertEventsByTime request */
            else if (request.getParameter(ServiceParameters.OPERATION).equals(GET_ALERT_EVENTS_BY_TIME)) {
                List<AlertEvent> events = db.alertUtil.getAlertEventsByTime(params.getUnitId(), params.from, params.to);
                DBJsonUtils.writeJSON(out, events);
            } else {
                throw new ServletException("Wrong request "+request.getQueryString());
            }
        } catch (SQLException e) {
            solveGetException(e, out);
        }
    }

    static class RequestParameters {
        private final long unitId;
        private final String from;
        private final String to;

        RequestParameters(HttpServletRequest request) throws NullPointerException {
            Object uid = request.getParameter("unit_id");
            this.unitId = uid != null ? Long.parseLong(uid.toString()) : null;
            this.from = request.getParameter("from");
            this.to = request.getParameter("to");
        }

        public long getUnitId() {
            return unitId;
        }
    }
}