package us.kbase.auth2.service.api;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import us.kbase.auth2.lib.Authentication;
import us.kbase.auth2.lib.exceptions.AuthError;
import us.kbase.auth2.lib.exceptions.AuthException;
import us.kbase.auth2.lib.exceptions.AuthenticationException;
import us.kbase.auth2.lib.storage.exceptions.AuthStorageException;
import us.kbase.auth2.lib.token.HashedToken;
import us.kbase.auth2.lib.token.IncomingToken;

@Path("/api/legacy/globus")
public class LegacyGlobus {

	//TODO TEST
	//TODO JAVADOC
	
	@Inject
	private Authentication auth;
	
	// note that access_token_hash is not returned in the structure
	// also note that unlike the globus api, this does not refresh the token
	@GET
	@Path("/token")
	@Produces(MediaType.APPLICATION_JSON)
	public Map<String, Object> introspectToken(
			@HeaderParam("x-globus-goauthtoken") final String token,
			@QueryParam("grant_type") final String grantType)
			throws AuthException, AuthStorageException {

		if (!"client_credentials".equals(grantType)) {
			throw new AuthException(AuthError.UNSUPPORTED_OP,
					"Only client_credentials grant_type supported. Got " +
					grantType);
		}
		if (token == null || token.isEmpty()) {
			throw new AuthenticationException(AuthError.NO_TOKEN, "");
		}
		final HashedToken ht = auth.getToken(new IncomingToken(token));
		final Map<String, Object> ret = new HashMap<>();
		ret.put("access_token", token);
		ret.put("client_id", ht.getUserName().getName());
		ret.put("expires_in", ht.getExpirationDate().getTime() -
				new Date().getTime());
		ret.put("expiry", ht.getExpirationDate().getTime());
		ret.put("issued_on", ht.getCreationDate().getTime());
		ret.put("lifetime", ht.getExpirationDate().getTime() -
				ht.getCreationDate().getTime());
		ret.put("refresh_token", "");
		ret.put("scopes", new LinkedList<String>());
		ret.put("token_id", ht.getId().toString());
		ret.put("token_type", "Bearer");
		ret.put("user_name", ht.getUserName().getName());
		return ret;
	}
}

@@ -1594,16 +1594,194 @@
 oken));%0A
+%09%09final long created = (long) Math.floor(%0A%09%09%09%09ht.getCreationDate().getTime() / 1000.0);%0A%09%09final long expires = (long) Math.floor(%0A%09%09%09%09ht.getExpirationDate().getTime() / 1000.0);%0A
 %09%09final 
@@ -1937,47 +1937,18 @@
 n%22, 
-ht.getExpirationDate().getTime() -%0A%09%09%09%09
+expires - 
 new 
@@ -1990,190 +1990,90 @@
 y%22, 
-ht.getExpirationDate().getTime());%0A%09%09ret.put(%22issued_on%22, ht.getCreationDate().getTime());%0A%09%09ret.put(%22lifetime%22, ht.getExpirationDate().getTime() -%0A%09%09%09%09ht.getCreationDate().getTime()
+expires);%0A%09%09ret.put(%22issued_on%22, created);%0A%09%09ret.put(%22lifetime%22, expires - created
 );%0A%09

Fix globus endpoint times