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

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Context f10002e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f10003f;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public AtomicBoolean f10005h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public File f10006i;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ArrayBlockingQueue f9998a = new ArrayBlockingQueue(100);

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f10000c = new HashMap();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final HashSet f10004g = new HashSet(Arrays.asList("noop", "activeViewPingSent", "viewabilityChanged", "visibilityChanged"));

    public final void a(Map map, lo loVar) {
        FileOutputStream fileOutputStream;
        Uri.Builder builderBuildUpon = Uri.parse(this.f10001d).buildUpon();
        for (Map.Entry entry : map.entrySet()) {
            builderBuildUpon.appendQueryParameter((String) entry.getKey(), (String) entry.getValue());
        }
        String string = builderBuildUpon.build().toString();
        if (loVar != null) {
            StringBuilder sb = new StringBuilder(string);
            if (!TextUtils.isEmpty(loVar.f13799b)) {
                sb.append("&it=");
                sb.append(loVar.f13799b);
            }
            if (!TextUtils.isEmpty(loVar.f13800c)) {
                sb.append("&blat=");
                sb.append(loVar.f13800c);
            }
            string = sb.toString();
        }
        String str = string;
        if (!this.f10005h.get()) {
            p7.r0 r0Var = m7.n.C.f30464c;
            new p7.a0(this.f10002e, this.f10003f, str, null, null).b();
            return;
        }
        File file = this.f10006i;
        if (file == null) {
            q7.j.d("CsiReporter: File doesn't exist. Cannot write CSI data to file.");
            return;
        }
        FileOutputStream fileOutputStream2 = null;
        try {
            fileOutputStream = new FileOutputStream(file, true);
        } catch (IOException unused) {
        } catch (Throwable th) {
            th = th;
        }
        try {
            fileOutputStream.write(str.getBytes());
            fileOutputStream.write(10);
            try {
                fileOutputStream.close();
            } catch (IOException unused2) {
            }
        } catch (IOException unused3) {
            fileOutputStream2 = fileOutputStream;
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.close();
                } catch (IOException unused4) {
                }
            }
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream2 = fileOutputStream;
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.close();
                } catch (IOException unused5) {
                }
            }
            throw th;
        }
    }

    public final Map zzc(Map map, @Nullable Map map2) {
        LinkedHashMap linkedHashMap = new LinkedHashMap(map);
        for (Map.Entry entry : map2.entrySet()) {
            String str = (String) entry.getKey();
            String str2 = (String) entry.getValue();
            String str3 = (String) linkedHashMap.get(str);
            io ioVar = (io) this.f10000c.get(str);
            if (ioVar == null) {
                ioVar = io.f12578a;
            }
            linkedHashMap.put(str, ioVar.zza(str3, str2));
        }
        return linkedHashMap;
    }
}
