package defpackage;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.strmr.ps.R;
import com.strmr.ps.data.local.entity.PlaybackProgress;
import com.strmr.ps.data.model.tmdb.TMDBEpisode;
import defpackage.dp2;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0018\u00002\u00020\u0001:\u0001\u0002¨\u0006\u0003"}, d2 = {"Ldp2;", "Landroid/app/Dialog;", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class dp2 extends Dialog {
    public static final /* synthetic */ int m = 0;
    public final int a;
    public final int b;
    public final uga c;
    public RecyclerView d;
    public ProgressBar e;
    public TextView f;
    public TextView i;
    public TextView j;
    public List k;
    public boolean l;

    @Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0002\u0018\u00002\f\u0012\b\u0012\u00060\u0002R\u00020\u00000\u0001:\u0001\u0003¨\u0006\u0004"}, d2 = {"Ldp2$a;", "Landroidx/recyclerview/widget/RecyclerView$g;", "Ldp2$a$a;", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a extends RecyclerView.g<C0129a> {
        public final List a;
        public final int b;
        public final co c;

        /* JADX INFO: renamed from: dp2$a$a, reason: collision with other inner class name */
        @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\u0086\u0004\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Ldp2$a$a;", "Landroidx/recyclerview/widget/RecyclerView$ag;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
        public final class C0129a extends RecyclerView.ag {
            public static final /* synthetic */ int g = 0;
            public final ImageView a;
            public final TextView b;
            public final TextView c;
            public final TextView d;
            public final TextView e;

            public C0129a(View view) {
                super(view);
                View viewFindViewById = view.findViewById(R.id.episode_thumbnail);
                cr4.f(viewFindViewById, "findViewById(...)");
                this.a = (ImageView) viewFindViewById;
                View viewFindViewById2 = view.findViewById(R.id.episode_number);
                cr4.f(viewFindViewById2, "findViewById(...)");
                this.b = (TextView) viewFindViewById2;
                View viewFindViewById3 = view.findViewById(R.id.episode_title);
                cr4.f(viewFindViewById3, "findViewById(...)");
                this.c = (TextView) viewFindViewById3;
                View viewFindViewById4 = view.findViewById(R.id.episode_date);
                cr4.f(viewFindViewById4, "findViewById(...)");
                this.d = (TextView) viewFindViewById4;
                View viewFindViewById5 = view.findViewById(R.id.current_indicator);
                cr4.f(viewFindViewById5, "findViewById(...)");
                this.e = (TextView) viewFindViewById5;
                boolean zHasSystemFeature = view.getContext().getPackageManager().hasSystemFeature("android.software.leanback");
                view.setFocusable(true);
                view.setFocusableInTouchMode(zHasSystemFeature);
            }
        }

        public a(List list, int i, co coVar) {
            cr4.g(list, "episodes");
            this.a = list;
            this.b = i;
            this.c = coVar;
        }

        @Override // androidx.recyclerview.widget.RecyclerView.g
        public final int getItemCount() {
            return this.a.size();
        }

        @Override // androidx.recyclerview.widget.RecyclerView.g
        public final void onBindViewHolder(RecyclerView.ag agVar, int i) {
            C0129a c0129a = (C0129a) agVar;
            cr4.g(c0129a, "holder");
            final TMDBEpisode tMDBEpisode = (TMDBEpisode) this.a.get(i);
            final a aVar = a.this;
            ImageView imageView = c0129a.a;
            TextView textView = c0129a.d;
            cr4.g(tMDBEpisode, PlaybackProgress.TYPE_EPISODE);
            Integer episodeNumber = tMDBEpisode.getEpisodeNumber();
            int iIntValue = episodeNumber != null ? episodeNumber.intValue() : 0;
            String name = tMDBEpisode.getName();
            if (name == null) {
                name = n41.k("Episode ", iIntValue);
            }
            c0129a.b.setText(String.format("E%02d", Arrays.copyOf(new Object[]{Integer.valueOf(iIntValue)}, 1)));
            c0129a.c.setText(name);
            Integer runtime = tMDBEpisode.getRuntime();
            if (runtime == null || runtime.intValue() <= 0) {
                String airDate = tMDBEpisode.getAirDate();
                if (airDate == null) {
                    airDate = "";
                }
                textView.setText(airDate);
            } else {
                textView.setText(runtime + CmcdData.OBJECT_TYPE_MANIFEST);
            }
            String stillUrl = tMDBEpisode.getStillUrl();
            if (stillUrl != null) {
                ((l08) com.bumptech.glide.a.f(c0129a.itemView.getContext()).n(stillUrl).b()).ah(imageView);
            } else {
                imageView.setImageResource(android.R.color.darker_gray);
            }
            final boolean z = iIntValue == aVar.b;
            c0129a.itemView.setActivated(z);
            c0129a.e.setVisibility(z ? 0 : 8);
            c0129a.itemView.setOnClickListener(new View.OnClickListener() { // from class: cp2
                @Override // android.view.View.OnClickListener
                public final void onClick(View view) {
                    int i2 = dp2.a.C0129a.g;
                    if (z) {
                        return;
                    }
                    aVar.c.invoke(tMDBEpisode);
                }
            });
            c0129a.itemView.setOnFocusChangeListener(new fb(c0129a, 3));
        }

        @Override // androidx.recyclerview.widget.RecyclerView.g
        public final RecyclerView.ag onCreateViewHolder(ViewGroup viewGroup, int i) {
            View viewD = n41.d(viewGroup, "parent", R.layout.item_episode_selection, viewGroup, false);
            cr4.d(viewD);
            return new C0129a(viewD);
        }
    }

    public dp2(Context context, int i, int i2, uga ugaVar) {
        super(context, R.style.PlayerPanelDialogTheme);
        this.a = i;
        this.b = i2;
        this.c = ugaVar;
        this.k = ik2.a;
    }

    public final void a(List list) {
        this.k = list;
        ProgressBar progressBar = this.e;
        if (progressBar == null) {
            cr4.n("loadingIndicator");
            throw null;
        }
        progressBar.setVisibility(8);
        int i = 0;
        if (this.k.isEmpty()) {
            TextView textView = this.f;
            if (textView == null) {
                cr4.n("emptyText");
                throw null;
            }
            textView.setVisibility(0);
            RecyclerView recyclerView = this.d;
            if (recyclerView != null) {
                recyclerView.setVisibility(8);
                return;
            } else {
                cr4.n("recyclerView");
                throw null;
            }
        }
        TextView textView2 = this.f;
        if (textView2 == null) {
            cr4.n("emptyText");
            throw null;
        }
        textView2.setVisibility(8);
        RecyclerView recyclerView2 = this.d;
        if (recyclerView2 == null) {
            cr4.n("recyclerView");
            throw null;
        }
        recyclerView2.setVisibility(0);
        List list2 = this.k;
        co coVar = new co(this, 10);
        int i2 = this.b;
        a aVar = new a(list2, i2, coVar);
        RecyclerView recyclerView3 = this.d;
        if (recyclerView3 == null) {
            cr4.n("recyclerView");
            throw null;
        }
        recyclerView3.setAdapter(aVar);
        Iterator it = this.k.iterator();
        while (true) {
            if (!it.hasNext()) {
                i = -1;
                break;
            }
            Integer episodeNumber = ((TMDBEpisode) it.next()).getEpisodeNumber();
            if (episodeNumber != null && episodeNumber.intValue() == i2) {
                break;
            } else {
                i++;
            }
        }
        if (i >= 0) {
            RecyclerView recyclerView4 = this.d;
            if (recyclerView4 != null) {
                recyclerView4.post(new px(this, i, 2));
                return;
            } else {
                cr4.n("recyclerView");
                throw null;
            }
        }
        if (this.k.isEmpty()) {
            return;
        }
        RecyclerView recyclerView5 = this.d;
        if (recyclerView5 != null) {
            recyclerView5.post(new r21(this, 23));
        } else {
            cr4.n("recyclerView");
            throw null;
        }
    }

    @Override // android.app.Dialog, android.view.Window.Callback
    public final boolean dispatchKeyEvent(KeyEvent keyEvent) {
        cr4.g(keyEvent, "event");
        int keyCode = keyEvent.getKeyCode();
        if (keyCode == 23 || keyCode == 66) {
            if (keyEvent.getAction() == 1 && !this.l) {
                this.l = true;
                return true;
            }
            if (keyEvent.getAction() == 0 && !this.l) {
                return true;
            }
        }
        return super.dispatchKeyEvent(keyEvent);
    }

    @Override // android.app.Dialog
    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(1);
        setContentView(R.layout.dialog_episode_selection);
        Window window = getWindow();
        if (window != null) {
            window.setLayout(-1, -1);
            window.setGravity(8388613);
        }
        this.d = (RecyclerView) findViewById(R.id.episode_list);
        this.e = (ProgressBar) findViewById(R.id.loading_indicator);
        this.f = (TextView) findViewById(R.id.empty_text);
        this.i = (TextView) findViewById(R.id.dialog_title);
        this.j = (TextView) findViewById(R.id.dialog_subtitle);
        TextView textView = this.i;
        if (textView == null) {
            cr4.n("titleText");
            throw null;
        }
        textView.setText("Episodes");
        TextView textView2 = this.j;
        if (textView2 == null) {
            cr4.n("subtitleText");
            throw null;
        }
        textView2.setText("Season " + this.a);
        RecyclerView recyclerView = this.d;
        if (recyclerView == null) {
            cr4.n("recyclerView");
            throw null;
        }
        getContext();
        recyclerView.setLayoutManager(new LinearLayoutManager());
        RecyclerView recyclerView2 = this.d;
        if (recyclerView2 != null) {
            recyclerView2.addItemDecoration(new ep2());
        } else {
            cr4.n("recyclerView");
            throw null;
        }
    }

    @Override // android.app.Dialog
    public final void show() {
        super.show();
        this.l = false;
    }
}
