package com.cm.token;

import com.cm.AesUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Administrator
 * @version 1.0
 * @className TokenEncoder
 * @description token
 * @date 2018/12/7 10:36
 **/
public class TokenEncoder implements IToken {

    private static final String ENCODE_RULE = "CMXX";
    private static final String SPLIT_CONTENT = "_C450292408M_";
    private String key;
    private String value;
    private String content;
    private int tokenType;

    private TokenEncoder(int tokenType) {
        this.tokenType = tokenType;
    }

    private TokenEncoder(String content, int tokenType) {
        this.content = content;
        this.tokenType = tokenType;
    }

    private TokenEncoder(String key, String value, int tokenType) {
        this.key = key;
        this.value = value;
        this.tokenType = tokenType;
    }

    /**
     * 基础token
     *
     * @return
     */
    public static TokenEncoder createToken() {
        return new TokenEncoder(TOKEN_TYPE_DEFAULT);
    }

    /**
     * 内容token
     *
     * @param content
     * @return
     */
    public static TokenEncoder createToken(String content) throws Exception {
        if (StringUtils.isEmpty(content)) {
            throw new Exception("content can't contain null value");
        }
        return new TokenEncoder(content, TOKEN_TYPE_CONTENT);
    }

    /**
     * 创建键值对类token
     *
     * @param key
     * @param value
     * @return
     */
    public static TokenEncoder createToken(String key, String value) throws Exception {
        if (StringUtils.isEmpty(key)) {
            throw new Exception("key can't contain null value");
        }
        if (StringUtils.isEmpty(value)) {
            throw new Exception("value can't contain null value");
        }
        return new TokenEncoder(key, value, TOKEN_TYPE_KEY_AND_VALUE);
    }

    /**
     * 获取用户名密码token
     *
     * @return
     */
    public String getToken() {
        String token = null;
        try {
            StringBuilder tokenInfo;
            switch (this.tokenType) {
                // 键值对类token
                case TOKEN_TYPE_KEY_AND_VALUE:
                    tokenInfo = new StringBuilder(TOKEN_PREFIX_KEY_AND_VALUE).append(SPLIT_CONTENT)
                            .append(this.key).append(SPLIT_CONTENT)
                            .append(this.value).append(SPLIT_CONTENT)
                            .append(System.currentTimeMillis()).append(SPLIT_CONTENT).append(TOKEN_SUFFIX);
                    break;
                case TOKEN_TYPE_CONTENT:
                    tokenInfo = new StringBuilder(TOKEN_PREFIX_CONTENT).append(SPLIT_CONTENT)
                            .append(this.content).append(SPLIT_CONTENT)
                            .append(System.currentTimeMillis()).append(SPLIT_CONTENT).append(TOKEN_SUFFIX);
                    break;
                default:
                    tokenInfo = new StringBuilder(TOKEN_PREFIX_DEFAULT).append(SPLIT_CONTENT)
                            .append(System.currentTimeMillis()).append(SPLIT_CONTENT).append(TOKEN_SUFFIX);
                    break;
            }
            token = AesUtil.aesEncoder(ENCODE_RULE, tokenInfo.toString());
            token = Base64.encodeBase64String(token.getBytes(CHARSET_NAME_UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

}
