package androidx.leanback.widget;

import android.util.Pair;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import androidx.leanback.widget.ap;
import androidx.leanback.widget.bb;
import defpackage.m38;
import defpackage.uy3;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
@m38
public class ar {
    public final ArrayList a = new ArrayList();
    public boolean b;
    public ap.g c;

    public static void e(bb.b bVar, TextView textView) {
        uy3 uy3Var = bVar.a;
        if (textView == bVar.d) {
            if (uy3Var.g != null) {
                uy3Var.g = textView.getText();
                return;
            } else {
                uy3Var.d = textView.getText();
                return;
            }
        }
        if (textView == bVar.c) {
            if (uy3Var.f != null) {
                uy3Var.f = textView.getText();
            } else {
                uy3Var.c = textView.getText();
            }
        }
    }

    public final void a(ap apVar, ap apVar2) {
        this.a.add(new Pair(apVar, apVar2));
        if (apVar != null) {
            apVar.j = this;
        }
        if (apVar2 != null) {
            apVar2.j = this;
        }
    }

    public final void b(View view) {
        if (this.b) {
            this.b = false;
            ((InputMethodManager) view.getContext().getSystemService("input_method")).hideSoftInputFromWindow(view.getWindowToken(), 0);
            this.c.b();
        }
    }

    public final void c(ap apVar, TextView textView) {
        int iIndexOf;
        bb.b bVarD = apVar.d(textView);
        e(bVarD, textView);
        ap.f fVar = apVar.h;
        if (fVar != null) {
            fVar.a(bVarD.a);
        }
        this.c.a();
        apVar.i.f(bVarD, false, true);
        uy3 uy3Var = bVarD.a;
        if (-2 != uy3Var.a && (iIndexOf = apVar.g.indexOf(uy3Var)) >= 0) {
            int i = iIndexOf + 1;
            while (true) {
                ArrayList arrayList = apVar.g;
                int size = arrayList.size();
                while (i < size) {
                    ((uy3) arrayList.get(i)).getClass();
                    i++;
                }
                if (i < size) {
                    bb.b bVar = (bb.b) apVar.i.b.findViewHolderForPosition(i);
                    if (bVar != null) {
                        bVar.a.getClass();
                        b(bVar.itemView);
                        bVar.itemView.requestFocus();
                        return;
                    }
                } else {
                    int i2 = 0;
                    while (true) {
                        ArrayList arrayList2 = this.a;
                        if (i2 >= arrayList2.size()) {
                            apVar = null;
                            break;
                        }
                        Pair pair = (Pair) arrayList2.get(i2);
                        if (pair.first == apVar) {
                            apVar = (ap) pair.second;
                            break;
                        }
                        i2++;
                    }
                    if (apVar == null) {
                        break;
                    } else {
                        i = 0;
                    }
                }
            }
        }
        b(textView);
        bVarD.itemView.requestFocus();
    }

    public final void d(ap apVar, TextView textView) {
        bb.b bVarD = apVar.d(textView);
        e(bVarD, textView);
        this.c.c();
        apVar.i.f(bVarD, false, true);
        b(textView);
        bVarD.itemView.requestFocus();
    }
}
