package com.brytonsport.active.bleplugin;

import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;
import kotlin.UByte;
import org.apache.xerces.impl.xs.SchemaSymbols;

/* loaded from: classes.dex */
public class ParserUtil {
    public static int byte2Int(byte b) {
        return b;
    }

    public static int getUnsignedByte(byte b) {
        return b & UByte.MAX_VALUE;
    }

    public static byte[] InputStreamToByte(InputStream is) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        while (true) {
            int read = is.read();
            if (read != -1) {
                byteArrayOutputStream.write(read);
            } else {
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                byteArrayOutputStream.close();
                return byteArray;
            }
        }
    }

    public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
        return ByteBuffer.allocate(byte1.length + byte2.length).put(byte1).put(byte2).array();
    }

    public static byte[] bufToDataPkg(byte[] buf, int offset, int size) {
        byte[] bArr = new byte[size + 8];
        return joinByteArray(joinByteArray(ByteBuffer.allocate(4).putInt(offset).array(), ByteBuffer.allocate(4).putInt(buf.length).array()), Arrays.copyOfRange(buf, offset, size + offset));
    }

    public static String byteToBin(byte b) {
        String binaryString = Integer.toBinaryString(b & UByte.MAX_VALUE);
        if (binaryString.length() < 8) {
            return "00000000".substring(0, 8 - binaryString.length()) + binaryString;
        }
        return binaryString;
    }

    public static Integer getBitByByte(byte b, int begIndex, int endIndex) {
        if (begIndex >= 8 || endIndex >= 8 || begIndex >= endIndex) {
            return null;
        }
        return Integer.valueOf(Integer.parseInt(byteToBin(b).substring(begIndex, endIndex + 1), 2));
    }

    public static String toBin(byte numDec) {
        int unsignedByte = getUnsignedByte(numDec);
        if (unsignedByte == 0) {
            return "00000000";
        }
        String binaryString = Integer.toBinaryString(unsignedByte);
        int length = 8 - (binaryString.length() % 8);
        if (length != 8) {
            for (int i = 0; i < length; i++) {
                binaryString = SchemaSymbols.ATTVAL_FALSE_0 + binaryString;
            }
        }
        return binaryString;
    }

    public static long stringToInt(String bin) {
        String trim;
        long j = 0;
        for (int length = bin.trim().length() - 1; length >= 0; length--) {
            j = (long) (j + (Integer.parseInt(String.valueOf(trim.charAt(length))) * Math.pow(2.0d, (trim.length() - 1) - length)));
        }
        return j;
    }

    public static String fileIdToFormatString(int fileId) {
        byte[] array = ByteBuffer.allocate(4).putInt(fileId).array();
        Date date = new Date(stringToInt(toBin(array[0]) + toBin(array[1]) + toBin(array[2]) + toBin(array[3])) * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return simpleDateFormat.format(date);
    }

    public static String fileIdToFormatString(long fileId) {
        Date date = new Date(new Timestamp(1000 * fileId).getTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        String format = simpleDateFormat.format(date);
        Log.d("轉時間", "fileIdToFormatString: fileId: " + fileId + ", result: " + format);
        return format;
    }

    public static String fitNameToFormatName(byte[] data) {
        Date date = new Date(stringToInt(toBin(data[2]) + toBin(data[3]) + toBin(data[4]) + toBin(data[5])) * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return simpleDateFormat.format(date);
    }

    public static byte[] fitFileNameToByteArray(int fileId) {
        Date date;
        Log.d("ParserUtil", "fitFileNameToByteArray() -> fileId = " + fileId);
        String fileIdToFormatString = fileIdToFormatString(fileId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            date = simpleDateFormat.parse(fileIdToFormatString);
        } catch (ParseException e) {
            e.printStackTrace();
            date = null;
        }
        if (date != null) {
            int time = (int) (date.getTime() / 1000);
            return new byte[]{(byte) (time >> 24), (byte) (time >> 16), (byte) (time >> 8), (byte) time};
        }
        return null;
    }

    public static String intToBinaryString(int integer, int numberOfBits) {
        if (numberOfBits > 0) {
            return String.format("%" + numberOfBits + "s", Integer.toBinaryString(integer)).replaceAll(" ", SchemaSymbols.ATTVAL_FALSE_0);
        }
        return null;
    }

    public static byte[] encodeCmd(byte[] data) {
        data[data.length - 1] = (byte) calChecksum(data);
        return data;
    }

    public static boolean isValidData(byte[] data) {
        int i = 0;
        for (int i2 = 0; i2 < data.length - 1; i2++) {
            i += data[i2];
        }
        return i % 256 == data[data.length - 1];
    }

    public static int calChecksum(byte[] data) {
        int i = 0;
        for (int i2 = 0; i2 < data.length - 1; i2++) {
            i += getUnsignedByte(data[i2]);
        }
        return i % 256;
    }

    public static long toUInt(final byte[] data) {
        if (data == null || data.length != 4) {
            throw new IllegalArgumentException("!= 4 bytes");
        }
        return ((data[3] & 255) << 24) | ((data[2] & 255) << 16) | ((data[1] & 255) << 8) | (255 & data[0]);
    }

    public static int toUShort(final byte[] data) {
        if (data == null || data.length != 2) {
            throw new IllegalArgumentException("!= 2 bytes");
        }
        return (data[0] & UByte.MAX_VALUE) | ((data[1] & UByte.MAX_VALUE) << 8);
    }
}