package q7;

import android.util.Base64;
import android.util.JsonWriter;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import xyz.stream.utils.f0;
import xyz.stream.utils.j0;

/* JADX INFO: loaded from: classes2.dex */
public final class g {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static boolean f32231c = false;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static boolean f32232d = false;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Object f32230b = new Object();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final HashSet f32233e = new HashSet(Arrays.asList(new String[0]));

    public g(@Nullable String str) {
        this.f32234a = !b() ? new ArrayList() : Arrays.asList("network_request_".concat(String.valueOf(UUID.randomUUID().toString())));
    }

    public static boolean b() {
        boolean z10;
        synchronized (f32230b) {
            try {
                z10 = false;
                if (f32231c && f32232d) {
                    z10 = true;
                }
            } finally {
            }
        }
        return z10;
    }

    public static void c(String str, String str2, Map map, byte[] bArr, JsonWriter jsonWriter) throws IOException {
        jsonWriter.name("params").beginObject();
        jsonWriter.name("firstline").beginObject();
        jsonWriter.name("uri").value(str);
        jsonWriter.name("verb").value(str2);
        jsonWriter.endObject();
        zzp(jsonWriter, map);
        if (bArr != null) {
            jsonWriter.name("body").value(Base64.encodeToString(bArr, 0));
        }
        jsonWriter.endObject();
    }

    public static /* synthetic */ void d(int i10, Map map, JsonWriter jsonWriter) throws IOException {
        jsonWriter.name("params").beginObject();
        jsonWriter.name("firstline").beginObject();
        jsonWriter.name("code").value(i10);
        jsonWriter.endObject();
        zzp(jsonWriter, map);
        jsonWriter.endObject();
    }

    private final void zzm(String str, String str2, @Nullable Map map, @Nullable byte[] bArr) {
        e("onNetworkRequest", new u.e(str, str2, map, bArr));
    }

    private final void zzn(@Nullable Map map, int i10) {
        e("onNetworkResponse", new j2.e(i10, map));
    }

    private final void zzo(@Nullable String str) {
        e("onNetworkRequestError", new j0(str, 15));
    }

    private static void zzp(JsonWriter jsonWriter, @Nullable Map map) throws IOException {
        if (map == null) {
            return;
        }
        jsonWriter.name("headers").beginArray();
        Iterator it = map.entrySet().iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Map.Entry entry = (Map.Entry) it.next();
            String str = (String) entry.getKey();
            if (!f32233e.contains(str)) {
                if (!(entry.getValue() instanceof List)) {
                    if (!(entry.getValue() instanceof String)) {
                        j.b("Connection headers should be either Map<String, String> or Map<String, List<String>>");
                        break;
                    }
                    jsonWriter.beginObject();
                    jsonWriter.name("name").value(str);
                    jsonWriter.name("value").value((String) entry.getValue());
                    jsonWriter.endObject();
                } else {
                    for (String str2 : (List) entry.getValue()) {
                        jsonWriter.beginObject();
                        jsonWriter.name("name").value(str);
                        jsonWriter.name("value").value(str2);
                        jsonWriter.endObject();
                    }
                }
            }
        }
        jsonWriter.endArray();
    }

    public final void a(HttpURLConnection httpURLConnection, int i10) {
        if (b()) {
            String responseMessage = null;
            zzn(httpURLConnection.getHeaderFields() == null ? null : new HashMap(httpURLConnection.getHeaderFields()), i10);
            if (i10 < 200 || i10 >= 300) {
                try {
                    responseMessage = httpURLConnection.getResponseMessage();
                } catch (IOException e10) {
                    j.d("Can not get error message from error HttpURLConnection\n".concat(String.valueOf(e10.getMessage())));
                }
                zzo(responseMessage);
            }
        }
    }

    public final void e(String str, f fVar) {
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter);
        try {
            jsonWriter.beginObject();
            jsonWriter.name("timestamp").value(System.currentTimeMillis());
            jsonWriter.name("event").value(str);
            jsonWriter.name("components").beginArray();
            Iterator it = this.f32234a.iterator();
            while (it.hasNext()) {
                jsonWriter.value((String) it.next());
            }
            jsonWriter.endArray();
            fVar.c(jsonWriter);
            jsonWriter.endObject();
            jsonWriter.flush();
            jsonWriter.close();
        } catch (IOException unused) {
        }
        String string = stringWriter.toString();
        synchronized (g.class) {
            try {
                j.c("GMA Debug BEGIN");
                int i10 = 0;
                while (i10 < string.length()) {
                    int i11 = i10 + 4000;
                    j.c("GMA Debug CONTENT ".concat(String.valueOf(string.substring(i10, Math.min(i11, string.length())))));
                    i10 = i11;
                }
                j.c("GMA Debug FINISH");
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void zza(HttpURLConnection httpURLConnection, @Nullable byte[] bArr) {
        if (b()) {
            zzm(new String(httpURLConnection.getURL().toString()), new String(httpURLConnection.getRequestMethod()), httpURLConnection.getRequestProperties() == null ? null : new HashMap(httpURLConnection.getRequestProperties()), bArr);
        }
    }

    public final void zzb(String str, String str2, @Nullable Map map, @Nullable byte[] bArr) {
        if (b()) {
            zzm(str, "GET", map, bArr);
        }
    }

    public final void zzd(@Nullable Map map, int i10) {
        if (b()) {
            zzn(map, i10);
            if (i10 < 200 || i10 >= 300) {
                zzo(null);
            }
        }
    }

    public final void zze(@Nullable String str) {
        if (b() && str != null) {
            e("onNetworkResponseBody", new f0(str.getBytes(), 12));
        }
    }
}
