package it.enea.cruise.service.idp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import it.enea.cruise.model.user.Community;
import it.enea.cruise.model.user.User;
import it.enea.cruise.model.user.UserCommunity;



public class JWTManager {

	List<String> userGroups;

	public static User jwt2User(String token) {

		User user = new User();
		List<String> userGroups;
		List<UserCommunity> userCommunities = new ArrayList<UserCommunity>();
		List<Community> communities = new ArrayList<Community>();
		Community tempCommunity = new Community();
		try {
			DecodedJWT jwt = JWT.decode(token);

			Map<String, Claim> claims = jwt.getClaims();
		
			String localResource = jwt.getClaim("azp").asString();
			Claim resource_access = claims.get("resource_access");
			if (resource_access != null) {
				Map<String, Object> as = resource_access.asMap();
				Map<String, Object> resource = (Map<String, Object>) as.get(localResource);
				if (resource == null) { //non e' definito il ruolo dell'utente nella localReource
					//quindi imposto il ruolo ad unknwown. Poi verra' aggiornato d qualcun altro. 
					List<String> tempRoles = new ArrayList<String>();
					tempRoles.add("unknown");
					user.setKeycloakRoles(tempRoles);
					
				}else {
				
				user.setKeycloakRoles((List<String>) resource.get("roles"));
			} }
			
			Claim realm_access = claims.get("realm_access");
			if (realm_access != null) {
				Map<String, Object> as = realm_access.asMap();
				userGroups = (List<String>) as.get("roles");
			} else {
				Claim userGroupsMap = claims.get("roles");
				userGroups = (List<String>) userGroupsMap.asList(token.getClass());
				System.out.println(userGroups);
			}
			
			Claim communityMap = claims.get("communities");
			
			if(communityMap == null) {
				Long communityId = jwt.getClaim("communityId").asLong();
				user.setTmpCommunity(new Long(communityId));
			}else {
				communities = (List<Community>) communityMap.asList(Community.class);
			}
			
			//System.out.println(communities);
			
			Claim walletsMap = claims.get("wallets");
			if(walletsMap == null) {
				String walletId = jwt.getClaim("walletId").asString();
				user.setTmpWallet(walletId);
						
			}else {
	
			}
			String name = jwt.getClaim("name").asString();
			String email = jwt.getClaim("email").asString();
			String preferred_username = jwt.getClaim("preferred_username").asString();
			String sub = jwt.getClaim("sub").asString();
			String given_name = jwt.getClaim("given_name").asString();
			String family_name = jwt.getClaim("family_name").asString();
			
			//user.setUserfirstname(given_name);
			//user.setLastName(family_name);
			//user.setEmail(email);
			//user.setUsername(preferred_username);
			user.setSubject(sub);
			//user.setUserCommunities(userCommunities);

					
			
		} catch (JWTDecodeException e) {
			System.out.println(e.getLocalizedMessage());
		}
		return user;
	}
	
	public static String getSubject(String token) {
		DecodedJWT jwt = JWT.decode(token);
		return jwt.getClaim("sub").asString();
		
	}
}
