package a.b.a.f;

import a.a.a.b;
import a.b.a.f.h;
import android.bluetooth.le.AdvertiseData;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.common.io.BaseEncoding;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Random;

public class a {

    /* renamed from: a  reason: collision with root package name */
    private static final String f9a = "ScanResponseBuilder";
    private static final String b = "\\.";
    private static final int c = 2;
    private static final int d = 16;
    private static final int e = 6;
    private static final int f = 2;
    private static final int g = 4;
    private static final int h = 1;
    private static final int i = 1;
    private static final byte[] j;
    private static final String k = "serial_num_prefs";
    private static final String l = "last_serial_num";

    static {
        byte[] bArr = new byte[1];
        j = bArr;
        new Random().nextBytes(bArr);
    }

    private a() {
    }

    public static AdvertiseData a(String str) {
        if (str != null) {
            return new AdvertiseData.Builder().addServiceData(h.b.d, a.b.a.i.a.a(d(str))).build();
        }
        throw new IllegalArgumentException();
    }

    public static AdvertiseData a(String str, String str2, String str3, String str4, int i2, boolean z, Context context) {
        if (str == null || str2 == null || str3 == null || str4 == null) {
            throw new IllegalArgumentException();
        }
        return new AdvertiseData.Builder().addServiceUuid(h.b.b).addServiceData(h.b.c, a.b.a.i.a.a(f(str), a(z, context), b(str2), c(str3), e(str4), a(i2))).build();
    }

    private static byte[] a(int i2) {
        return new byte[]{(byte) i2};
    }

    private static byte[] a(boolean z, Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(k, 0);
        if (!z) {
            int i2 = sharedPreferences.getInt(l, 0);
            b.c(f9a, "advertising new serial number");
            byte[] bArr = j;
            bArr[0] = (byte) (bArr[0] + 1);
            while (true) {
                byte[] bArr2 = j;
                if (i2 != bArr2[0]) {
                    break;
                }
                new Random().nextBytes(bArr2);
            }
        }
        StringBuilder append = new StringBuilder().append("advertising serial number is :");
        byte[] bArr3 = j;
        b.c(f9a, append.append((int) bArr3[0]).toString());
        sharedPreferences.edit().putInt(l, bArr3[0]).apply();
        return bArr3;
    }

    private static byte[] b(String str) {
        return Arrays.copyOfRange(BaseEncoding.base16().lowerCase().decode(str.toLowerCase()), 0, 6);
    }

    private static byte[] c(String str) {
        return Arrays.copyOfRange(BaseEncoding.base16().lowerCase().decode(str.toLowerCase()), 0, 4);
    }

    private static byte[] d(String str) {
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        while (bytes.length > 16 && !str.isEmpty()) {
            str = str.substring(0, str.length() - 1);
            bytes = c.a(str + "…");
        }
        return bytes.length < 16 ? Arrays.copyOfRange(bytes, 0, 16) : bytes;
    }

    private static byte[] e(String str) {
        return Arrays.copyOfRange(BaseEncoding.base16().lowerCase().decode(str.toLowerCase()), 0, 2);
    }

    private static byte[] f(String str) {
        String[] split = str.split(b);
        if (split.length >= 2) {
            byte[] bArr = new byte[2];
            for (int i2 = 0; i2 < 2; i2++) {
                bArr[i2] = (byte) Integer.parseInt(split[i2]);
            }
            return bArr;
        }
        throw new IllegalArgumentException();
    }
}
