package com.tiesheng.login.config.token;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.jwt.JWT;
import com.tiesheng.login.config.token.bean.TokenBean;
import com.tiesheng.util.ServletKit;
import com.tiesheng.util.exception.ApiException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;


/**
 * @author hao
 */
@Configuration
@ConfigurationProperties("tiesheng.token")
public class TsTokenConfig {

    private Map<String, TokenBean> ignores = MapUtil.newHashMap();
    private String encryptKey = "%kIp9frQCu";
    private Integer expireHours = 48;


    /**
     * 获取当前登录的token
     *
     * @return
     */
    public static TokenBean get() {
        TsTokenConfig tokenConfig = SpringUtil.getBean(TsTokenConfig.class);
        return tokenConfig.validToken(true);
    }


    /**
     * 获取当前登录的token
     *
     * @return
     */
    public static TokenBean getWithoutThr() {
        TsTokenConfig tokenConfig = SpringUtil.getBean(TsTokenConfig.class);
        return tokenConfig.validToken(false);
    }


    /**
     * 验证token
     *
     * @param token
     * @return
     */
    public TokenBean isIgnored(String token) {
        if (ignores == null) {
            return null;
        }
        return ignores.get(token);
    }


    /**
     * 验证token
     *
     * @return
     */
    public TokenBean validToken(boolean thrExp) {
        String token = ServletUtil.getHeader(ServletKit.getRequest(), "token", "utf-8");
        TokenBean tokenBean = isIgnored(token);
        if (tokenBean != null) {
            return tokenBean;
        }

        try {
            JWT decode = JWT.of(token);
            String id = decode.getPayload("id").toString();
            String environmentType = decode.getPayload("environmentType").toString();
            String service = decode.getPayload("service").toString();
            String extra = decode.getPayload("extra").toString();
            Long expireTime = decode.getPayloads().getLong("time");
            if (expireTime != null && expireTime < System.currentTimeMillis()) {
                throw new ApiException("登录过期，请重新登陆");
            }

            tokenBean = new TokenBean(id, environmentType, service);
            tokenBean.setExtra(extra);
        } catch (Exception ignored) {
        }

        if (tokenBean == null && thrExp) {
            throw new ApiException(StrUtil.isEmpty(token) ? "请先登录" : "登录过期，请重新登陆");
        }

        if (tokenBean == null) {
            tokenBean = new TokenBean();
            tokenBean.setId("");
        }

        return tokenBean;
    }

    ///////////////////////////////////////////////////////////////////////////
    // setter\getter
    ///////////////////////////////////////////////////////////////////////////

    public Map<String, TokenBean> getIgnores() {
        return ignores;
    }

    public void setIgnores(Map<String, TokenBean> ignores) {
        this.ignores = ignores;
    }

    public String getEncryptKey() {
        return encryptKey;
    }

    public void setEncryptKey(String encryptKey) {
        this.encryptKey = encryptKey;
    }

    public Integer getExpireHours() {
        return expireHours;
    }

    public void setExpireHours(Integer expireHours) {
        this.expireHours = expireHours;
    }
}
