package com.ningdatech.pmapi.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.ningdatech.pmapi.common.model.ZwddTokenUser;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @author liuxinxin
 * @date 2023/5/4 下午2:34
 */

public class JwtTokenUtil {

    private static final String SECRET_KEY = "dbe1cc89019c4c7abd5085bf47a25fb0";

    private static final String CLAIM_KEY = "content";

    private static final Integer DEFAULT_EXPIRE_TIME = Integer.valueOf(86400);

    public static String createToken(ZwddTokenUser dingTokenUser) {
        return createToken(dingTokenUser, DEFAULT_EXPIRE_TIME);
    }

    public static String createToken(ZwddTokenUser dingTokenUser, Integer expireTime) {
        if (dingTokenUser == null) {
            throw new IllegalArgumentException("");
        }
        if (dingTokenUser.getTenantId() == null) {
            throw new IllegalArgumentException("");
        }
        if (StringUtils.isEmpty(dingTokenUser.getEmployeeCode())) {
            throw new IllegalArgumentException("");
        }
        if (dingTokenUser.getTenantId() == null) {
            throw new IllegalArgumentException("");
        }
        return createToken(JSON.toJSONString(dingTokenUser), expireTime);
    }

    private static String createToken(String content, Integer expireTime) {
        if (StringUtils.isEmpty(content)) {
            throw new IllegalArgumentException("");
        }
        Map<String, Object> header = new HashMap<>(4);
        header.put("alg", "HS256");
        header.put("typ", "JWT");
        Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
        return JWT.create()
                .withHeader(header)
                .withClaim("content", content)
                .withIssuedAt(new Date())
                .withExpiresAt(new Date(System.currentTimeMillis() + (expireTime.intValue() * 1000)))
                .sign(algorithm);
    }

    public static boolean verifyToken(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new IllegalArgumentException("token");
        }
        Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
        JWTVerifier verifier = JWT.require(algorithm).build();
        verifier.verify(token);
        return true;
    }

    private static String parseJwtString(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new IllegalArgumentException("token");
        }
        Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
        JWTVerifier verifier = JWT.require(algorithm).build();
        return verifier.verify(token).getClaim("content").asString();
    }

    public static ZwddTokenUser parseJwt(String token) {
        String dingUserJson = parseJwtString(token);
        if (StringUtils.isEmpty(dingUserJson)) {
            return null;
        }
        ZwddTokenUser dingTokenUser = (ZwddTokenUser) JSONObject.parseObject(dingUserJson, ZwddTokenUser.class);
        return dingTokenUser;
    }
}
