package com.huiju.estateagents.common;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.servlet.http.HttpServletRequest;
import java.security.Key;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * JWTUtils
 * jwt 辅助类
 * https://github.com/jwtk/jjwt
 */
public class JWTUtils {
    // 直接定义常量是不安全的
    public final static Key key = Keys.hmacShaKeyFor("yansenisahero,2019-5-17 16:18:14".getBytes());
    
    /**
     * getSubject 获取 jwt subject
     * @param request
     * @return
     */
    public static String getSubject(HttpServletRequest request) {
        String jwtHeader = request.getHeader("authorization");
        if (null == jwtHeader || "".equals(jwtHeader.trim())) return null;

        String[] jwtAuths = jwtHeader.split("Bearer ");
        if (null == jwtAuths || jwtAuths.length < 2) return null;

        try {
            return Jwts.parser().setSigningKey(key).parseClaimsJws(jwtAuths[1]).getBody().getSubject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    /**
     * getSubject 获取 jwt subject
     * @param request
     * @return
     */
    public static Map getUserIdAndOrgId(HttpServletRequest request) {
        String jwtHeader = request.getHeader("authorization");
        if (null == jwtHeader || "".equals(jwtHeader.trim())) return null;
        
        String[] jwtAuths = jwtHeader.split("Bearer ");
        if (null == jwtAuths || jwtAuths.length < 2) return null;
        
        try {
            return Jwts.parser().setSigningKey(key).parseClaimsJws(jwtAuths[1]).getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    /**
     * getSubject 获取 jwt subject
     * @param request
     * @return
     */
    public static String getToken(HttpServletRequest request) {
        String jwtHeader = request.getHeader("authorization");
        if (null == jwtHeader || "".equals(jwtHeader.trim())) {
            return null;
        }
        
        String[] jwtAuths = jwtHeader.split("Bearer ");
        if (null == jwtAuths || jwtAuths.length < 2) {
            return null;
        }
        
        
        return jwtAuths[1];
    }

    /**
     * newToken 生成新的 token
     * @param subject
     * @return
     */
    public static String newToken(String subject) {
        // 有效期 7 天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+7);
        return Jwts.builder().setSubject(subject).setExpiration(calendar.getTime()).signWith(key).compact();
    }
    
    /**
     * newToken 生成新的 token
     * @param subject
     * @return
     */
    public static String newTokenByMap(Map subject) {
        // 有效期 7 天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+7);

        return Jwts.builder().setClaims(subject).setExpiration(calendar.getTime()).signWith(key).compact();
    }
}
