package androidx.media3.session;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.media3.common.BundleListRetriever;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.BundleCollectionUtil;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.session.MediaLibraryService;
import defpackage.bj4;
import defpackage.if7;
import defpackage.rq6;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class LibraryResult<V> {
    public static final int RESULT_ERROR_BAD_VALUE = -3;
    public static final int RESULT_ERROR_INVALID_STATE = -2;
    public static final int RESULT_ERROR_IO = -5;
    public static final int RESULT_ERROR_NOT_SUPPORTED = -6;
    public static final int RESULT_ERROR_PERMISSION_DENIED = -4;
    public static final int RESULT_ERROR_SESSION_AUTHENTICATION_EXPIRED = -102;
    public static final int RESULT_ERROR_SESSION_CONCURRENT_STREAM_LIMIT = -104;
    public static final int RESULT_ERROR_SESSION_DISCONNECTED = -100;
    public static final int RESULT_ERROR_SESSION_NOT_AVAILABLE_IN_REGION = -106;
    public static final int RESULT_ERROR_SESSION_PARENTAL_CONTROL_RESTRICTED = -105;
    public static final int RESULT_ERROR_SESSION_PREMIUM_ACCOUNT_REQUIRED = -103;
    public static final int RESULT_ERROR_SESSION_SETUP_REQUIRED = -108;
    public static final int RESULT_ERROR_SESSION_SKIP_LIMIT_REACHED = -107;
    public static final int RESULT_ERROR_UNKNOWN = -1;
    public static final int RESULT_INFO_SKIPPED = 1;
    public static final int RESULT_SUCCESS = 0;
    private static final int VALUE_TYPE_ERROR = 4;
    private static final int VALUE_TYPE_ITEM = 2;
    private static final int VALUE_TYPE_ITEM_LIST = 3;
    private static final int VALUE_TYPE_VOID = 1;
    public final long completionTimeMs;

    @rq6
    public final MediaLibraryService.LibraryParams params;
    public final int resultCode;

    @rq6
    @UnstableApi
    public final SessionError sessionError;

    @rq6
    public final V value;
    private final int valueType;
    private static final String FIELD_RESULT_CODE = Util.intToStringMaxRadix(0);
    private static final String FIELD_COMPLETION_TIME_MS = Util.intToStringMaxRadix(1);
    private static final String FIELD_PARAMS = Util.intToStringMaxRadix(2);
    private static final String FIELD_VALUE = Util.intToStringMaxRadix(3);
    private static final String FIELD_VALUE_TYPE = Util.intToStringMaxRadix(4);
    private static final String FIELD_SESSION_ERROR = Util.intToStringMaxRadix(5);

    @Target({ElementType.TYPE_USE})
    @Documented
    @Retention(RetentionPolicy.SOURCE)
    public @interface Code {
    }

    private LibraryResult(int i, long j, @rq6 MediaLibraryService.LibraryParams libraryParams, @rq6 SessionError sessionError, @rq6 V v, int i2) {
        this.resultCode = i;
        this.completionTimeMs = j;
        this.params = libraryParams;
        this.sessionError = sessionError;
        this.value = v;
        this.valueType = i2;
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0043  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static LibraryResult<?> fromBundle(Bundle bundle, @rq6 Integer num) {
        SessionError sessionError;
        SessionError sessionError2;
        int i;
        int i2 = bundle.getInt(FIELD_RESULT_CODE, 0);
        long j = bundle.getLong(FIELD_COMPLETION_TIME_MS, SystemClock.elapsedRealtime());
        Bundle bundle2 = bundle.getBundle(FIELD_PARAMS);
        Object objFromBundle = null;
        MediaLibraryService.LibraryParams libraryParamsFromBundle = bundle2 == null ? null : MediaLibraryService.LibraryParams.fromBundle(bundle2);
        Bundle bundle3 = bundle.getBundle(FIELD_SESSION_ERROR);
        if (bundle3 != null) {
            sessionError2 = SessionError.fromBundle(bundle3);
        } else {
            if (i2 == 0) {
                sessionError = null;
                i = bundle.getInt(FIELD_VALUE_TYPE);
                if (i != 1) {
                    if (i == 2) {
                        if7.o(num == null || num.intValue() == 2);
                        Bundle bundle4 = bundle.getBundle(FIELD_VALUE);
                        if (bundle4 != null) {
                            objFromBundle = MediaItem.fromBundle(bundle4);
                        }
                    } else if (i == 3) {
                        if7.o(num == null || num.intValue() == 3);
                        IBinder binder = bundle.getBinder(FIELD_VALUE);
                        if (binder != null) {
                            objFromBundle = BundleCollectionUtil.fromBundleList(new defpackage.av(25), BundleListRetriever.getList(binder));
                        }
                    } else if (i != 4) {
                        throw new IllegalStateException();
                    }
                }
                return new LibraryResult<>(i2, j, libraryParamsFromBundle, sessionError, objFromBundle, i);
            }
            sessionError2 = new SessionError(i2, "no error message provided");
        }
        sessionError = sessionError2;
        i = bundle.getInt(FIELD_VALUE_TYPE);
        if (i != 1) {
        }
        return new LibraryResult<>(i2, j, libraryParamsFromBundle, sessionError, objFromBundle, i);
    }

    @UnstableApi
    public static LibraryResult<MediaItem> fromItemBundle(Bundle bundle) {
        return fromBundle(bundle, 2);
    }

    @UnstableApi
    public static LibraryResult<bj4<MediaItem>> fromItemListBundle(Bundle bundle) {
        return fromBundle(bundle, 3);
    }

    @UnstableApi
    public static LibraryResult<?> fromUnknownBundle(Bundle bundle) {
        return fromBundle(bundle, null);
    }

    @UnstableApi
    public static LibraryResult<Void> fromVoidBundle(Bundle bundle) {
        return fromUnknownBundle(bundle);
    }

    @SuppressLint({"WrongConstant"})
    public static <V> LibraryResult<V> ofError(int i) {
        return ofError(new SessionError(i, "no error message provided", Bundle.EMPTY));
    }

    public static LibraryResult<MediaItem> ofItem(MediaItem mediaItem, @rq6 MediaLibraryService.LibraryParams libraryParams) {
        verifyMediaItem(mediaItem);
        return new LibraryResult<>(0, SystemClock.elapsedRealtime(), libraryParams, null, mediaItem, 2);
    }

    public static LibraryResult<bj4<MediaItem>> ofItemList(List<MediaItem> list, @rq6 MediaLibraryService.LibraryParams libraryParams) {
        Iterator<MediaItem> it = list.iterator();
        while (it.hasNext()) {
            verifyMediaItem(it.next());
        }
        return new LibraryResult<>(0, SystemClock.elapsedRealtime(), libraryParams, null, bj4.l(list), 3);
    }

    public static LibraryResult<Void> ofVoid() {
        return new LibraryResult<>(0, SystemClock.elapsedRealtime(), null, null, null, 1);
    }

    private static void verifyMediaItem(MediaItem mediaItem) {
        if7.f(!TextUtils.isEmpty(mediaItem.mediaId), "mediaId must not be empty");
        if7.f(mediaItem.mediaMetadata.isBrowsable != null, "mediaMetadata must specify isBrowsable");
        if7.f(mediaItem.mediaMetadata.isPlayable != null, "mediaMetadata must specify isPlayable");
    }

    /* JADX WARN: Code restructure failed: missing block: B:18:0x0045, code lost:
    
        if (r2 != 4) goto L19;
     */
    @UnstableApi
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        bundle.putInt(FIELD_RESULT_CODE, this.resultCode);
        bundle.putLong(FIELD_COMPLETION_TIME_MS, this.completionTimeMs);
        MediaLibraryService.LibraryParams libraryParams = this.params;
        if (libraryParams != null) {
            bundle.putBundle(FIELD_PARAMS, libraryParams.toBundle());
        }
        SessionError sessionError = this.sessionError;
        if (sessionError != null) {
            bundle.putBundle(FIELD_SESSION_ERROR, sessionError.toBundle());
        }
        bundle.putInt(FIELD_VALUE_TYPE, this.valueType);
        V v = this.value;
        if (v != null) {
            int i = this.valueType;
            if (i != 1) {
                if (i == 2) {
                    bundle.putBundle(FIELD_VALUE, ((MediaItem) v).toBundle());
                    return bundle;
                }
                if (i == 3) {
                    bundle.putBinder(FIELD_VALUE, new BundleListRetriever(BundleCollectionUtil.toBundleList((bj4) this.value, new defpackage.av(24))));
                    return bundle;
                }
            }
            throw new IllegalStateException();
        }
        return bundle;
    }

    @SuppressLint({"WrongConstant"})
    public static <V> LibraryResult<V> ofError(int i, @rq6 MediaLibraryService.LibraryParams libraryParams) {
        return new LibraryResult<>(i, SystemClock.elapsedRealtime(), libraryParams, new SessionError(i, "no error message provided", Bundle.EMPTY), null, 4);
    }

    public static LibraryResult<Void> ofVoid(@rq6 MediaLibraryService.LibraryParams libraryParams) {
        return new LibraryResult<>(0, SystemClock.elapsedRealtime(), libraryParams, null, null, 1);
    }

    @SuppressLint({"WrongConstant"})
    @UnstableApi
    public static <V> LibraryResult<V> ofError(SessionError sessionError) {
        return new LibraryResult<>(sessionError.code, SystemClock.elapsedRealtime(), null, sessionError, null, 4);
    }

    @SuppressLint({"WrongConstant"})
    @UnstableApi
    public static <V> LibraryResult<V> ofError(SessionError sessionError, MediaLibraryService.LibraryParams libraryParams) {
        return new LibraryResult<>(sessionError.code, SystemClock.elapsedRealtime(), libraryParams, sessionError, null, 4);
    }
}
