package cn.stylefeng.team.project.api.util;

public class Base25Util {

    private static final char[] DIGITS = "34679ACDEFGHJKMNPQRTUVWXY".toCharArray();

    private static final int BASE = 25;

    public static String encode(int value) {

        StringBuilder sb = new StringBuilder();

        while (value > 0) {
            sb.append(DIGITS[(value % BASE)]);
            value /= BASE;
        }

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

    public static int decode(String encodedValue) {

        int result = 0;

        for (int i = 0; i < encodedValue.length(); i++) {

            char c = encodedValue.charAt(i);
            int value = -1;
            for (int j = 0; j < DIGITS.length; j++) {
                if (DIGITS[j] == c) {
                    value = j;
                    break;
                }
            }

            if (value == -1) {
                throw new IllegalArgumentException("Invalid character in the encoded value: " + c);
            }

            result += (int) (value * Math.pow(BASE, encodedValue.length() - i - 1));
        }

        return result;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 200; i++) {
            System.out.println(Base25Util.encode(i));
        }
        System.out.println(Base25Util.encode(9765624));
        System.out.println(Base25Util.decode("33334"));
        System.out.println(Base25Util.decode("YYYYY"));
    }

}

