package androidx.media3.exoplayer.ima;

import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import com.google.ads.interactivemedia.v3.api.ImaSdkFactory;
import com.google.ads.interactivemedia.v3.api.StreamRequest;
import com.google.android.gms.measurement.internal.u1;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.v1;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.HashMap;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class ImaServerSideAdInsertionUriBuilder {
    private static final String ADS_ID = "adsId";
    private static final String AD_TAG_PARAMETERS = "adTagParameters";
    private static final String API_KEY = "apiKey";
    private static final String ASSET_KEY = "assetKey";
    private static final String AUTH_TOKEN = "authToken";
    private static final String CONTENT_SOURCE_ID = "contentSourceId";
    private static final String CONTENT_URL = "contentUrl";
    public static final int DEFAULT_LOAD_VIDEO_TIMEOUT_MS = 10000;
    private static final String FORMAT = "format";
    static final String IMA_AUTHORITY = "dai.google.com";
    private static final String LOAD_VIDEO_TIMEOUT_MS = "loadVideoTimeoutMs";
    private static final String MANIFEST_SUFFIX = "manifestSuffix";
    private static final String STREAM_ACTIVITY_MONITOR_ID = "streamActivityMonitorId";
    private static final String VIDEO_ID = "videoId";

    @Nullable
    private String adsId;

    @Nullable
    private String apiKey;

    @Nullable
    private String assetKey;

    @Nullable
    private String authToken;

    @Nullable
    private String contentSourceId;

    @Nullable
    private String contentUrl;

    @Nullable
    private String manifestSuffix;

    @Nullable
    private String streamActivityMonitorId;

    @Nullable
    private String videoId;
    private ImmutableMap<String, String> adTagParameters = ImmutableMap.g();
    private int loadVideoTimeoutMs = 10000;
    public int format = 4;

    public static StreamRequest createStreamRequest(Uri uri) {
        if (!C.SSAI_SCHEME.equals(uri.getScheme()) || !IMA_AUTHORITY.equals(uri.getAuthority())) {
            throw new IllegalArgumentException("Invalid URI scheme or authority.");
        }
        String queryParameter = uri.getQueryParameter(ASSET_KEY);
        String queryParameter2 = uri.getQueryParameter(API_KEY);
        StreamRequest streamRequestCreateLiveStreamRequest = !TextUtils.isEmpty(queryParameter) ? ImaSdkFactory.getInstance().createLiveStreamRequest(queryParameter, queryParameter2) : ImaSdkFactory.getInstance().createVodStreamRequest((String) Assertions.checkNotNull(uri.getQueryParameter(CONTENT_SOURCE_ID)), (String) Assertions.checkNotNull(uri.getQueryParameter(VIDEO_ID)), queryParameter2);
        int i10 = Integer.parseInt(uri.getQueryParameter("format"));
        if (i10 == 0) {
            streamRequestCreateLiveStreamRequest.setFormat(StreamRequest.StreamFormat.DASH);
        } else {
            if (i10 != 2) {
                throw new IllegalArgumentException(u1.g("Unsupported stream format:", i10));
            }
            streamRequestCreateLiveStreamRequest.setFormat(StreamRequest.StreamFormat.HLS);
        }
        String queryParameter3 = uri.getQueryParameter(AD_TAG_PARAMETERS);
        if (!TextUtils.isEmpty(queryParameter3)) {
            HashMap map = new HashMap();
            Uri uri2 = Uri.parse(queryParameter3);
            for (String str : uri2.getQueryParameterNames()) {
                String queryParameter4 = uri2.getQueryParameter(str);
                if (!TextUtils.isEmpty(queryParameter4)) {
                    map.put(str, queryParameter4);
                }
            }
            streamRequestCreateLiveStreamRequest.setAdTagParameters(map);
        }
        String queryParameter5 = uri.getQueryParameter(MANIFEST_SUFFIX);
        if (queryParameter5 != null) {
            streamRequestCreateLiveStreamRequest.setManifestSuffix(queryParameter5);
        }
        String queryParameter6 = uri.getQueryParameter(CONTENT_URL);
        if (queryParameter6 != null) {
            streamRequestCreateLiveStreamRequest.setContentUrl(queryParameter6);
        }
        String queryParameter7 = uri.getQueryParameter(AUTH_TOKEN);
        if (queryParameter7 != null) {
            streamRequestCreateLiveStreamRequest.setAuthToken(queryParameter7);
        }
        String queryParameter8 = uri.getQueryParameter(STREAM_ACTIVITY_MONITOR_ID);
        if (queryParameter8 != null) {
            streamRequestCreateLiveStreamRequest.setStreamActivityMonitorId(queryParameter8);
        }
        return streamRequestCreateLiveStreamRequest;
    }

    public static String getAdsId(Uri uri) {
        return (String) Assertions.checkNotNull(uri.getQueryParameter(ADS_ID));
    }

    public static int getLoadVideoTimeoutMs(Uri uri) {
        String queryParameter = uri.getQueryParameter(LOAD_VIDEO_TIMEOUT_MS);
        if (TextUtils.isEmpty(queryParameter)) {
            return 10000;
        }
        return Integer.parseInt(queryParameter);
    }

    public static boolean isLiveStream(Uri uri) {
        return !TextUtils.isEmpty(uri.getQueryParameter(ASSET_KEY));
    }

    public Uri build() {
        Assertions.checkState(!(!TextUtils.isEmpty(this.assetKey) || TextUtils.isEmpty(this.contentSourceId) || TextUtils.isEmpty(this.videoId)) || (!TextUtils.isEmpty(this.assetKey) && TextUtils.isEmpty(this.contentSourceId) && TextUtils.isEmpty(this.videoId)));
        Assertions.checkState(this.format != 4);
        String str = this.adsId;
        if (str == null && (str = this.assetKey) == null) {
            str = (String) Assertions.checkNotNull(this.videoId);
        }
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(C.SSAI_SCHEME);
        builder.authority(IMA_AUTHORITY);
        builder.appendQueryParameter(ADS_ID, str);
        int i10 = this.loadVideoTimeoutMs;
        if (i10 != 10000) {
            builder.appendQueryParameter(LOAD_VIDEO_TIMEOUT_MS, String.valueOf(i10));
        }
        String str2 = this.assetKey;
        if (str2 != null) {
            builder.appendQueryParameter(ASSET_KEY, str2);
        }
        String str3 = this.apiKey;
        if (str3 != null) {
            builder.appendQueryParameter(API_KEY, str3);
        }
        String str4 = this.contentSourceId;
        if (str4 != null) {
            builder.appendQueryParameter(CONTENT_SOURCE_ID, str4);
        }
        String str5 = this.videoId;
        if (str5 != null) {
            builder.appendQueryParameter(VIDEO_ID, str5);
        }
        String str6 = this.manifestSuffix;
        if (str6 != null) {
            builder.appendQueryParameter(MANIFEST_SUFFIX, str6);
        }
        String str7 = this.contentUrl;
        if (str7 != null) {
            builder.appendQueryParameter(CONTENT_URL, str7);
        }
        String str8 = this.authToken;
        if (str8 != null) {
            builder.appendQueryParameter(AUTH_TOKEN, str8);
        }
        String str9 = this.streamActivityMonitorId;
        if (str9 != null) {
            builder.appendQueryParameter(STREAM_ACTIVITY_MONITOR_ID, str9);
        }
        if (!this.adTagParameters.isEmpty()) {
            Uri.Builder builder2 = new Uri.Builder();
            v1 it = this.adTagParameters.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                builder2.appendQueryParameter((String) entry.getKey(), (String) entry.getValue());
            }
            builder.appendQueryParameter(AD_TAG_PARAMETERS, builder2.build().toString());
        }
        builder.appendQueryParameter("format", String.valueOf(this.format));
        return builder.build();
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setAdTagParameters(Map<String, String> map) {
        this.adTagParameters = ImmutableMap.b(map);
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setAdsId(String str) {
        this.adsId = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setApiKey(@Nullable String str) {
        this.apiKey = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setAssetKey(@Nullable String str) {
        this.assetKey = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setAuthToken(@Nullable String str) {
        this.authToken = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setContentSourceId(@Nullable String str) {
        this.contentSourceId = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setContentUrl(@Nullable String str) {
        this.contentUrl = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setFormat(int i10) {
        Assertions.checkArgument(i10 == 0 || i10 == 2);
        this.format = i10;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setLoadVideoTimeoutMs(int i10) {
        this.loadVideoTimeoutMs = i10;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setManifestSuffix(@Nullable String str) {
        this.manifestSuffix = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setStreamActivityMonitorId(@Nullable String str) {
        this.streamActivityMonitorId = str;
        return this;
    }

    @CanIgnoreReturnValue
    public ImaServerSideAdInsertionUriBuilder setVideoId(@Nullable String str) {
        this.videoId = str;
        return this;
    }
}
