package com.brytonsport.active.bleplugin;

import android.util.Base64;
import android.util.Log;
import com.mapbox.android.accounts.v1.MapboxAccounts;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class DataUtil {
    private static final String TAG = "DataUtil";
    private static DataUtil instance;
    static int sendFileType;

    protected DataUtil() {
    }

    public static DataUtil getInstance() {
        if (instance == null) {
            instance = new DataUtil();
        }
        return instance;
    }

    public static JSONObject requestDataResult(byte[] data) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("result", Base64.encodeToString(data, 10));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jSONObject;
    }

    public static String getFileListResult(byte[] data) {
        int length = data.length / 36;
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("extra", length);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONArray jSONArray = new JSONArray();
        char c = 0;
        int i = 0;
        while (i < length) {
            int i2 = i * 36;
            int byte2Int = ParserUtil.byte2Int(data[i2 + 0]);
            byte[] bArr = new byte[4];
            for (int i3 = 0; i3 < 4; i3++) {
                bArr[i3] = data[i2 + i3 + 1];
            }
            int intValue = new BigInteger(bArr).intValue();
            Date date = new Date(ParserUtil.stringToInt(ParserUtil.toBin(bArr[c]) + ParserUtil.toBin(bArr[1]) + ParserUtil.toBin(bArr[2]) + ParserUtil.toBin(bArr[3])) * 1000);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            String format = simpleDateFormat.format(date);
            byte[] bArr2 = new byte[4];
            for (int i4 = 0; i4 < 4; i4++) {
                bArr2[i4] = data[i2 + i4 + 5];
            }
            int intValue2 = new BigInteger(bArr2).intValue();
            byte[] bArr3 = new byte[4];
            for (int i5 = 0; i5 < 4; i5++) {
                bArr3[i5] = data[i2 + i5 + 9];
            }
            int intValue3 = new BigInteger(bArr3).intValue();
            int byte2Int2 = ParserUtil.byte2Int(data[13]);
            byte[] bArr4 = new byte[4];
            for (int i6 = 0; i6 < 4; i6++) {
                bArr4[i6] = data[i2 + i6 + 14];
            }
            int intValue4 = new BigInteger(bArr4).intValue();
            int byte2Int3 = ParserUtil.byte2Int(data[i2 + 18]);
            byte[] bArr5 = new byte[17];
            int i7 = length;
            for (int i8 = 0; i8 < 17; i8++) {
                bArr5[i8] = data[i2 + i8 + 19];
            }
            new BigInteger(bArr5).intValue();
            JSONObject jSONObject2 = new JSONObject();
            try {
                jSONObject2.put("activityDist", intValue3);
                jSONObject2.put("activityTime", intValue2);
                jSONObject2.put("fileId", intValue);
                jSONObject2.put("isWorkout", byte2Int2);
                jSONObject2.put("sportType", byte2Int3);
                jSONObject2.put("workoutId", intValue4);
                jSONObject2.put("fileIdToTimeStamp", format);
                jSONObject2.put("payloadSize", byte2Int);
                jSONArray.put(jSONObject2);
            } catch (Exception unused) {
            }
            i++;
            length = i7;
            c = 0;
        }
        try {
            jSONObject.put("result", jSONArray);
        } catch (JSONException e2) {
            e2.printStackTrace();
        }
        return jSONObject.toString();
    }

    public static JSONObject readDeviceInfoFormat(byte[] data) {
        List<byte[]> split = split(data, (byte) 0);
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        for (byte[] bArr : split) {
            String str = TAG;
            Log.d(str, "readDeviceInfoArray dataArray split = " + Arrays.toString(bArr));
            String str2 = new String(bArr, StandardCharsets.UTF_8);
            JSONArray jSONArray2 = new JSONArray();
            if ("UD".equals(str2.substring(0, 2))) {
                jSONArray2.put("UD");
                jSONArray2.put(str2.substring(2));
                str2.substring(2);
            } else if ("IV".equals(str2.substring(0, 2))) {
                jSONArray2.put("IV");
                jSONArray2.put(str2.substring(2));
                str2.substring(2);
            } else if ("MN".equals(str2.substring(0, 2))) {
                jSONArray2.put("MN");
                jSONArray2.put(str2.substring(2));
                str2.substring(2);
            } else if ("CP".equals(str2.substring(0, 2))) {
                jSONArray2.put("CP");
                jSONArray2.put(str2.substring(2));
                str2.substring(2);
            }
            jSONArray.put(jSONArray2);
            try {
                jSONObject.put("result", jSONArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jSONObject;
    }

    private static List<byte[]> split(byte[] array, byte value) {
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        for (byte b : array) {
            if (b == value && !arrayList2.isEmpty()) {
                arrayList.add(convertToPrimitiveArray(arrayList2));
                arrayList2 = new ArrayList();
            } else if (b != value) {
                arrayList2.add(Byte.valueOf(b));
            }
        }
        if (!arrayList2.isEmpty()) {
            arrayList.add(convertToPrimitiveArray(arrayList2));
        }
        return arrayList;
    }

    private static byte[] convertToPrimitiveArray(List<Byte> list) {
        byte[] bArr = new byte[list.size()];
        for (int i = 0; i < list.size(); i++) {
            bArr[i] = list.get(i).byteValue();
        }
        return bArr;
    }

    public byte[] smartNotifyText(int seqNo) {
        return ParserUtil.joinByteArray(new byte[]{(byte) seqNo, 71, 48}, new byte[]{0});
    }

    public byte[] smartNotifyMail(int seqNo) {
        return ParserUtil.joinByteArray(new byte[]{(byte) seqNo, 70, 48}, new byte[]{0});
    }

    public byte[] smartNotifyPhone(int seqNo, int type, String content) {
        byte[] bArr = {0};
        byte[] bArr2 = new byte[3];
        bArr2[0] = (byte) seqNo;
        bArr2[1] = 69;
        if (type == 0) {
            bArr2[2] = 48;
        } else if (type == 1) {
            bArr2[2] = 49;
        } else if (type == 2) {
            bArr2[2] = 50;
        }
        byte[] bytes = content.getBytes();
        int length = bytes.length < 16 ? bytes.length : 16;
        byte[] bArr3 = new byte[length];
        for (int i = 0; i < length; i++) {
            bArr3[i] = bytes[i];
        }
        return ParserUtil.joinByteArray(bArr2, ParserUtil.joinByteArray(bArr3, bArr));
    }

    public byte[] smartNotifyApp(int seqNo, int type, String content) {
        byte[] bArr = {0};
        byte[] bArr2 = new byte[3];
        bArr2[0] = (byte) seqNo;
        bArr2[1] = 65;
        if (type == 0) {
            bArr2[2] = 48;
        } else if (type == 1) {
            bArr2[2] = 49;
        } else if (type == 2) {
            bArr2[2] = 50;
        }
        byte[] bytes = content.getBytes();
        int length = bytes.length < 16 ? bytes.length : 16;
        byte[] bArr3 = new byte[length];
        for (int i = 0; i < length; i++) {
            bArr3[i] = bytes[i];
        }
        return ParserUtil.joinByteArray(bArr2, ParserUtil.joinByteArray(bArr3, bArr));
    }

    public byte[] requestDataCmd(int cmdId, int seqNoForTrans) {
        return requestDataCmd(cmdId, seqNoForTrans, 0, 0, "");
    }

    public byte[] requestDataCmd(int cmdId, int seqNoForTrans, String userAddress) {
        return requestDataCmd(cmdId, seqNoForTrans, 0, 0, userAddress);
    }

    public byte[] requestDataCmd(int cmdId, int seqNoForTrans, int fileType) {
        return requestDataCmd(cmdId, seqNoForTrans, fileType, 0, "");
    }

    public byte[] requestDataCmd(int cmdId, int seqNoForTrans, int fileType, int fileId, String userAddress) {
        return requestDataCmd(cmdId, seqNoForTrans, fileType, fileId, 0, 0, userAddress);
    }

    public byte[] requestDataCmd(int cmdId, int seqNoForTrans, int fileType, int fileId, int offset, int chunkSize, String userAddress) {
        byte[] bArr;
        if (cmdId == 8) {
            bArr = new byte[10];
        } else if (cmdId != 12) {
            switch (cmdId) {
                case 15:
                case 16:
                    bArr = new byte[3];
                    break;
                case 17:
                    bArr = new byte[15];
                    break;
                default:
                    bArr = new byte[2];
                    break;
            }
        } else {
            bArr = new byte[7];
        }
        bArr[0] = (byte) cmdId;
        String intToBinaryString = ParserUtil.intToBinaryString(seqNoForTrans, 3);
        bArr[1] = Byte.parseByte("000" + intToBinaryString + MapboxAccounts.SKU_ID_MAPS_MAUS, 2);
        if (cmdId != 8) {
            if (cmdId == 12) {
                byte[] fitFileNameToByteArray = ParserUtil.fitFileNameToByteArray(fileId);
                bArr[2] = fitFileNameToByteArray[0];
                bArr[3] = fitFileNameToByteArray[1];
                bArr[4] = fitFileNameToByteArray[2];
                bArr[5] = fitFileNameToByteArray[3];
                bArr[6] = (byte) fileType;
            } else {
                switch (cmdId) {
                    case 15:
                        bArr[2] = (byte) fileType;
                        break;
                    case 16:
                        sendFileType = fileType;
                        bArr[2] = (byte) fileType;
                        break;
                    case 17:
                        byte[] fitFileNameToByteArray2 = ParserUtil.fitFileNameToByteArray(fileId);
                        bArr[2] = fitFileNameToByteArray2[0];
                        bArr[3] = fitFileNameToByteArray2[1];
                        bArr[4] = fitFileNameToByteArray2[2];
                        bArr[5] = fitFileNameToByteArray2[3];
                        bArr[6] = (byte) fileType;
                        byte[] array = ByteBuffer.allocate(4).putInt(offset).array();
                        bArr[7] = array[0];
                        bArr[8] = array[1];
                        bArr[9] = array[2];
                        bArr[10] = array[3];
                        byte[] array2 = ByteBuffer.allocate(4).putInt(chunkSize).array();
                        bArr[11] = array2[0];
                        bArr[12] = array2[1];
                        bArr[13] = array2[2];
                        bArr[14] = array2[3];
                        break;
                }
            }
        } else {
            byte[] bytes = userAddress.getBytes();
            for (int i = 2; i < bArr.length; i++) {
                bArr[i] = bytes[i - 2];
            }
        }
        return bArr;
    }
}