package com.hboxs.serialport.plc.util;

import android.util.Log;

import com.quyunshuo.androidbaseframemvvm.common.util.ByteUtils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class HexUtils {
    private static final String TAG = "HexUtils";

    /**
     * 16进制字符串转2进制字符串
     *
     * @param hexStr 16进制字符串
     * @return 2进制字符串
     */
    public static String hexStr2BinStr(String hexStr) {
        // 16进制转2进制
        StringBuilder binDataStrBuilder = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i++) {
            String hex = Integer.toBinaryString(Integer.parseInt(hexStr.charAt(i) + "", 16));
            if (hex.length() == 1) {
                binDataStrBuilder.append("000");
            } else if (hex.length() == 2) {
                binDataStrBuilder.append("00");
            } else if (hex.length() == 3) {
                binDataStrBuilder.append("0");
            }
            binDataStrBuilder.append(hex);
        }


        return binDataStrBuilder.toString();
    }

    public static  boolean sumHexPairs(String hexString) {
        String data = hexString.substring(0, hexString.length() - 4);
        Log.d(TAG, "sumHexPairs: "+data);
        String jym = hexString.substring(hexString.length() - 4);
        int sum = 0;
        int length = data.length();

        for (int i = 0; i < length; i += 2) {
            String hexPair = data.substring(i, i + 2);
            int decimalValue = Integer.parseInt(hexPair, 16);
            sum += decimalValue;
        }

        int diwei = sum %256;//低位
        int gaowei = sum/256;//高位

        String lowStr = ByteUtils.decimal2fitHex(diwei, 2);//低位
        String highStr = ByteUtils.decimal2fitHex(gaowei, 2);//高位

        Log.d(TAG, sum+":sumHexPairs: "+lowStr+":"+ highStr +":"+jym);
        String jymStr = highStr+lowStr;
        Log.d(TAG, "jymStr:"+jymStr+"jym"+jym);

        if (jymStr.equals(jym)) {
            return true;
        }
        return false;
    }
    /**
     * 字符串转16进制字节数组
     * (原始字符串 → 16进制表示的字节数组)
     *
     * 示例: "ab" → [0x61, 0x62] (即字节数组包含'a'和'b'的ASCII码)
     *
     * @param text     输入字符串
     * @param charset  字符编码 (默认UTF-8)
     * @return 16进制格式的字节数组
     */
    public static byte[] stringToHexBytes(String text, Charset charset) {
        if (text == null) return null;
        charset = (charset == null) ? StandardCharsets.UTF_8 : charset;

        // 1. 字符串转原始字节
        byte[] rawBytes = text.getBytes(charset);
        // 2. 原始字节转16进制字符数组
        char[] hexChars = Hex.encodeHex(rawBytes, false);
        // 3. 字符数组转字节数组 (ASCII编码)
        return new String(hexChars).getBytes(StandardCharsets.US_ASCII);
    }

    /**
     * 字符串转16进制字符串
     * (原始字符串 → 16进制表示的字符串)
     *
     * 示例: "ab" → "6162"
     *
     * @param text     输入字符串
     * @param charset  字符编码 (默认UTF-8)
     * @return 16进制字符串 (小写形式)
     */
    public static String stringToHexString(String text, Charset charset) {
        if (text == null) return null;
        charset = (charset == null) ? StandardCharsets.UTF_8 : charset;
        return Hex.encodeHexString(text.getBytes(charset));
    }

    /**
     * 16进制字节数组转原始字符串 (反向操作)
     */
    public static String hexBytesToString(byte[] hexBytes, Charset charset) throws DecoderException {
        if (hexBytes == null) return null;
        charset = (charset == null) ? StandardCharsets.UTF_8 : charset;

        // 1. 16进制字节数组转字符串 (ASCII解码)
        String hexString = new String(hexBytes, StandardCharsets.US_ASCII);
        // 2. 16进制字符串转原始字节
        byte[] rawBytes = Hex.decodeHex(hexString.toCharArray());
        // 3. 原始字节转字符串
        return new String(rawBytes, charset);
    }

    /**
     * 16进制字符串转原始字符串 (反向操作)
     */
    public static String hexStringToString(String hexString, Charset charset) throws DecoderException {
        if (hexString == null) return null;
        charset = (charset == null) ? StandardCharsets.UTF_8 : charset;
        return new String(Hex.decodeHex(hexString.toCharArray()), charset);
    }

    // 重载方法：使用默认UTF-8编码
    public static byte[] stringToHexBytes(String text) {
        return stringToHexBytes(text, StandardCharsets.UTF_8);
    }

    public static String stringToHexString(String text) {
        return stringToHexString(text, StandardCharsets.UTF_8);
    }
}
