package androidx.media3.exoplayer.dash;

import F3.AbstractC0220u;
import android.os.SystemClock;
import android.util.Pair;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.dash.manifest.BaseUrl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class BaseUrlExclusionList {
    private final Map<Integer, Long> excludedPriorities;
    private final Map<String, Long> excludedServiceLocations;
    private final Random random;
    private final Map<List<Pair<String, Integer>>, BaseUrl> selectionsTaken;

    public BaseUrlExclusionList() {
        this(new Random());
    }

    private static <T> void addExclusion(T t6, long j4, Map<T, Long> map) {
        if (map.containsKey(t6)) {
            j4 = Math.max(j4, ((Long) Util.castNonNull(map.get(t6))).longValue());
        }
        map.put(t6, Long.valueOf(j4));
    }

    private List<BaseUrl> applyExclusions(List<BaseUrl> list) {
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        removeExpiredExclusions(jElapsedRealtime, this.excludedServiceLocations);
        removeExpiredExclusions(jElapsedRealtime, this.excludedPriorities);
        ArrayList arrayList = new ArrayList();
        for (int i6 = 0; i6 < list.size(); i6++) {
            BaseUrl baseUrl = list.get(i6);
            if (!this.excludedServiceLocations.containsKey(baseUrl.serviceLocation) && !this.excludedPriorities.containsKey(Integer.valueOf(baseUrl.priority))) {
                arrayList.add(baseUrl);
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static int compareBaseUrl(BaseUrl baseUrl, BaseUrl baseUrl2) {
        int iCompare = Integer.compare(baseUrl.priority, baseUrl2.priority);
        return iCompare != 0 ? iCompare : baseUrl.serviceLocation.compareTo(baseUrl2.serviceLocation);
    }

    public static int getPriorityCount(List<BaseUrl> list) {
        HashSet hashSet = new HashSet();
        for (int i6 = 0; i6 < list.size(); i6++) {
            hashSet.add(Integer.valueOf(list.get(i6).priority));
        }
        return hashSet.size();
    }

    private static <T> void removeExpiredExclusions(long j4, Map<T, Long> map) {
        ArrayList arrayList = new ArrayList();
        for (Map.Entry<T, Long> entry : map.entrySet()) {
            if (entry.getValue().longValue() <= j4) {
                arrayList.add(entry.getKey());
            }
        }
        for (int i6 = 0; i6 < arrayList.size(); i6++) {
            map.remove(arrayList.get(i6));
        }
    }

    private BaseUrl selectWeighted(List<BaseUrl> list) {
        int i6 = 0;
        for (int i7 = 0; i7 < list.size(); i7++) {
            i6 += list.get(i7).weight;
        }
        int iNextInt = this.random.nextInt(i6);
        int i8 = 0;
        for (int i9 = 0; i9 < list.size(); i9++) {
            BaseUrl baseUrl = list.get(i9);
            i8 += baseUrl.weight;
            if (iNextInt < i8) {
                return baseUrl;
            }
        }
        return (BaseUrl) AbstractC0220u.k(list);
    }

    public void exclude(BaseUrl baseUrl, long j4) {
        long jElapsedRealtime = SystemClock.elapsedRealtime() + j4;
        addExclusion(baseUrl.serviceLocation, jElapsedRealtime, this.excludedServiceLocations);
        int i6 = baseUrl.priority;
        if (i6 != Integer.MIN_VALUE) {
            addExclusion(Integer.valueOf(i6), jElapsedRealtime, this.excludedPriorities);
        }
    }

    public int getPriorityCountAfterExclusion(List<BaseUrl> list) {
        HashSet hashSet = new HashSet();
        List<BaseUrl> listApplyExclusions = applyExclusions(list);
        for (int i6 = 0; i6 < listApplyExclusions.size(); i6++) {
            hashSet.add(Integer.valueOf(listApplyExclusions.get(i6).priority));
        }
        return hashSet.size();
    }

    public void reset() {
        this.excludedServiceLocations.clear();
        this.excludedPriorities.clear();
        this.selectionsTaken.clear();
    }

    public BaseUrl selectBaseUrl(List<BaseUrl> list) {
        List<BaseUrl> listApplyExclusions = applyExclusions(list);
        if (listApplyExclusions.size() < 2) {
            return (BaseUrl) AbstractC0220u.j(listApplyExclusions, null);
        }
        Collections.sort(listApplyExclusions, new a());
        ArrayList arrayList = new ArrayList();
        int i6 = listApplyExclusions.get(0).priority;
        int i7 = 0;
        while (true) {
            if (i7 >= listApplyExclusions.size()) {
                break;
            }
            BaseUrl baseUrl = listApplyExclusions.get(i7);
            if (i6 == baseUrl.priority) {
                arrayList.add(new Pair(baseUrl.serviceLocation, Integer.valueOf(baseUrl.weight)));
                i7++;
            } else if (arrayList.size() == 1) {
                return listApplyExclusions.get(0);
            }
        }
        BaseUrl baseUrl2 = this.selectionsTaken.get(arrayList);
        if (baseUrl2 != null) {
            return baseUrl2;
        }
        BaseUrl baseUrlSelectWeighted = selectWeighted(listApplyExclusions.subList(0, arrayList.size()));
        this.selectionsTaken.put(arrayList, baseUrlSelectWeighted);
        return baseUrlSelectWeighted;
    }

    public BaseUrlExclusionList(Random random) {
        this.selectionsTaken = new HashMap();
        this.random = random;
        this.excludedServiceLocations = new HashMap();
        this.excludedPriorities = new HashMap();
    }
}
