package com.inspect.base.core.utils;

import com.inspect.base.core.constant.TokenConst;
import com.inspect.base.core.exception.ServiceException;
import com.inspect.base.core.text.Convert;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;

public class JwtUtils {

    public static String createToken(Map<String, Object> claims) {
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, TokenConst.SECRET)
                .compact();
    }

    public static Claims parseToken(String token) {
        return Jwts.parser()
                .setSigningKey(TokenConst.SECRET)
                .parseClaimsJws(token)
                .getBody();
    }

    public static boolean isAdmin(HttpServletRequest request) {
        String token = request.getHeader(TokenConst.AUTHENTICATION);
        if (StringUtils.isEmpty(token))
            return false;

        if (token.startsWith(TokenConst.BEARER)) {
            token = token.replaceFirst(TokenConst.BEARER, StringUtils.EMPTY);
        }

        return isAdmin(token);
    }

    public static boolean isAdmin(String token) {
        Claims body = parseToken(token);
        if (body == null) {
            throw new ServiceException("登录异常");
        }

        Object o = body.get("admin_flag");
        if (null == o) {
            return false;
        } else {
            Integer flag = (Integer) o;
            return flag == 1;
        }
    }

    public static String getUserKey(String token) {
        Claims claims = parseToken(token);
        return getValue(claims, TokenConst.USER_KEY);
    }

    public static String getUserKey(Claims claims) {
        return getValue(claims, TokenConst.USER_KEY);
    }

    public static String getUserId(String token) {
        Claims claims = parseToken(token);
        return getValue(claims, TokenConst.USER_ID);
    }

    public static String getUserId(Claims claims) {
        return getValue(claims, TokenConst.USER_ID);
    }

    public static String getUserName(String token) {
        Claims claims = parseToken(token);
        return getValue(claims, TokenConst.USERNAME);
    }

    public static String getUserName(Claims claims) {
        return getValue(claims, TokenConst.USERNAME);
    }

    public static String getValue(Claims claims, String key) {
        return Convert.toStr(claims.get(key), StringUtils.EMPTY);
    }

    public static void main(String[] args) {
        final String token = "eyJhbGciOiJIUzUxMiJ9.eyJhZG1pbl9mbGFnIjoxLCJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjI1MjNhY2YxLTNmMjQtNGY2Ny1hNDdiLWY2NDE5OThlYjFiMCIsInVzZXJuYW1lIjoiYWRtaW4ifQ.CwnhSTSdrnyvXsw1Xa3KtTxaZwm-pwbae69diTv9mDomPA1ELDXegkUb9JxLOonC636uWAUy_uSoeE6mSQVa2g";
        Claims claims = parseToken(token);
        String userKey = JwtUtils.getUserKey(claims);
        System.out.println("[GAB] userKey=" + userKey);
    }
}
