package com.ninjas.tnpresidential.resources.core;

import com.ninjas.tnpresidential.MainConfiguration;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Locale;

/**
 * @author ayassinov on 03/10/2014.
 */
public class BaseResource {

    /**
     * Jersey creates a fresh resource every request so this is safe
     */
    @Context
    protected UriInfo uriInfo;
    /**
     * Jersey creates a fresh resource every request so this is safe
     */
    @Context
    protected HttpHeaders httpHeaders;

    @Autowired
    protected MainConfiguration configuration;

    /**
     * @return The most appropriate locale for the upstream request (never null)
     */
    public Locale getLocale() {
    	// TODO : This should be a configuration setting
        Locale defaultLocale = Locale.UK;
        Locale locale;
        if (httpHeaders == null) {
            locale = defaultLocale;
        } else {
            locale = httpHeaders.getLanguage();
            if (locale == null) {
                locale = defaultLocale;
            }
        }
        return locale;
    }

    /**
     * @return A suitable exception to represent NOT_FOUND
     */
    protected WebApplicationException notFound() {
        return new WebApplicationException(Response.Status.NOT_FOUND);
    }

    /**
     * @return A suitable exception to represent BAD_REQUEST
     */
    protected WebApplicationException badRequest() {
        return new WebApplicationException(Response.Status.BAD_REQUEST);
    }

}
