package com.ss.android.socialbase.downloader.model;

import X.C3561QXu;
import X.C3623Qa4;
import X.QZ8;
import X.QZI;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import com.bytedance.covode.number.Covode;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class DownloadChunk implements Parcelable {
    public static final Parcelable.Creator<DownloadChunk> CREATOR = new Parcelable.Creator<DownloadChunk>() {
        static {
            Covode.recordClassIndex(49204);
        }

        public final /* bridge */ /* synthetic */ Object[] newArray(int i) {
            return new DownloadChunk[i];
        }

        public final /* synthetic */ Object createFromParcel(Parcel parcel) {
            return new DownloadChunk(parcel);
        }
    };
    public static final String LJIIJJI = DownloadChunk.class.getSimpleName();
    public int LIZ;
    public long LIZIZ;
    public long LIZJ;
    public long LIZLLL;
    public int LJ;
    public long LJFF;
    public List<DownloadChunk> LJI;
    public DownloadChunk LJII;
    public int LJIIIIZZ;
    public boolean LJIIIZ;
    public AtomicBoolean LJIIJ;
    public AtomicLong LJIIL;
    public AtomicInteger LJIILIIL;
    public QZI LJIILJJIL;

    public int describeContents() {
        return 0;
    }

    public final boolean LIZIZ() {
        if (LIZ() == -1) {
            return true;
        }
        return false;
    }

    public final boolean LIZLLL() {
        List<DownloadChunk> list = this.LJI;
        if (list == null || list.size() <= 0) {
            return false;
        }
        return true;
    }

    public final long LJII() {
        AtomicLong atomicLong = this.LJIIL;
        if (atomicLong != null) {
            return atomicLong.get();
        }
        return 0;
    }

    static {
        Covode.recordClassIndex(49203);
    }

    public final int LIZ() {
        AtomicInteger atomicInteger = this.LJIILIIL;
        if (atomicInteger == null) {
            return -1;
        }
        return atomicInteger.get();
    }

    public final DownloadChunk LIZJ() {
        DownloadChunk downloadChunk;
        if (!LIZIZ()) {
            downloadChunk = this.LJII;
            if (downloadChunk == null) {
                return null;
            }
        } else {
            downloadChunk = this;
        }
        if (!downloadChunk.LIZLLL()) {
            return null;
        }
        return downloadChunk.LJI.get(0);
    }

    public final boolean LJFF() {
        long j = this.LIZIZ;
        if (LIZIZ()) {
            long j2 = this.LJFF;
            if (j2 > this.LIZIZ) {
                j = j2;
            }
        }
        if (LJIIIIZZ() - j >= this.LIZLLL) {
            return true;
        }
        return false;
    }

    public final boolean LJ() {
        DownloadChunk downloadChunk = this.LJII;
        if (downloadChunk == null) {
            return true;
        }
        if (!downloadChunk.LIZLLL()) {
            return false;
        }
        for (int i = 0; i < this.LJII.LJI.size(); i++) {
            DownloadChunk downloadChunk2 = this.LJII.LJI.get(i);
            if (downloadChunk2 != null) {
                int indexOf = this.LJII.LJI.indexOf(this);
                if (indexOf > i && !downloadChunk2.LJFF()) {
                    return false;
                }
                if (indexOf == i) {
                    return true;
                }
            }
        }
        return false;
    }

    public final long LJI() {
        List<DownloadChunk> list;
        DownloadChunk downloadChunk = this.LJII;
        if (!(downloadChunk == null || (list = downloadChunk.LJI) == null)) {
            int indexOf = list.indexOf(this);
            boolean z = false;
            for (int i = 0; i < this.LJII.LJI.size(); i++) {
                DownloadChunk downloadChunk2 = this.LJII.LJI.get(i);
                if (downloadChunk2 != null) {
                    if (z) {
                        return downloadChunk2.LJIIIIZZ();
                    }
                    if (indexOf == i) {
                        z = true;
                    }
                }
            }
        }
        return -1;
    }

    public final long LJIIIZ() {
        long LJIIIIZZ2 = LJIIIIZZ() - this.LIZIZ;
        if (LIZLLL()) {
            LJIIIIZZ2 = 0;
            for (int i = 0; i < this.LJI.size(); i++) {
                DownloadChunk downloadChunk = this.LJI.get(i);
                if (downloadChunk != null) {
                    LJIIIIZZ2 += downloadChunk.LJIIIIZZ() - downloadChunk.LIZIZ;
                }
            }
        }
        return LJIIIIZZ2;
    }

    public final long LJIIIIZZ() {
        if (!LIZIZ() || !LIZLLL()) {
            return LJII();
        }
        long j = 0;
        for (int i = 0; i < this.LJI.size(); i++) {
            DownloadChunk downloadChunk = this.LJI.get(i);
            if (downloadChunk != null) {
                if (!downloadChunk.LJFF()) {
                    return downloadChunk.LJII();
                }
                if (j < downloadChunk.LJII()) {
                    j = downloadChunk.LJII();
                }
            }
        }
        return j;
    }

    private void LIZ(int i) {
        AtomicInteger atomicInteger = this.LJIILIIL;
        if (atomicInteger == null) {
            this.LJIILIIL = new AtomicInteger(i);
        } else {
            atomicInteger.set(i);
        }
    }

    public DownloadChunk(C3623Qa4 qa4) {
        if (qa4 != null) {
            this.LIZ = qa4.LIZ;
            this.LIZIZ = qa4.LIZIZ;
            this.LJIIL = new AtomicLong(qa4.LIZJ);
            this.LIZJ = qa4.LIZLLL;
            this.LIZLLL = qa4.LJ;
            this.LJ = qa4.LJFF;
            this.LJFF = qa4.LJI;
            this.LJIILIIL = new AtomicInteger(-1);
            LIZ(qa4.LJII);
            this.LJIIJ = new AtomicBoolean(false);
        }
    }

    public final void LIZ(long j) {
        AtomicLong atomicLong = this.LJIIL;
        if (atomicLong != null) {
            atomicLong.set(j);
        } else {
            this.LJIIL = new AtomicLong(j);
        }
    }

    public DownloadChunk(Parcel parcel) {
        this.LIZ = parcel.readInt();
        this.LIZIZ = parcel.readLong();
        this.LJIIL = new AtomicLong(parcel.readLong());
        this.LIZJ = parcel.readLong();
        this.LIZLLL = parcel.readLong();
        this.LJ = parcel.readInt();
        this.LJIILIIL = new AtomicInteger(parcel.readInt());
    }

    public final void LIZ(QZI qzi) {
        this.LJIILJJIL = qzi;
        this.LJFF = LJIIIIZZ();
    }

    public final long LIZIZ(boolean z) {
        long LJIIIIZZ2 = LJIIIIZZ();
        long j = this.LIZLLL;
        long j2 = this.LJFF;
        long j3 = j - (LJIIIIZZ2 - j2);
        if (!z && LJIIIIZZ2 == j2) {
            j3 = j - (LJIIIIZZ2 - this.LIZIZ);
        }
        C3561QXu.LIZIZ("DownloadChunk", "contentLength:" + this.LIZLLL + " curOffset:" + LJIIIIZZ() + " oldOffset:" + this.LJFF + " retainLen:" + j3);
        if (j3 < 0) {
            return 0;
        }
        return j3;
    }

    public DownloadChunk(Cursor cursor) {
        if (cursor != null) {
            this.LIZ = cursor.getInt(cursor.getColumnIndex("_id"));
            this.LJ = cursor.getInt(cursor.getColumnIndex("chunkIndex"));
            this.LIZIZ = cursor.getLong(cursor.getColumnIndex("startOffset"));
            int columnIndex = cursor.getColumnIndex("curOffset");
            if (columnIndex != -1) {
                this.LJIIL = new AtomicLong(cursor.getLong(columnIndex));
            } else {
                this.LJIIL = new AtomicLong(0);
            }
            this.LIZJ = cursor.getLong(cursor.getColumnIndex("endOffset"));
            int columnIndex2 = cursor.getColumnIndex("hostChunkIndex");
            if (columnIndex2 != -1) {
                this.LJIILIIL = new AtomicInteger(cursor.getInt(columnIndex2));
            } else {
                this.LJIILIIL = new AtomicInteger(-1);
            }
            int columnIndex3 = cursor.getColumnIndex("chunkContentLen");
            if (columnIndex3 != -1) {
                this.LIZLLL = cursor.getLong(columnIndex3);
            }
            this.LJIIJ = new AtomicBoolean(false);
        }
    }

    public final void LIZ(DownloadChunk downloadChunk) {
        this.LJII = downloadChunk;
        if (downloadChunk != null) {
            LIZ(downloadChunk.LJ);
        }
    }

    public final void LIZ(boolean z) {
        AtomicBoolean atomicBoolean = this.LJIIJ;
        if (atomicBoolean == null) {
            this.LJIIJ = new AtomicBoolean(z);
        } else {
            atomicBoolean.set(z);
        }
        this.LJIILJJIL = null;
    }

    public /* synthetic */ DownloadChunk(C3623Qa4 qa4, byte b) {
        this(qa4);
    }

    public void writeToParcel(Parcel parcel, int i) {
        long j;
        parcel.writeInt(this.LIZ);
        parcel.writeLong(this.LIZIZ);
        AtomicLong atomicLong = this.LJIIL;
        if (atomicLong != null) {
            j = atomicLong.get();
        } else {
            j = 0;
        }
        parcel.writeLong(j);
        parcel.writeLong(this.LIZJ);
        parcel.writeLong(this.LIZLLL);
        parcel.writeInt(this.LJ);
        int i2 = -1;
        AtomicInteger atomicInteger = this.LJIILIIL;
        if (atomicInteger != null) {
            i2 = atomicInteger.get();
        }
        parcel.writeInt(i2);
    }

    public final List<DownloadChunk> LIZ(int i, long j) {
        long j2;
        long j3;
        long j4;
        long j5;
        long j6 = j;
        DownloadChunk downloadChunk = this;
        if (!LIZIZ() || LIZLLL()) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        long LJII2 = LJII();
        int i2 = 1;
        long LIZIZ2 = downloadChunk.LIZIZ(true);
        int i3 = i;
        long j7 = LIZIZ2 / ((long) i3);
        C3561QXu.LIZIZ(LJIIJJI, "retainLen:" + LIZIZ2 + " divideChunkForReuse chunkSize:" + j7 + " current host downloadChunk index:" + downloadChunk.LJ);
        int i4 = 0;
        while (i4 < i3) {
            if (i4 == 0) {
                j3 = downloadChunk.LIZIZ;
                i4 = 0;
            } else {
                int i5 = i3 - 1;
                if (i4 == i5) {
                    j4 = downloadChunk.LIZJ;
                    if (j4 > LJII2) {
                        j5 = (j4 - LJII2) + 1;
                    } else {
                        j5 = LIZIZ2 - (((long) i5) * j7);
                    }
                    j3 = LJII2;
                    C3623Qa4 qa4 = new C3623Qa4(downloadChunk.LIZ);
                    qa4.LJFF = (-i4) - i2;
                    qa4.LIZIZ = j3;
                    qa4.LIZJ = LJII2;
                    qa4.LJI = LJII2;
                    qa4.LIZLLL = j4;
                    qa4.LJ = j5;
                    qa4.LJII = downloadChunk;
                    DownloadChunk LIZ2 = qa4.LIZ();
                    String str = LJIIJJI;
                    StringBuilder sb = new StringBuilder("divide sub chunk : ");
                    sb.append(i4);
                    sb.append(" startOffset:");
                    sb.append(j3);
                    sb.append(" curOffset:");
                    sb.append(LJII2);
                    sb.append(" endOffset:");
                    sb.append(j4);
                    sb.append(" contentLen:");
                    sb.append(j5);
                    C3561QXu.LIZIZ(str, sb.toString());
                    arrayList.add(LIZ2);
                    LJII2 += j7;
                    i4++;
                    i2 = 1;
                    downloadChunk = this;
                } else {
                    j3 = LJII2;
                }
            }
            j4 = (LJII2 + j7) - 1;
            j5 = j7;
            C3623Qa4 qa42 = new C3623Qa4(downloadChunk.LIZ);
            qa42.LJFF = (-i4) - i2;
            qa42.LIZIZ = j3;
            qa42.LIZJ = LJII2;
            qa42.LJI = LJII2;
            qa42.LIZLLL = j4;
            qa42.LJ = j5;
            qa42.LJII = downloadChunk;
            DownloadChunk LIZ22 = qa42.LIZ();
            String str2 = LJIIJJI;
            StringBuilder sb2 = new StringBuilder("divide sub chunk : ");
            sb2.append(i4);
            sb2.append(" startOffset:");
            sb2.append(j3);
            sb2.append(" curOffset:");
            sb2.append(LJII2);
            sb2.append(" endOffset:");
            sb2.append(j4);
            sb2.append(" contentLen:");
            sb2.append(j5);
            C3561QXu.LIZIZ(str2, sb2.toString());
            arrayList.add(LIZ22);
            LJII2 += j7;
            i4++;
            i2 = 1;
            downloadChunk = this;
        }
        long j8 = 0;
        for (int size = arrayList.size() - 1; size > 0; size--) {
            DownloadChunk downloadChunk2 = (DownloadChunk) arrayList.get(size);
            if (downloadChunk2 != null) {
                j8 += downloadChunk2.LIZLLL;
            }
        }
        C3561QXu.LIZIZ(LJIIJJI, "reuseChunkContentLen:".concat(String.valueOf(j8)));
        DownloadChunk downloadChunk3 = (DownloadChunk) arrayList.get(0);
        if (downloadChunk3 != null) {
            long j9 = this.LIZJ;
            if (j9 <= 0) {
                j2 = j6 - this.LIZIZ;
            } else {
                j2 = (j9 - this.LIZIZ) + 1;
            }
            downloadChunk3.LIZLLL = j2 - j8;
            downloadChunk3.LJ = this.LJ;
            QZI qzi = this.LJIILJJIL;
            if (qzi != null) {
                long j10 = downloadChunk3.LIZJ;
                long j11 = this.LIZLLL - j8;
                if (qzi.LIZ != null) {
                    QZ8 qz8 = qzi.LIZ;
                    qz8.LJ = j10;
                    qz8.LJFF = j11;
                }
            }
        }
        this.LJI = arrayList;
        return arrayList;
    }
}
