package com.nmgs.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class TokenUtil {
    /**
     * token秘钥
     */
    //private static final String TOKEN_SECRET = "apiManager";
    private static final String TOKEN_SECRET = "5267915"; //0.13上用

    /**
     * 设置过期时间
     */
    private static final long EXPIRE_DATE = 30 * 60 * 100000;

    public static String token(String secreteKey) {

        String token = "";
        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRE_DATE);
            //秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String, Object> header = new HashMap<>();
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            //携带username，password信息，生成签名
            token = JWT.create()
                    .withHeader(header)
                    .withClaim("jobNumber", secreteKey)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (Exception e) {
            log.error("获取token异常", e);
            return null;
        }
        return token;
    }

    public static boolean verify(String token, HttpServletRequest request) {
        /**
         * @desc 验证token，通过返回true
         * @params [token]需要校验的串
         **/
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            HttpSession session = request.getSession();
//            String Man = jwt.getClaim("Man").asString().replaceAll("\\\"","");;
//            String Manid = jwt.getClaim("Manid").toString().replaceAll("\\\"","");;
//            String Manno = jwt.getClaim("Manno").toString().replaceAll("\\\"","");;
            String jobNumber = jwt.getClaim("jobNumber").toString().replaceAll("\\\"","");;
            if (jobNumber == null || jobNumber.equals("")) {
                return false;
            }
            session.setAttribute("jobNumber", jobNumber);
            return true;
        } catch (Exception e) {
            log.error("验证token异常", e);
            return false;
        }
    }

    public static boolean verify(String token) {
        /**
         * @desc 验证token，通过返回true
         * @params [token]需要校验的串
         **/
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception e) {
            log.error("验证token异常", e);
            return false;
        }
    }
}