package defpackage;

import defpackage.am1;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.NavigableSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes2.dex */
public class dm1 {
    public static final Charset e = Charset.forName("UTF-8");
    public static final int f = 15;
    public static final cm1 g = new cm1();
    public static final ch3 h = new ch3(4);
    public static final dl1 i = new dl1(2);
    public final AtomicInteger a = new AtomicInteger(0);
    public final oy2 b;
    public final tn8 c;
    public final fl1 d;

    public dm1(oy2 oy2Var, tn8 tn8Var, fl1 fl1Var) {
        this.b = oy2Var;
        this.c = tn8Var;
        this.d = fl1Var;
    }

    public static void a(List list) {
        Iterator it = list.iterator();
        while (it.hasNext()) {
            ((File) it.next()).delete();
        }
    }

    public static String e(File file) {
        byte[] bArr = new byte[8192];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        FileInputStream fileInputStream = new FileInputStream(file);
        while (true) {
            try {
                int i2 = fileInputStream.read(bArr);
                if (i2 <= 0) {
                    String str = new String(byteArrayOutputStream.toByteArray(), e);
                    fileInputStream.close();
                    return str;
                }
                byteArrayOutputStream.write(bArr, 0, i2);
            } catch (Throwable th) {
                try {
                    fileInputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }

    public static void f(File file, String str) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), e);
        try {
            outputStreamWriter.write(str);
            outputStreamWriter.close();
        } catch (Throwable th) {
            try {
                outputStreamWriter.close();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public final ArrayList b() {
        ArrayList arrayList = new ArrayList();
        oy2 oy2Var = this.b;
        arrayList.addAll(oy2.e(oy2Var.f.listFiles()));
        arrayList.addAll(oy2.e(oy2Var.g.listFiles()));
        ch3 ch3Var = h;
        Collections.sort(arrayList, ch3Var);
        List listE = oy2.e(oy2Var.e.listFiles());
        Collections.sort(listE, ch3Var);
        arrayList.addAll(listE);
        return arrayList;
    }

    public final NavigableSet c() {
        return new TreeSet(oy2.e(this.b.d.list())).descendingSet();
    }

    public final void d(am1.f.d dVar, String str, boolean z) {
        oy2 oy2Var = this.b;
        int i2 = this.c.c().a.a;
        g.getClass();
        try {
            f(oy2Var.b(str, vw2.ab("event", String.format(Locale.US, "%010d", Integer.valueOf(this.a.getAndIncrement())), z ? "_" : "")), cm1.a.b(dVar));
        } catch (IOException e2) {
            hh5.a.b("Could not persist event for session " + str, e2);
        }
        dl1 dl1Var = new dl1(3);
        oy2Var.getClass();
        File file = new File(oy2Var.d, str);
        file.mkdirs();
        List<File> listE = oy2.e(file.listFiles(dl1Var));
        Collections.sort(listE, new ch3(5));
        int size = listE.size();
        for (File file2 : listE) {
            if (size <= i2) {
                return;
            }
            oy2.d(file2);
            size--;
        }
    }
}
