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

import java.sql.SQLException;
import java.util.logging.Logger;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.mortbay.jetty.HttpStatus;

import cz.hsrs.db.util.ExportUtil;
import cz.hsrs.db.util.UserUtil;
import cz.hsrs.rest.beans.ExceptionBean;
import cz.hsrs.rest.util.AuthUtil;
import cz.hsrs.servlet.security.LoginUser;

/**
 * @author mkepka
 *
 */
@Path("/observation/")
public class ObservationRest {
    
    public static Logger logger = Logger.getLogger("ObservationRest");

    /**
     * Empty constructor
     */
    public ObservationRest() {
        super();
    }
    
    /**
     * 
     * @return
     * /rest/observation/export?group_id=25&sensor_id=340340092&from_time=2021-05-30&to_time=2021-05-31&style=crosstab
     * /rest/observation/export?group_id=25&sensor_id=340340092&from_time=2021-05-30&to_time=2021-05-30+04:00:00&style=crosstab&nullable=false
     */
    @Path("/export")
    @GET
    public Response getObservationsCrossTab(
            @QueryParam("sensor_id") Long sensorId,
            @QueryParam("group_id") Integer groupId,
            @QueryParam(Params.FROM_TIME) String fromTime,
            @QueryParam(Params.TO_TIME) String toTime,
            @QueryParam("style") String exportStyle,
            @DefaultValue("true") @QueryParam("nullable") Boolean nullable,
            @Context HttpServletRequest req){
        try {
            LoginUser loggedUser = AuthUtil.getAuthenticatedLoginUser(req);
            if(exportStyle.equalsIgnoreCase(Params.CROSS_TAB_STYLE)) {
                if(groupId == null) {
                	groupId = UserUtil.getUserGroupId(loggedUser.getUserName());
                }
                String CSV = ExportUtil.getObservationsBySensorByGroupCross(sensorId, groupId, fromTime, toTime, nullable);
                return Response.ok()
                        .entity(CSV)
                        .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
                        .build();
            } else {
                return Response.ok()
                        .entity("Empty")
                        .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
                        .build();
            }
            
        } catch (SQLException e) {
            return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
                    .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
                    .build();
        } catch (AuthenticationException e) {
        	return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
                    .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
                    .build();
		}
    }
    
    private class Params{
        static final String CROSS_TAB_STYLE = "crosstab";
        static final String FROM_TIME = "from_time";
        static final String TO_TIME = "to_time";
    }
}