package com.repair.common.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.repair.common.result.ResponseStatusEnum;
import com.repair.model.pojo.RepairAdmin;
import com.repair.model.result.CommonResult;
import lombok.extern.slf4j.Slf4j;

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

/**
 * jwt 工具
 **/
@Slf4j
public class JWTUtil {

    private static String SIGNATURE = "token!@#$%^7890";

    /**
     * token过期时间，7天
     */
//    private static final long EXPIRED = 1000 * 60 * 60 * 24 * 7;
    private static final long EXPIRED = 1000 * 60 * 60 * 24 * 1;

    /**
     * 生成token 有过期时间
     * @return 返回token
     */
    public static String getToken(RepairAdmin ra,Long expired){

        // 签发时间
        Date iatDate = new Date();

//        // 过期时间
//        Calendar nowTime = Calendar.getInstance();
//        nowTime.add(Calendar.DAY_OF_WEEK, 3);
//        Date expiresDate = nowTime.getTime();

        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HMAC256");
        map.put("typ", "JWT");
        String token = JWT.create()
                .withHeader(map) // header
                .withClaim("account", ra.getAccount()) // 账号
                .withClaim("password", ra.getPassword()) // 密码
                .withExpiresAt(new Date(CommonUtil.getCurrentTimestamp() + (expired == null ? EXPIRED : expired))) // 设置过期时间。过期时间要大于签发时间
                .withIssuedAt(iatDate) // 设置签发时间
                .sign(Algorithm.HMAC256(SIGNATURE)); // 加密
        return token;

//
//        JWTCreator.Builder builder = JWT.create();
//        map.forEach((k,v)->{
//            builder.withClaim(k,v);
//        });
//        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.SECOND,7);
//        builder.withExpiresAt(instance.getTime());
//        return builder.sign(Algorithm.HMAC256(SIGNATURE)).toString();
    }

    public static long getExpired() {
        return EXPIRED;
    }


    /**
     * 验证token
     * @param token
     */
    public static void verify(String token){
        JWT.require(Algorithm.HMAC256(SIGNATURE)).build().verify(token);
    }

    /**
     * 获取token中payload
     * @param token
     * @return
     */
    public static DecodedJWT getToken(String token){
        return JWT.require(Algorithm.HMAC256(SIGNATURE)).build().verify(token);
    }



    /*
     * 解密Token
     * */
    public static CommonResult verifyToken(String token)  {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SIGNATURE)).build();
        DecodedJWT jwt = null;
        try {
            jwt = verifier.verify(token);
        } catch (Exception e) {
            System.out.println("登录凭证校验，message："+e.getMessage()+"，token值："+token);
            return CommonResult.resultValue(ResponseStatusEnum.TOKEN_EXPIRE);
        }

        return CommonResult.ok(jwt.getClaims());
    }
}
