package com.quyunshuo.androidbaseframemvvm.base.utils.commons;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class StringUtils {
    public StringUtils() {
    }
    /**
     * 将整数转换为指定位数的十六进制字符串
     * @param num 输入整数（支持负数补码转换）
     * @param bits 目标十六进制位数（1~8）
     * @param uppercase 是否大写字母
     * @param truncate 超长时是否截断高位（false时抛异常）
     * @return 格式化后的十六进制字符串
     * @throws IllegalArgumentException 参数不合法或数据溢出
     */
    public static String intToHex(
            int num,
            int bits,
            boolean uppercase,
            boolean truncate
    ) throws IllegalArgumentException {
        // 参数校验
        if (bits < 1 || bits > 8) {
            throw new IllegalArgumentException("Bits must be between 1 and 8");
        }

        long maxVal = (1L << (bits * 4)) - 1;
        long minVal = (bits < 8) ? -(1L << (bits * 4 - 1)) : Integer.MIN_VALUE;

        // 溢出检查
        if (!truncate) {
            if (num > maxVal || num < minVal) {
                throw new IllegalArgumentException(
                        "Value " + num + " out of range for " + bits + " hex digits"
                );
            }
        }

        // 转换为无符号数值（处理补码）
        long mask = (1L << (bits * 4)) - 1;
        long unsignedNum = num & mask;

        // 格式化为字符串
        String format = "%0" + bits + (uppercase ? "X" : "x");
        return String.format(format, unsignedNum);
    }

    // 重载简化方法
    public static String intToHex(int num, int bits) {
        return intToHex(num, bits, true, true);
    }
    public static boolean equals(CharSequence cs1, CharSequence cs2) {
        if (cs1 == cs2) {
            return true;
        } else if (cs1 != null && cs2 != null) {
            if (cs1 instanceof String && cs2 instanceof String) {
                return cs1.equals(cs2);
            } else {
                return cs1.length() == cs2.length() && CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, cs1.length());
            }
        } else {
            return false;
        }
    }

    private static ByteBuffer getByteBuffer(String string, Charset charset) {
        return string == null ? null : ByteBuffer.wrap(string.getBytes(charset));
    }

    public static ByteBuffer getByteBufferUtf8(String string) {
        return getByteBuffer(string, StandardCharsets.UTF_8);
    }

    private static byte[] getBytes(String string, Charset charset) {
        return string == null ? null : string.getBytes(charset);
    }

    public static byte[] getBytesIso8859_1(String string) {
        return getBytes(string, StandardCharsets.ISO_8859_1);
    }

    public static byte[] getBytesUnchecked(String string, String charsetName) {
        if (string == null) {
            return null;
        } else {
            try {
                return string.getBytes(charsetName);
            } catch (UnsupportedEncodingException var3) {
                UnsupportedEncodingException e = var3;
                throw newIllegalStateException(charsetName, e);
            }
        }
    }

    public static byte[] getBytesUsAscii(String string) {
        return getBytes(string, StandardCharsets.US_ASCII);
    }

    public static byte[] getBytesUtf16(String string) {
        return getBytes(string, StandardCharsets.UTF_16);
    }

    public static byte[] getBytesUtf16Be(String string) {
        return getBytes(string, StandardCharsets.UTF_16BE);
    }

    public static byte[] getBytesUtf16Le(String string) {
        return getBytes(string, StandardCharsets.UTF_16LE);
    }

    public static byte[] getBytesUtf8(String string) {
        return getBytes(string, StandardCharsets.UTF_8);
    }

    private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) {
        return new IllegalStateException(charsetName + ": " + e);
    }

    private static String newString(byte[] bytes, Charset charset) {
        return bytes == null ? null : new String(bytes, charset);
    }

    public static String newString(byte[] bytes, String charsetName) {
        if (bytes == null) {
            return null;
        } else {
            try {
                return new String(bytes, charsetName);
            } catch (UnsupportedEncodingException var3) {
                UnsupportedEncodingException e = var3;
                throw newIllegalStateException(charsetName, e);
            }
        }
    }

    public static String newStringIso8859_1(byte[] bytes) {
        return newString(bytes, StandardCharsets.ISO_8859_1);
    }

    public static String newStringUsAscii(byte[] bytes) {
        return newString(bytes, StandardCharsets.US_ASCII);
    }

    public static String newStringUtf16(byte[] bytes) {
        return newString(bytes, StandardCharsets.UTF_16);
    }

    public static String newStringUtf16Be(byte[] bytes) {
        return newString(bytes, StandardCharsets.UTF_16BE);
    }

    public static String newStringUtf16Le(byte[] bytes) {
        return newString(bytes, StandardCharsets.UTF_16LE);
    }

    public static String newStringUtf8(byte[] bytes) {
        return newString(bytes, StandardCharsets.UTF_8);
    }

    public static int parseInt(String hex) {
        return Integer.parseInt(hex, 16);
    }
    /**
     * 严格模式转换（限制输入范围）
     * @param value 0-65535 范围的整数
     * @return 4位大写十六进制字符串
     * @throws IllegalArgumentException 输入超出范围时抛出
     */
    public static String intTo4DigitHexStrict(int value) {
        if (value < 0 || value > 0xFFFF) {
            throw new IllegalArgumentException(
                    "Value " + value + " exceeds 16-bit range (0-65535)");
        }
        return String.format("%04X", value);
    }
    // 判断字符串是否为null或空
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.isEmpty();
    }

    // 判断字符串是否不为null且不为空
    public static boolean isNotNullOrEmpty(String str) {
        return !isNullOrEmpty(str);
    }

}
