package m6;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.SystemClock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import l6.g;
import l6.h;
import n5.s;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import xyz.stream.utils.a0;

/* JADX INFO: loaded from: classes.dex */
public final class f {

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final n6.a f30416j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public SQLiteStatement f30417k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public volatile long f30418l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public volatile long f30419m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public volatile boolean f30420n;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final HashSet f30410d = new HashSet();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final HashSet f30411e = new HashSet();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final HashSet f30412f = new HashSet();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final HashSet f30413g = new HashSet();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final AtomicInteger f30414h = new AtomicInteger(0);

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public volatile AtomicInteger f30415i = new AtomicInteger(0);

    public f(d dVar, h hVar) {
        l6.e eVarWsy;
        this.f30408b = dVar;
        this.f30409c = hVar;
        if (hVar.ajl() && (eVarWsy = hVar.wsy()) != null) {
            this.f30416j = new n6.a(dVar, hVar.yz(), eVarWsy);
        }
        if (dVar.f30398b.wsy() >= hVar.fm()) {
            hVar.toString();
        }
    }

    public final int a() {
        int iZ = (int) (hb.a.Z(true) * this.f30409c.ro());
        if (iZ <= 0) {
            return 100;
        }
        return iZ;
    }

    public final void b(a aVar) {
        this.f30414h.set(0);
        int iA = a();
        int iCeil = (int) Math.ceil((f(aVar) * 1.0f) / iA);
        Objects.toString(this.f30409c);
        for (int i10 = 0; i10 < iCeil; i10++) {
            int i11 = i10 * iA;
            try {
                g gVarRo = this.f30408b.f30398b.ro();
                ExecutorService executorServiceRo = gVarRo != null ? gVarRo.ro() : null;
                if (executorServiceRo == null) {
                    executorServiceRo = o6.b.k();
                }
                executorServiceRo.submit(new e(this, aVar, i11));
            } catch (Exception unused) {
                n6.a aVar2 = this.f30416j;
                if (aVar2 != null) {
                    aVar2.a(10, 1);
                }
            }
        }
    }

    public final void c(ArrayList arrayList, boolean z10, a0 a0Var) {
        arrayList.size();
        this.f30418l = SystemClock.elapsedRealtime();
        h hVar = this.f30409c;
        s sVar = new s();
        sVar.f30881c = this;
        sVar.f30879a = z10;
        sVar.f30880b = a0Var;
        hVar.fm(arrayList, sVar);
    }

    public final void d(a aVar, int i10) {
        String str;
        String[] strArr;
        ArrayList arrayList;
        this.f30414h.set(0);
        int i11 = 1;
        Cursor cursor = null;
        try {
            SQLiteDatabase readableDatabase = aVar.getReadableDatabase();
            int iA = a();
            if (this.f30410d.isEmpty()) {
                str = null;
                strArr = null;
            } else {
                StringBuilder sb = new StringBuilder("data_id NOT IN (");
                ArrayList arrayList2 = new ArrayList();
                synchronized (this.f30410d) {
                    try {
                        for (String str2 : this.f30410d) {
                            if (!arrayList2.isEmpty()) {
                                sb.append(",");
                            }
                            sb.append("?");
                            arrayList2.add(str2);
                            if (arrayList2.size() >= iA) {
                                break;
                            }
                        }
                    } finally {
                    }
                }
                sb.append(")");
                String string = sb.toString();
                String[] strArr2 = (String[]) arrayList2.toArray(new String[0]);
                Arrays.toString(strArr2);
                str = string;
                strArr = strArr2;
            }
            Cursor cursorQuery = readableDatabase.query(this.f30409c.yz(), null, str, strArr, null, null, "priority DESC, create_time DESC", String.valueOf(iA * i10 * 2));
            if (cursorQuery != null) {
                try {
                    if (!cursorQuery.moveToFirst()) {
                        try {
                            cursorQuery.close();
                            return;
                        } catch (Exception unused) {
                            return;
                        }
                    }
                    int columnIndexOrThrow = cursorQuery.getColumnIndexOrThrow("data");
                    int columnIndexOrThrow2 = cursorQuery.getColumnIndexOrThrow("data_id");
                    int columnIndexOrThrow3 = cursorQuery.getColumnIndexOrThrow("priority");
                    int columnIndexOrThrow4 = cursorQuery.getColumnIndexOrThrow("upload_retry_count");
                    ArrayList arrayList3 = new ArrayList();
                    int i12 = 0;
                    while (true) {
                        try {
                            String string2 = cursorQuery.getString(columnIndexOrThrow2);
                            synchronized (this.f30410d) {
                                try {
                                    if (!this.f30410d.contains(string2)) {
                                        this.f30410d.add(string2);
                                        byte[] blob = cursorQuery.getBlob(columnIndexOrThrow);
                                        l6.c cVarYz = this.f30408b.f30398b.yz();
                                        if (cVarYz != null) {
                                            blob = cVarYz.ro(blob);
                                            n6.a aVar2 = this.f30416j;
                                            if (aVar2 != null) {
                                                aVar2.a(blob != null ? 7 : 8, i11);
                                            }
                                        }
                                        l6.b bVarFm = this.f30409c.fm(string2, blob, cursorQuery.getInt(columnIndexOrThrow3), cursorQuery.getInt(columnIndexOrThrow4));
                                        if (bVarFm == null) {
                                            synchronized (this.f30410d) {
                                                this.f30410d.remove(string2);
                                            }
                                        } else {
                                            arrayList3.add(bVarFm);
                                            if (arrayList3.size() >= iA) {
                                                c(arrayList3, true, null);
                                                i12++;
                                                arrayList = new ArrayList();
                                                if (i12 < i10) {
                                                    arrayList3 = arrayList;
                                                }
                                            }
                                        }
                                    }
                                } finally {
                                }
                            }
                        } catch (Exception unused2) {
                            n6.a aVar3 = this.f30416j;
                            if (aVar3 != null) {
                                aVar3.a(11, 1);
                            }
                        }
                        if (!cursorQuery.moveToNext()) {
                            arrayList = arrayList3;
                            break;
                        }
                        i11 = 1;
                    }
                    if (!arrayList.isEmpty()) {
                        c(arrayList, false, null);
                    }
                } catch (Throwable unused3) {
                    cursor = cursorQuery;
                    try {
                        n6.a aVar4 = this.f30416j;
                        if (aVar4 != null) {
                            aVar4.a(IMediaPlayer.MEDIA_INFO_OPEN_INPUT, 1);
                        }
                        if (cursor != null) {
                            try {
                                cursor.close();
                                return;
                            } catch (Exception unused4) {
                                return;
                            }
                        }
                        return;
                    } catch (Throwable th) {
                        if (cursor != null) {
                            try {
                                cursor.close();
                            } catch (Exception unused5) {
                            }
                        }
                        throw th;
                    }
                }
            }
            if (cursorQuery != null) {
                try {
                    cursorQuery.close();
                } catch (Exception unused6) {
                }
            }
        } catch (Throwable unused7) {
        }
    }

    public final void e(a aVar) {
        ArrayList arrayList;
        synchronized (this.f30413g) {
            arrayList = new ArrayList(this.f30413g);
            this.f30413g.clear();
        }
        try {
            SQLiteDatabase writableDatabase = aVar.getWritableDatabase();
            StringBuilder sb = new StringBuilder("(");
            String[] strArr = new String[arrayList.size()];
            for (int i10 = 0; i10 < arrayList.size(); i10++) {
                strArr[i10] = (String) arrayList.get(i10);
                if (i10 > 0) {
                    sb.append(",");
                }
                sb.append("?");
            }
            sb.append(")");
            writableDatabase.execSQL("UPDATE " + this.f30409c.yz() + " SET upload_retry_count = upload_retry_count+1 WHERE data_id IN " + ((Object) sb), strArr);
            arrayList.size();
        } catch (Throwable unused) {
            n6.a aVar2 = this.f30416j;
            if (aVar2 != null) {
                aVar2.a(10000, 1);
            }
        }
        synchronized (this.f30410d) {
            try {
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    this.f30410d.remove((String) it.next());
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:30:0x002d A[EXC_TOP_SPLITTER, PHI: r11
      0x002d: PHI (r11v3 android.database.Cursor) = (r11v2 android.database.Cursor), (r11v6 android.database.Cursor) binds: [B:17:0x003b, B:10:0x002b] A[DONT_GENERATE, DONT_INLINE], SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int f(a aVar) {
        Cursor cursorQuery;
        try {
            cursorQuery = aVar.getReadableDatabase().query(this.f30409c.yz(), new String[]{"count(*)"}, null, null, null, null, null);
            if (cursorQuery != null) {
                try {
                    if (cursorQuery.moveToFirst()) {
                        int i10 = cursorQuery.getInt(0);
                        try {
                            cursorQuery.close();
                        } catch (Exception unused) {
                        }
                        return i10;
                    }
                } catch (Throwable unused2) {
                    try {
                        n6.a aVar2 = this.f30416j;
                        if (aVar2 != null) {
                            aVar2.a(10007, 1);
                        }
                        if (cursorQuery != null) {
                        }
                    } catch (Throwable th) {
                        if (cursorQuery != null) {
                            try {
                                cursorQuery.close();
                            } catch (Exception unused3) {
                            }
                        }
                        throw th;
                    }
                }
            }
        } catch (Throwable unused4) {
            cursorQuery = null;
        }
        if (cursorQuery != null) {
            try {
                cursorQuery.close();
            } catch (Exception unused5) {
            }
        }
        return 0;
    }

    public final void g() {
        ArrayList arrayList = new ArrayList(this.f30407a.size());
        synchronized (this.f30407a) {
            try {
                int size = this.f30407a.size();
                for (int i10 = 0; i10 < size; i10++) {
                    arrayList.add((l6.b) this.f30407a.get(i10));
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        synchronized (this.f30410d) {
            try {
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    l6.b bVar = (l6.b) it.next();
                    if (this.f30410d.contains(bVar.ajl())) {
                        bVar.toString();
                        it.remove();
                    } else {
                        this.f30410d.add(bVar.ajl());
                    }
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        arrayList.size();
        if (arrayList.isEmpty()) {
            return;
        }
        c(arrayList, false, new a0(this));
    }

    /* JADX WARN: Removed duplicated region for block: B:58:0x0092 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void h(a aVar) {
        ArrayList arrayList;
        SQLiteDatabase writableDatabase;
        synchronized (this.f30412f) {
            arrayList = new ArrayList(this.f30412f);
            this.f30412f.clear();
        }
        try {
            writableDatabase = aVar.getWritableDatabase();
        } catch (Throwable unused) {
            writableDatabase = null;
        }
        try {
            writableDatabase.beginTransaction();
            HashSet hashSet = new HashSet();
            StringBuilder sb = new StringBuilder("(");
            String[] strArr = new String[arrayList.size()];
            for (int i10 = 0; i10 < arrayList.size(); i10++) {
                strArr[i10] = (String) arrayList.get(i10);
                if (i10 > 0) {
                    sb.append(",");
                }
                sb.append("?");
            }
            sb.append(")");
            if (writableDatabase.delete(this.f30409c.yz(), "data_id in ".concat(String.valueOf(sb)), strArr) == 0) {
                hashSet.addAll(arrayList);
            }
            writableDatabase.setTransactionSuccessful();
            arrayList.size();
            hashSet.size();
            hashSet.size();
        } catch (Throwable unused2) {
            try {
                n6.a aVar2 = this.f30416j;
                if (aVar2 != null) {
                    aVar2.a(10001, 1);
                }
                if (writableDatabase != null && writableDatabase.inTransaction()) {
                }
                synchronized (this.f30410d) {
                }
            } catch (Throwable th) {
                if (writableDatabase != null) {
                    try {
                        if (writableDatabase.inTransaction()) {
                            writableDatabase.endTransaction();
                        }
                    } catch (Exception unused3) {
                    }
                }
                throw th;
            }
        }
        if (writableDatabase.inTransaction()) {
            writableDatabase.endTransaction();
        }
        synchronized (this.f30410d) {
            try {
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    this.f30410d.remove((String) it.next());
                }
            } finally {
            }
        }
    }
}
