package org.springblade.common.utils;

import cn.hutool.core.util.StrUtil;

/**
 * 62进制编码工具类
 * 
 * @author William
 * @date 2022年11月10日 下午12:10:57
 */
public class Number62 {
//	public static String ALPHABET = "vPh7zZwA2LyU4bGq5tcVfIMxJi6XaSoK9CNp0OWljYTHQ8REnmu31BrdgeDkFs";
	public static String ALPHABET = "vph7zzwa2lyu4bgq5tcv2imxji3xa2ok3cnp0owljythq80enmu31b1dgedkf2";

	public static String encode(long num) {
		if (num < 1){
			throw new IllegalArgumentException("num must be greater than 0.");
		}
		StringBuilder sb = new StringBuilder();
		for (; num > 0; num /= 62) {
			sb.append(ALPHABET.charAt((int) (num % 62)));
		}

		return StrUtil.reverse(sb.toString());
	}

	public static long decode(String str) {
		if (str == null || str.trim().length() == 0) {
			throw new IllegalArgumentException("str must not be empty.");
		}
		long result = 0;
		for (int i = 0; i < str.length(); i++) {
			result += ALPHABET.indexOf(str.charAt(i)) * Math.pow(62, i);
		}
		return result;
	}
	
	/**
	 * 
	 * @param args
	 */

	public static void main(String[] args) {
		System.out.println(encode(System.currentTimeMillis()));
	}
}
