package com.strmr.ps.ui.player;

import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.repository.PlaybackProgressRepository;
import com.strmr.ps.data.repository.TraktScrobbleRepository;
import com.strmr.ps.data.repository.TraktSyncRepository;
import defpackage.bk1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.gj1;
import defpackage.kp6;
import defpackage.m12;
import defpackage.ng1;
import defpackage.q38;
import defpackage.rx4;
import defpackage.s38;
import defpackage.sc2;
import defpackage.sm5;
import defpackage.wj0;
import defpackage.zd9;
import javax.inject.Inject;
import javax.inject.Singleton;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;

/* JADX INFO: loaded from: classes3.dex */
@Singleton
@Metadata(d1 = {"\u0000\u001c\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0007\u0018\u00002\u00020\u0001:\u0001\nB!\b\u0007\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004\u0012\u0006\u0010\u0007\u001a\u00020\u0006¢\u0006\u0004\b\b\u0010\t¨\u0006\u000b"}, d2 = {"Lcom/strmr/ps/ui/player/ExternalPlayerTracker;", "", "Lcom/strmr/ps/data/repository/PlaybackProgressRepository;", "playbackProgressRepository", "Lcom/strmr/ps/data/repository/TraktScrobbleRepository;", "traktScrobbleRepository", "Lcom/strmr/ps/data/repository/TraktSyncRepository;", "traktSyncRepository", "<init>", "(Lcom/strmr/ps/data/repository/PlaybackProgressRepository;Lcom/strmr/ps/data/repository/TraktScrobbleRepository;Lcom/strmr/ps/data/repository/TraktSyncRepository;)V", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
public final class ExternalPlayerTracker {
    public final PlaybackProgressRepository a;
    public final TraktScrobbleRepository b;
    public final TraktSyncRepository c;
    public final ng1 d;
    public boolean e;
    public long f;
    public ContentItem g;
    public long h;
    public Integer i;
    public Integer j;
    public long k;
    public String l;

    @Metadata(d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0004\n\u0002\u0010\u0007\n\u0002\b\u0004\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0007R\u0014\u0010\b\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\b\u0010\u0007R\u0014\u0010\t\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\t\u0010\u0007R\u0014\u0010\u000b\u001a\u00020\n8\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u000b\u0010\fR\u0014\u0010\r\u001a\u00020\n8\u0002X\u0082T¢\u0006\u0006\n\u0004\b\r\u0010\f¨\u0006\u000e"}, d2 = {"Lcom/strmr/ps/ui/player/ExternalPlayerTracker$a;", "", "", "TAG", "Ljava/lang/String;", "", "MINIMUM_WATCH_TIME_MS", "J", "DEFAULT_MOVIE_DURATION_MS", "DEFAULT_EPISODE_DURATION_MS", "", "WATCHED_THRESHOLD", "F", "CONTINUE_WATCHING_THRESHOLD", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public /* synthetic */ class b {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[ContentItem.ItemType.values().length];
            try {
                iArr[ContentItem.ItemType.MOVIE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[ContentItem.ItemType.TV_SHOW.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            a = iArr;
        }
    }

    @Inject
    public ExternalPlayerTracker(@kp6 PlaybackProgressRepository playbackProgressRepository, @kp6 TraktScrobbleRepository traktScrobbleRepository, @kp6 TraktSyncRepository traktSyncRepository) {
        cr4.g(playbackProgressRepository, "playbackProgressRepository");
        cr4.g(traktScrobbleRepository, "traktScrobbleRepository");
        cr4.g(traktSyncRepository, "traktSyncRepository");
        this.a = playbackProgressRepository;
        this.b = traktScrobbleRepository;
        this.c = traktSyncRepository;
        gj1.b bVarB = zd9.b();
        m12 m12Var = sc2.a;
        this.d = bk1.a(gj1.b.a.c((rx4) bVarB, sm5.a));
    }

    /* JADX WARN: Code restructure failed: missing block: B:37:0x008e, code lost:
    
        if (r9 == r2) goto L38;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object a(ExternalPlayerTracker externalPlayerTracker, ContentItem contentItem, ContinuationImpl continuationImpl) {
        c cVar;
        boolean zBooleanValue;
        TraktSyncRepository traktSyncRepository = externalPlayerTracker.c;
        if (continuationImpl instanceof c) {
            cVar = (c) continuationImpl;
            int i = cVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                cVar.d = i - Integer.MIN_VALUE;
            } else {
                cVar = new c(externalPlayerTracker, continuationImpl);
            }
        }
        Object objMarkMovieWatched = cVar.b;
        ck1 ck1Var = ck1.a;
        int i2 = cVar.d;
        try {
            if (i2 == 0) {
                s38.b(objMarkMovieWatched);
                int i3 = b.a[contentItem.getItemType().ordinal()];
                if (i3 != 1) {
                    zBooleanValue = false;
                    if (i3 == 2) {
                        Integer num = externalPlayerTracker.i;
                        Integer num2 = externalPlayerTracker.j;
                        if (num == null || num2 == null) {
                            Log.w("ExternalPlayerTracker", "Cannot mark TV show watched without season/episode");
                        } else {
                            int tmdbId = contentItem.getTmdbId();
                            int iIntValue = num.intValue();
                            int iIntValue2 = num2.intValue();
                            cVar.a = contentItem;
                            cVar.d = 2;
                            objMarkMovieWatched = traktSyncRepository.markEpisodeWatched(tmdbId, iIntValue, iIntValue2, cVar);
                            if (objMarkMovieWatched == ck1Var) {
                            }
                            zBooleanValue = ((Boolean) objMarkMovieWatched).booleanValue();
                        }
                    }
                } else {
                    int tmdbId2 = contentItem.getTmdbId();
                    cVar.a = contentItem;
                    cVar.d = 1;
                    objMarkMovieWatched = traktSyncRepository.markMovieWatched(tmdbId2, cVar);
                }
                return ck1Var;
            }
            if (i2 == 1) {
                contentItem = cVar.a;
                s38.b(objMarkMovieWatched);
                zBooleanValue = ((Boolean) objMarkMovieWatched).booleanValue();
            } else {
                if (i2 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                contentItem = cVar.a;
                s38.b(objMarkMovieWatched);
                zBooleanValue = ((Boolean) objMarkMovieWatched).booleanValue();
            }
            if (zBooleanValue) {
                contentItem.getTitle();
            }
        } catch (Exception e) {
            Log.e("ExternalPlayerTracker", "Failed to mark as watched on Trakt", e);
        }
        return e6a.a;
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0014  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object b(ExternalPlayerTracker externalPlayerTracker, long j, ContinuationImpl continuationImpl) {
        f fVar;
        if (continuationImpl instanceof f) {
            fVar = (f) continuationImpl;
            int i = fVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                fVar.c = i - Integer.MIN_VALUE;
            } else {
                fVar = new f(externalPlayerTracker, continuationImpl);
            }
        }
        f fVar2 = fVar;
        Object obj = fVar2.a;
        ck1 ck1Var = ck1.a;
        int i2 = fVar2.c;
        e6a e6aVar = e6a.a;
        try {
            if (i2 != 0) {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(obj);
                Object obj2 = ((q38) obj).a;
                return e6aVar;
            }
            s38.b(obj);
            ContentItem contentItem = externalPlayerTracker.g;
            if (contentItem != null) {
                TraktScrobbleRepository traktScrobbleRepository = externalPlayerTracker.b;
                long j2 = externalPlayerTracker.h;
                Integer num = externalPlayerTracker.i;
                Integer num2 = externalPlayerTracker.j;
                fVar2.c = 1;
                if (traktScrobbleRepository.m156stopWatchinghUnOzRk(contentItem, j, j2, num, num2, fVar2) == ck1Var) {
                    return ck1Var;
                }
            }
            return e6aVar;
        } catch (Exception e) {
            Boxing.boxInt(Log.e("ExternalPlayerTracker", "Failed to stop scrobble with progress", e));
            return e6aVar;
        }
    }

    public final void c() {
        if (this.e) {
            wj0.c(this.d, null, null, new com.strmr.ps.ui.player.b(this, null), 3);
            f();
        }
    }

    public final void d(ContentItem contentItem, Integer num, Integer num2, Integer num3, long j, String str) {
        cr4.g(contentItem, "item");
        contentItem.getTitle();
        this.f = System.currentTimeMillis();
        this.g = contentItem;
        this.i = num2;
        this.j = num3;
        this.k = j;
        if (str == null) {
            str = contentItem.getTitle();
        }
        this.l = str;
        this.e = true;
        this.h = (num == null || num.intValue() <= 0) ? contentItem.getItemType() == ContentItem.ItemType.TV_SHOW ? 2700000L : 5400000L : ((long) (num.intValue() * 60)) * 1000;
        wj0.c(this.d, null, null, new d(this, contentItem, num2, num3, null), 3);
    }

    public final long e() {
        if (!this.e) {
            return -1L;
        }
        ContentItem contentItem = this.g;
        if (contentItem == null) {
            Log.w("ExternalPlayerTracker", "No content item set, skipping progress save");
            f();
            return -1L;
        }
        long jCurrentTimeMillis = System.currentTimeMillis() - this.f;
        long j = 1000;
        Log.w("ExternalPlayerTracker", "Returned from external player after " + (jCurrentTimeMillis / j) + CmcdData.STREAMING_FORMAT_SS);
        ng1 ng1Var = this.d;
        if (jCurrentTimeMillis < 30000) {
            Log.w("ExternalPlayerTracker", "Watch time too short (" + jCurrentTimeMillis + "ms < 30000ms), discarding");
            wj0.c(ng1Var, null, null, new com.strmr.ps.ui.player.b(this, null), 3);
            f();
            return jCurrentTimeMillis;
        }
        long j2 = this.k + jCurrentTimeMillis;
        long j3 = this.h;
        if (j2 > j3) {
            j2 = j3;
        }
        float f = j2 / j3;
        long j4 = j2 / j;
        long j5 = j3 / j;
        wj0.c(ng1Var, null, null, new e(this, contentItem, j2, f, null), 3);
        f();
        return jCurrentTimeMillis;
    }

    public final void f() {
        this.e = false;
        this.f = 0L;
        this.g = null;
        this.h = 0L;
        this.i = null;
        this.j = null;
        this.k = 0L;
        this.l = null;
    }
}
