package androidx.media3.datasource.cache;

import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.TreeSet;
import q.AbstractC2761L;

/* JADX INFO: loaded from: classes.dex */
final class CachedContent {
    private static final String TAG = "CachedContent";
    private final TreeSet<SimpleCacheSpan> cachedSpans;
    public final int id;
    public final String key;
    private final ArrayList<Range> lockedRanges;
    private DefaultContentMetadata metadata;

    public static final class Range {
        public final long length;
        public final long position;

        public Range(long j4, long j6) {
            this.position = j4;
            this.length = j6;
        }

        public boolean contains(long j4, long j6) {
            long j7 = this.length;
            if (j7 == -1) {
                return j4 >= this.position;
            }
            if (j6 == -1) {
                return false;
            }
            long j8 = this.position;
            return j8 <= j4 && j4 + j6 <= j8 + j7;
        }

        public boolean intersects(long j4, long j6) {
            long j7 = this.position;
            if (j7 > j4) {
                return j6 == -1 || j4 + j6 > j7;
            }
            long j8 = this.length;
            return j8 == -1 || j7 + j8 > j4;
        }
    }

    public CachedContent(int i6, String str) {
        this(i6, str, DefaultContentMetadata.EMPTY);
    }

    public void addSpan(SimpleCacheSpan simpleCacheSpan) {
        this.cachedSpans.add(simpleCacheSpan);
    }

    public boolean applyMetadataMutations(ContentMetadataMutations contentMetadataMutations) {
        this.metadata = this.metadata.copyWithMutationsApplied(contentMetadataMutations);
        return !r2.equals(r0);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && CachedContent.class == obj.getClass()) {
            CachedContent cachedContent = (CachedContent) obj;
            if (this.id == cachedContent.id && this.key.equals(cachedContent.key) && this.cachedSpans.equals(cachedContent.cachedSpans) && this.metadata.equals(cachedContent.metadata)) {
                return true;
            }
        }
        return false;
    }

    public long getCachedBytesLength(long j4, long j6) {
        Assertions.checkArgument(j4 >= 0);
        Assertions.checkArgument(j6 >= 0);
        SimpleCacheSpan span = getSpan(j4, j6);
        if (span.isHoleSpan()) {
            return -Math.min(span.isOpenEnded() ? Long.MAX_VALUE : span.length, j6);
        }
        long j7 = j4 + j6;
        long j8 = j7 >= 0 ? j7 : Long.MAX_VALUE;
        long jMax = span.position + span.length;
        if (jMax < j8) {
            for (SimpleCacheSpan simpleCacheSpan : this.cachedSpans.tailSet(span, false)) {
                long j9 = simpleCacheSpan.position;
                if (j9 > jMax) {
                    break;
                }
                jMax = Math.max(jMax, j9 + simpleCacheSpan.length);
                if (jMax >= j8) {
                    break;
                }
            }
        }
        return Math.min(jMax - j4, j6);
    }

    public DefaultContentMetadata getMetadata() {
        return this.metadata;
    }

    public SimpleCacheSpan getSpan(long j4, long j6) {
        SimpleCacheSpan simpleCacheSpanCreateLookup = SimpleCacheSpan.createLookup(this.key, j4);
        SimpleCacheSpan simpleCacheSpanFloor = this.cachedSpans.floor(simpleCacheSpanCreateLookup);
        if (simpleCacheSpanFloor != null && simpleCacheSpanFloor.position + simpleCacheSpanFloor.length > j4) {
            return simpleCacheSpanFloor;
        }
        SimpleCacheSpan simpleCacheSpanCeiling = this.cachedSpans.ceiling(simpleCacheSpanCreateLookup);
        if (simpleCacheSpanCeiling != null) {
            long j7 = simpleCacheSpanCeiling.position - j4;
            j6 = j6 == -1 ? j7 : Math.min(j7, j6);
        }
        return SimpleCacheSpan.createHole(this.key, j4, j6);
    }

    public TreeSet<SimpleCacheSpan> getSpans() {
        return this.cachedSpans;
    }

    public int hashCode() {
        return this.metadata.hashCode() + AbstractC2761L.b(this.id * 31, 31, this.key);
    }

    public boolean isEmpty() {
        return this.cachedSpans.isEmpty();
    }

    public boolean isFullyLocked(long j4, long j6) {
        for (int i6 = 0; i6 < this.lockedRanges.size(); i6++) {
            if (this.lockedRanges.get(i6).contains(j4, j6)) {
                return true;
            }
        }
        return false;
    }

    public boolean isFullyUnlocked() {
        return this.lockedRanges.isEmpty();
    }

    public boolean lockRange(long j4, long j6) {
        for (int i6 = 0; i6 < this.lockedRanges.size(); i6++) {
            if (this.lockedRanges.get(i6).intersects(j4, j6)) {
                return false;
            }
        }
        this.lockedRanges.add(new Range(j4, j6));
        return true;
    }

    public boolean removeSpan(CacheSpan cacheSpan) {
        if (!this.cachedSpans.remove(cacheSpan)) {
            return false;
        }
        File file = cacheSpan.file;
        if (file == null) {
            return true;
        }
        file.delete();
        return true;
    }

    public SimpleCacheSpan setLastTouchTimestamp(SimpleCacheSpan simpleCacheSpan, long j4, boolean z6) {
        long j6;
        Assertions.checkState(this.cachedSpans.remove(simpleCacheSpan));
        File file = (File) Assertions.checkNotNull(simpleCacheSpan.file);
        if (z6) {
            j6 = j4;
            File cacheFile = SimpleCacheSpan.getCacheFile((File) Assertions.checkNotNull(file.getParentFile()), this.id, simpleCacheSpan.position, j6);
            if (file.renameTo(cacheFile)) {
                file = cacheFile;
            } else {
                Log.w(TAG, "Failed to rename " + file + " to " + cacheFile);
            }
        } else {
            j6 = j4;
        }
        SimpleCacheSpan simpleCacheSpanCopyWithFileAndLastTouchTimestamp = simpleCacheSpan.copyWithFileAndLastTouchTimestamp(file, j6);
        this.cachedSpans.add(simpleCacheSpanCopyWithFileAndLastTouchTimestamp);
        return simpleCacheSpanCopyWithFileAndLastTouchTimestamp;
    }

    public void unlockRange(long j4) {
        for (int i6 = 0; i6 < this.lockedRanges.size(); i6++) {
            if (this.lockedRanges.get(i6).position == j4) {
                this.lockedRanges.remove(i6);
                return;
            }
        }
        throw new IllegalStateException();
    }

    public CachedContent(int i6, String str, DefaultContentMetadata defaultContentMetadata) {
        this.id = i6;
        this.key = str;
        this.metadata = defaultContentMetadata;
        this.cachedSpans = new TreeSet<>();
        this.lockedRanges = new ArrayList<>();
    }
}
