package com.google.android.gms.internal.ads;

import android.content.Context;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/* JADX INFO: loaded from: classes2.dex */
public abstract class qj1 {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final String f15759h = new UUID(0, 0).toString();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String f15760a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f15761b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final String f15762c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final String f15763d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final String f15764e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final pj1 f15765f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final oj1 f15766g;

    public qj1(Context context, String str, String str2, String str3) {
        if (pj1.f15444c == null) {
            pj1.f15444c = new pj1(context);
        }
        this.f15765f = pj1.f15444c;
        this.f15766g = oj1.a(context);
        this.f15760a = str;
        this.f15761b = str.concat("_3p");
        this.f15762c = str2;
        this.f15763d = str2.concat("_3p");
        this.f15764e = str3;
    }

    public final nj1 a(String str, String str2) throws IOException {
        if (str == null) {
            return c(UUID.randomUUID().toString(), false);
        }
        String string = UUID.randomUUID().toString();
        this.f15765f.a(string, "paid_3p_hash_key");
        return c(d(str, str2, string), true);
    }

    public final void b(boolean z10) throws IOException {
        String str = z10 ? this.f15763d : this.f15762c;
        pj1 pj1Var = this.f15765f;
        pj1Var.b(str);
        pj1Var.b(z10 ? this.f15761b : this.f15760a);
    }

    public final nj1 c(String str, boolean z10) throws IOException {
        long jCurrentTimeMillis = System.currentTimeMillis();
        if (jCurrentTimeMillis < 0) {
            throw new IllegalStateException(this.f15764e.concat(": Invalid negative current timestamp. Updating PAID failed"));
        }
        String str2 = z10 ? this.f15763d : this.f15762c;
        Long lValueOf = Long.valueOf(jCurrentTimeMillis);
        pj1 pj1Var = this.f15765f;
        pj1Var.a(lValueOf, str2);
        pj1Var.a(str, z10 ? this.f15761b : this.f15760a);
        return new nj1(str, jCurrentTimeMillis);
    }

    public final String d(String str, String str2, String str3) {
        if (str2 != null && str3 != null) {
            return UUID.nameUUIDFromBytes(defpackage.e.p(new StringBuilder(str2.length() + str.length() + str3.length()), str, str2, str3).getBytes(StandardCharsets.UTF_8)).toString();
        }
        String str4 = str2 == null ? "null" : "not null";
        int length = str4.length();
        String str5 = str3 == null ? "null" : "not null";
        StringBuilder sb = new StringBuilder(str5.length() + length + 120);
        u.a.h(sb, this.f15764e, ": Invalid argument to generate PAIDv1 on 3p traffic, Ad ID is not null, package name is ", str4, ", hashKey is ");
        sb.append(str5);
        throw new IllegalArgumentException(sb.toString());
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x003b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final nj1 zza(@Nullable String str, @Nullable String str2, long j10, boolean z10) throws IOException {
        String str3 = this.f15761b;
        pj1 pj1Var = this.f15765f;
        if (str != null) {
            try {
                UUID.fromString(str);
                if (!str.equals(f15759h)) {
                    String string = pj1Var.f15446b.getString(str3, null);
                    String string2 = pj1Var.f15446b.getString("paid_3p_hash_key", null);
                    if (string != null && string2 != null) {
                        if (!string.equals(d(str, str2, string2))) {
                            return a(str, str2);
                        }
                    }
                }
            } catch (IllegalArgumentException unused) {
            }
            return new nj1();
        }
        boolean z11 = str != null;
        long jCurrentTimeMillis = System.currentTimeMillis();
        if (jCurrentTimeMillis < 0) {
            throw new IllegalStateException(this.f15764e.concat(": Invalid negative current timestamp. Updating PAID failed"));
        }
        String str4 = this.f15762c;
        String str5 = this.f15763d;
        long j11 = pj1Var.f15446b.getLong(z11 ? str5 : str4, -1L);
        if (j11 != -1) {
            if (jCurrentTimeMillis < j11) {
                pj1Var.a(Long.valueOf(jCurrentTimeMillis), z11 ? str5 : str4);
            } else if (jCurrentTimeMillis >= j11 + j10) {
                return a(str, str2);
            }
        }
        if (!z11) {
            str3 = this.f15760a;
        }
        String string3 = pj1Var.f15446b.getString(str3, null);
        if (string3 == null && !z10) {
            return a(str, str2);
        }
        if (z11) {
            str4 = str5;
        }
        return new nj1(string3, pj1Var.f15446b.getLong(str4, -1L));
    }
}
