package com.diagbot.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;

import java.util.Date;
import java.util.Map;

/**
 * @Description: Jwt Token解析工具类
 * @author: gaodm
 * @time: 2018/10/29 9:36
 */
public final class SysJwtUtil {

    static final String TOKEN_PREFIX = "Bearer";

    /**
     * 解析token获取用户ID
     *
     * @param token 输入token
     * @return 用户ID
     */
    public static String getUserId(String token) {
        DecodedJWT jwt = decodedJWT(token);
        Map<String, Claim> claims = jwt.getClaims();
        Claim claim = claims.get("user_id");
        return claim.asLong().toString();
    }


    /**
     * 解析token获取医院ID
     *
     * @param token 输入token
     * @return 医院ID
     */
    public static String getHospId(String token) {
        DecodedJWT jwt = decodedJWT(token);
        Map<String, Claim> claims = jwt.getClaims();
        Claim claim = claims.get("hosp_id");
        return claim.asLong().toString();
    }

    /**
     * 解析token获取用户ID
     *
     * @param token 输入token
     * @return 用户ID
     */
    public static Date getExp(String token) {
        DecodedJWT jwt = decodedJWT(token);
        Map<String, Claim> claims = jwt.getClaims();
        Claim claim = claims.get("exp");
        return claim.asDate();
    }

    /**
     * 解析token
     *
     * @param token 输入token
     * @return 解析结果
     */
    public static DecodedJWT decodedJWT(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt;
        } catch (JWTDecodeException e) {
            e.printStackTrace();
            throw new CommonException(CommonErrorCode.ANALYZER_TOKEN_FAIL);
        }
    }

    public static void main(String[] args) {
        String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyLCJ1c2VyX25hbWUiOiJoMSIsInNjb3BlIjpbInNlcnZpY2UiXSwiZXhwIjoxNTg2NzUzMjg4LCJob3NwX2lkIjozLCJhdXRob3JpdGllcyI6WyIvdXNlckF1dGhlbnRpY2F0aW9uL2dldHVzZXJBdXRoZ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WFsO0FMTCIsIi9tb3JTZXJ2aWNlcy9hZGRSZW5ld2Fsc0luZm87QUxMIiwiL3NlcnZpY2VJbmZvL2RlbFNlcnZpY2U7QUxMIiwiL3VzZXJBdXRoZW50aWNhdGlvbi91c2VyQXV0aGVudGljYXRpb247QUxMIiwiL3VzZXJBdXRoZW50aWNhdGlvbi9xdWVyeUF1dGhQcm9ncmVzcztBTEwiLCIvc2VydmljZVRva2VuL2dldFRva2VuQnlQcm9kdWN0U2VydmljZUlkO0FMTCIsIi9zZXJ2aWNlVG9rZW4vZGlzYWJsZVRva2VuO0FMTCIsIi9wcm9kdWN0T3JkZXIvZW5TaG93T3B0aW9uO0FMTCIsIi9tb3JTZXJ2aWNlcy9hZGRPcmRlcjtBTEwiLCIvcHJvZHVjdE9yZGVyL29wZW5VcE9uVHJpYWw7QUxMIl0sImp0aSI6IjNlMjgwNzFhLTIzN2QtNDg2YS05Mzk4LTM0MWQ5MmRjMmViMyIsImNsaWVudF9pZCI6InVhYS1zZXJ2aWNlIn0.SK7TpwLlTuaQ0h5fB50pvHD75lsrf1oRSBAamVkrU5sagpOuWbSJLbSTmPO5Gcv3sz4BRJRgg2sFgmGmS1zkV1_X80Thg8mgSB-GMg_JYbK74SsL8O3HVsCr_bkCKxPFKIMbuSz_sNqazVYojz0ZcdqErrYX2AB1BKVtcKc7XUS42P2GdbzSPMqkPnWFmJRUeVyvlsD2rSS5HEX3nz9tAUK6zeD-7d3P8goW3vkofzX5hck7zoAjqVH5SYSXWaZ9ZHsaNcyQjnFHBkGDbH19apT5boMPLS3C5MXS8hb9a8bAs2xd2PiAmndkUqn7GX3H4-2rYTigzROgg86JPoMkLA";
        //        System.out.println(GsonUtil.toJson(decodedJWT(token)));
        System.out.println(getUserId(token));
        System.out.println(getHospId(token));
        System.out.println(getExp(token));
    }
}
