package com.xhpc.pp.utils;

import org.apache.commons.lang3.ArrayUtils;

import java.util.Objects;

public class HexUtils {

    /**
     * 字节数据转字符串专用集合
     */
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static String toHexInt(long dec) {

        return toHex(toIntBytes(String.format("%08d", dec), 8));
    }

    public static int reverseHexInt(String hex) {

        byte[] data = toBytes(reverseHex(hex));
        return toInteger(data, 0, data.length);
    }

    public static String reverseHex(String hex) {

        int len = hex.length();
        String revers = "";
        for (int i = len; i > 0; i = i - 2) {
            revers = revers.concat(hex.substring(i - 2, i));
        }
        return revers;
    }

    public static String toHex(byte[] data) {

        StringBuilder stringBuilder = new StringBuilder();
        for (byte datum : data) {
            // 取出字节的高四位 作为索引得到相应的十六进制标识符 注意无符号右移
            stringBuilder.append(HEX_CHAR[(datum & 0xf0) >>> 4]);
            // 取出字节的低四位 作为索引得到相应的十六进制标识符
            stringBuilder.append(HEX_CHAR[(datum & 0x0f)]);
        }
        return stringBuilder.toString();

    }

    public static String toHex(byte[] data, int start, int end) {

        if (data == null || data.length < end)
            return "";
        return toHex(ArrayUtils.subarray(data, start, end));
    }

    public static String toString(byte[] data) {

        return new String(data);
    }

    public static String toString(byte[] data, int start, int end) {

        if (data == null || data.length < end)
            return "";
        return toString(ArrayUtils.subarray(data, start, end));
    }

    public static int toInteger(byte[] data, int start, int end) {

        if (data == null || data.length < end)
            return 0;
        return Integer.decode("0x" + toHex(data, start, end));
    }

    public static byte[] toBytes(String hex) {

        if (hex == null) return null;
        hex = hex.trim();
        if (hex.length() == 0 || hex.length() % 2 != 0)
            return null;
        byte[] b = new byte[hex.length() / 2];
        for (int i = 0; i < hex.length(); i = i + 2) {
            b[i / 2] = Integer.decode("0x" + hex.substring(i, i + 2)).byteValue();
        }
        return b;
    }

    public static byte[] stringToBytes(String str, int len) {

        byte[] data = str.getBytes();
        if (data.length > len)
            return data;
        for (int i = data.length; i < len; i++)
            data = ArrayUtils.add(data, (byte) 0);
        return data;
    }

    public static String toIntString(String hex) {

        return toIntString(hex, 4);
    }

    public static String toIntString(String hex, int len) {

        if (hex == null || hex.length() == 0 || hex.length() % len != 0)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hex.length(); i = i + len) {
            sb.append(String.format("%0" + len + "d", byte2int(Objects.requireNonNull(toBytes(hex.substring(i, i + len))))));
        }
        return sb.toString();
    }

    public static byte[] toIntBytes(String num, int len) {

        if (num == null || num.length() == 0 || num.length() % len != 0)
            return null;
        byte[] result = new byte[0];
        for (int i = 0; i < num.length(); i = i + len) {
            int n = Integer.parseInt(num.substring(i, i + len));

            result = ArrayUtils.addAll(result, toIntBytes(n, len / 2));
        }
        return result;
    }

    public static byte[] toIntBytes(int n, int len) {

        byte[] b = new byte[len];
        for (int i = 0; i < len; i++) {
            b[i] = (byte) (n >> i * 8 & 0xff);
        }
        return b;
    }

    public static int byte2int(byte[] b) {

        int res = 0;
        int bLen = b.length;
        if (bLen < 5) {// int 最大到4个字节
            for (int i = 0; i < bLen; i++) {
                res += (b[i] & 0xFF) << (8 * i);
            }
        }
        return res;
    }

    public static byte[] toLongBytes(long n) {

        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) ((n >> i * 8) & 0xff);
        }
        return b;
    }

    // 使用小端 字节index 0 对应 int 的最后一个字节
    public static long byte2Long(byte[] b) {

        long res = 0;
        int bLen = b.length;
        // int 最大到4个字节
        if (bLen < 9) {
            for (int i = 0; i < bLen; i++) {
                res += (b[i] & 0xFFL) << 8 * i;
            }
        }
        return res;
    }

    public static String toBits(String hex) {

        byte[] bytes = toBytes(hex);
        final StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            for (int i = 0; i < 8; i++) {
                result.append(b >> (8 - (i + 1)) & 0x0001);
            }
        }
        return result.toString();
    }

    public static final String toAscii(String hex) {

        if (hex == null) return null;
        byte[] bytes = toBytes(hex);
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(Character.valueOf((char) b));
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        System.out.println(Long.valueOf(10L).toString());
//        System.out.println(reverseHexInt("FF00"));
//        byte[] data1 = toBytes(reverseHex("10270000"));
//        System.out.println(toInteger(data1, 0, 4));
        System.out.println(reverseHexInt("D80E"));
        System.out.println(toHexInt(3800));
//        System.out.println(toHexInt(100000));
//        System.out.println(toBits("1000"));
//        System.out.println(toHex(new byte[]{104, 13, 19, 123, 0, 3, 105, -123, 71, -123, -106, 50, 84, 1, 0, 72, 106, 104,
//        -94, 19, 124, 0, 59, 105, -123, 71, -123, -106, 50, 84, 1, 33, 9, 24, 9, 67, 57, 0, 2, 105, -123, 71, -123, -106,
//        50, 84, 1, -88, 97, 43, 9, 50, 9, 21, 48, 117, 50, 9, 50, 9, 21, -64, 87, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
//        -40, 83, 1, 0}));

    }

}
