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

import java.sql.SQLException;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
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.UserUtil;
import cz.hsrs.rest.beans.UserBean;
import cz.hsrs.rest.util.AuthUtil;
import cz.hsrs.rest.util.UserRestUtil;
import cz.hsrs.servlet.security.LoginUser;
import net.sf.json.JSONObject;

/**
 * @author mkepka
 *
 */
@Path("/user")
public class UserRest {
	
	/**
	 * Empty constructor
	 */
	public UserRest() {
	}
	
	/**
	 * 
	 * @param req
	 * @return
	 */
	@GET
	public Response getUser(@Context HttpServletRequest req) {
        try {
        	LoginUser loggedUser = AuthUtil.getAuthenticatedLoginUser(req);
        	UserBean userDetails = UserRestUtil.getUser(loggedUser.getUserName());
        	return Response.ok().entity(userDetails)
    				.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
    				.build();
        } catch (AuthenticationException e1) {
        	return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
        		.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
        		.entity("Authentication failure for request "+ req.getQueryString())
        		.build();
        } catch (SQLException e) {
        	return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
            		.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
            		.entity(e.getLocalizedMessage())
            		.build();
		}
	}
	
	/**
	 * 
	 * @param userJSON
	 * @param req
	 * @return
	 */
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	public Response insertUser(JSONObject userJSON, @Context HttpServletRequest req) {
		try {
        	LoginUser loggedUser = AuthUtil.getAuthenticatedLoginUser(req);
        	if(loggedUser.getRightsID() == 0) {
        		UserUtil.insertUser(userJSON.getString("userName"),
        				userJSON.getString("userPass"),
        				userJSON.getString("userRealName"),
        				userJSON.getInt("groupId"),
        				userJSON.getInt("rightsId"));
        	return Response.ok()
    				.build();
        	}
        	else {
        		return Response.status(HttpStatus.ORDINAL_403_Forbidden)
                		.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
                		.entity("Not enough rights for inserting!")
                		.build();
        	}
        } catch (AuthenticationException e1) {
        	return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
        		.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
        		.entity("Authentication failure for request!")
        		.build();
        } catch (SQLException e) {
        	return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
            		.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
            		.entity(e.getLocalizedMessage())
            		.build();
		}
    }
}