package com.martsnew.tv.ui.search;

import A6.k;
import B6.a;
import C5.b;
import E1.F;
import E6.j;
import K2.e;
import N5.c;
import N5.l;
import O1.C0225a;
import O5.h;
import P6.n;
import W2.g;
import W5.A;
import W5.AbstractC0358a;
import W5.m;
import W5.w;
import W5.y;
import Z6.AbstractC0385z;
import Z6.r0;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.leanback.app.E;
import androidx.leanback.widget.C0455b;
import androidx.leanback.widget.C0468h0;
import androidx.leanback.widget.S;
import androidx.leanback.widget.T;
import androidx.leanback.widget.X;
import androidx.lifecycle.P;
import b7.EnumC0551a;
import c6.C0588d;
import c6.C0589e;
import c7.C0595c;
import c7.C0599g;
import c7.C0603k;
import com.google.android.gms.internal.measurement.AbstractC0707u1;
import com.martsnew.domain.model.Movie;
import com.martsnew.tv.R;
import com.martsnew.tv.customviews.keyboard.KeyboardView;
import com.martsnew.tv.ui.main.TvMainFragment;
import d3.V0;
import d5.AbstractC0844a;
import h0.ComponentCallbacksC1023x;
import java.lang.reflect.InvocationTargetException;
import n4.u0;
import r5.C1505j;
import u5.K0;
import u5.N0;
import u5.P0;
import y5.InterfaceC1760e;
import y5.InterfaceC1761f;

/* JADX INFO: loaded from: classes.dex */
public final class TvSearchFragment extends AbstractC0358a implements InterfaceC1760e, S, T, InterfaceC1761f, View.OnClickListener {

    /* JADX INFO: renamed from: Y0, reason: collision with root package name */
    public final e f11205Y0;

    /* JADX INFO: renamed from: Z0, reason: collision with root package name */
    public P0 f11206Z0;
    public K0 a1;

    /* JADX INFO: renamed from: b1, reason: collision with root package name */
    public N0 f11207b1;

    /* JADX INFO: renamed from: c1, reason: collision with root package name */
    public final k f11208c1;

    /* JADX INFO: renamed from: d1, reason: collision with root package name */
    public final k f11209d1;

    public TvSearchFragment() {
        A6.e eVarJ = AbstractC0707u1.j(new h(10, new h(9, this)));
        this.f11205Y0 = new e(n.a(A.class), new l(eVarJ, 12), new A5.h(this, eVarJ, 19), new l(eVarJ, 13));
        this.f11208c1 = new k(new b(9));
        this.f11209d1 = new k(new b(10));
    }

    public static final void s0(TvSearchFragment tvSearchFragment, boolean z8) {
        if (z8) {
            View view = tvSearchFragment.X;
            K0 k02 = tvSearchFragment.a1;
            if (k02 != null) {
                g.H(view, k02.f6656z);
                return;
            } else {
                P6.g.h("noResultBinding");
                throw null;
            }
        }
        View view2 = tvSearchFragment.X;
        K0 k03 = tvSearchFragment.a1;
        if (k03 != null) {
            g.J(view2, k03.f6656z);
        } else {
            P6.g.h("noResultBinding");
            throw null;
        }
    }

    @Override // androidx.leanback.app.g, h0.ComponentCallbacksC1023x
    public final void F(Bundle bundle) {
        super.F(bundle);
        c cVar = new c(1, 1);
        cVar.f4751c = false;
        cVar.f4749a = 6;
        this.K0 = cVar;
        cVar.f4752d = this.f8187R0;
        E e8 = this.f8183N0;
        if (e8 != null) {
            cVar.f4753e = e8;
        }
        C0455b c0455bT0 = t0();
        this.f8180J0 = c0455bT0;
        androidx.leanback.widget.P0 p02 = this.f8181L0;
        if (p02 != null) {
            this.K0.c(p02, c0455bT0);
            int i = this.f8185P0;
            if (i != -1) {
                this.f8181L0.f8486c.setSelectedPosition(i);
            }
        }
    }

    @Override // androidx.leanback.app.E, h0.ComponentCallbacksC1023x
    public final View G(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        P6.g.e(layoutInflater, "inflater");
        View viewG = super.G(layoutInflater, viewGroup, bundle);
        ViewGroup viewGroup2 = (ViewGroup) viewG;
        int i = P0.f17635Q;
        P0 p02 = (P0) U.c.b(layoutInflater, R.layout.layout_tv_keyboard, viewGroup2, true);
        this.f11206Z0 = p02;
        if (p02 == null) {
            P6.g.h("keyboardBinding");
            throw null;
        }
        View view = p02.f6656z;
        P6.g.d(view, "getRoot(...)");
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams == null) {
            throw new NullPointerException("null cannot be cast to non-null type android.widget.FrameLayout.LayoutParams");
        }
        FrameLayout.LayoutParams layoutParams2 = (FrameLayout.LayoutParams) layoutParams;
        layoutParams2.setMargins(s().getDimensionPixelSize(R.dimen.dp_150), 0, 0, 0);
        view.setLayoutParams(layoutParams2);
        K0 k0M = K0.M(layoutInflater, viewGroup2);
        this.a1 = k0M;
        if (k0M == null) {
            P6.g.h("noResultBinding");
            throw null;
        }
        View view2 = k0M.f6656z;
        P6.g.d(view2, "getRoot(...)");
        ViewGroup.LayoutParams layoutParams3 = view2.getLayoutParams();
        if (layoutParams3 == null) {
            throw new NullPointerException("null cannot be cast to non-null type android.widget.FrameLayout.LayoutParams");
        }
        FrameLayout.LayoutParams layoutParams4 = (FrameLayout.LayoutParams) layoutParams3;
        layoutParams4.setMargins(s().getDimensionPixelSize(R.dimen.dp_715), 0, 0, 0);
        view2.setLayoutParams(layoutParams4);
        int i5 = N0.f17624M;
        N0 n02 = (N0) U.c.b(layoutInflater, R.layout.layout_shimmer_search, viewGroup2, false);
        this.f11207b1 = n02;
        if (n02 == null) {
            P6.g.h("layoutShimmer");
            throw null;
        }
        View view3 = n02.f6656z;
        P6.g.d(view3, "getRoot(...)");
        ViewGroup.LayoutParams layoutParams5 = view3.getLayoutParams();
        if (layoutParams5 == null) {
            throw new NullPointerException("null cannot be cast to non-null type android.widget.FrameLayout.LayoutParams");
        }
        FrameLayout.LayoutParams layoutParams6 = (FrameLayout.LayoutParams) layoutParams5;
        layoutParams6.setMargins(s().getDimensionPixelSize(R.dimen.dp_715), s().getDimensionPixelSize(R.dimen.dp_188), 0, 0);
        view3.setLayoutParams(layoutParams6);
        N0 n03 = this.f11207b1;
        if (n03 == null) {
            P6.g.h("layoutShimmer");
            throw null;
        }
        n03.f17625K.setAdapter((W5.c) this.f11209d1.getValue());
        P0 p03 = this.f11206Z0;
        if (p03 == null) {
            P6.g.h("keyboardBinding");
            throw null;
        }
        KeyboardView keyboardView = p03.f17640O;
        keyboardView.requestFocus();
        keyboardView.setKeyClickListener(new a(3, p03));
        TextView textView = p03.f17641P;
        P6.g.d(textView, "textSearch");
        AbstractC0385z.r(P.f(this), null, new C0599g(new C0225a(new V0(4, new C0603k(new M5.b(10), new C0225a(new C0589e(textView, null), new C0595c(new C0588d(textView, null), j.f2226q, -2, EnumC0551a.f9221q)), null)), new W5.n(null, this), 26, false), null), 3);
        if (!X6.e.V(u0().f7141f)) {
            textView.setText(X6.e.j0(u0().f7141f).toString());
        }
        p03.f17638M.setOnClickListener(this);
        p03.f17637L.setOnClickListener(this);
        p03.f17636K.setOnClickListener(this);
        p03.f17639N.setOnClickListener(this);
        this.f8183N0 = this;
        c cVar = this.K0;
        if (cVar != null) {
            cVar.f4753e = this;
        }
        this.f8182M0 = this;
        AbstractC0385z.r(P.f(this), null, new m(null, this), 3);
        return viewG;
    }

    @Override // androidx.leanback.widget.S
    public final void b(X x8, Object obj, C0468h0 c0468h0, Object obj2) {
        ComponentCallbacksC1023x componentCallbacksC1023x;
        if (obj instanceof Movie) {
            Movie movie = (Movie) obj;
            ComponentCallbacksC1023x componentCallbacksC1023x2 = this.f13268N;
            if (componentCallbacksC1023x2 == null || (componentCallbacksC1023x = componentCallbacksC1023x2.f13268N) == null) {
                return;
            }
            F fS = u0.s(componentCallbacksC1023x);
            P6.g.e(movie, "movie");
            fS.j(new C1505j(movie));
        }
    }

    @Override // y5.InterfaceC1761f
    public final void c() {
        P0 p02 = this.f11206Z0;
        if (p02 != null) {
            p02.f17640O.requestFocus();
        } else {
            P6.g.h("keyboardBinding");
            throw null;
        }
    }

    @Override // androidx.leanback.widget.T
    public final void e(X x8, Object obj, C0468h0 c0468h0, Object obj2) throws IllegalAccessException, InvocationTargetException {
        if (obj instanceof Movie) {
            int iIndexOf = t0().f8626c.indexOf(obj);
            if (this.f8180J0.f8626c.size() < 50 || (this.f8180J0.f8626c.size() / 6) - (iIndexOf / 6) > 4) {
                return;
            }
            A aU0 = u0();
            P0 p02 = this.f11206Z0;
            if (p02 == null) {
                P6.g.h("keyboardBinding");
                throw null;
            }
            String string = p02.f17641P.getText().toString();
            int size = (this.f8180J0.f8626c.size() / 50) + 1;
            P6.g.e(string, "query");
            r0 r0Var = aU0.f7145k;
            if (r0Var != null) {
                r0Var.b(null);
            }
            aU0.f7145k = aU0.e(false, new y(aU0, string, size, null));
        }
    }

    @Override // y5.InterfaceC1760e
    public final void f() {
        P0 p02 = this.f11206Z0;
        if (p02 == null) {
            P6.g.h("keyboardBinding");
            throw null;
        }
        if (!p02.f17640O.x0()) {
            P0 p03 = this.f11206Z0;
            if (p03 == null) {
                P6.g.h("keyboardBinding");
                throw null;
            }
            if (!p03.f17638M.isFocused()) {
                P0 p04 = this.f11206Z0;
                if (p04 == null) {
                    P6.g.h("keyboardBinding");
                    throw null;
                }
                if (!p04.f17639N.isFocused()) {
                    return;
                }
            }
        }
        ComponentCallbacksC1023x componentCallbacksC1023x = this.f13268N;
        ComponentCallbacksC1023x componentCallbacksC1023x2 = componentCallbacksC1023x != null ? componentCallbacksC1023x.f13268N : null;
        if (componentCallbacksC1023x2 instanceof TvMainFragment) {
            ((TvMainFragment) componentCallbacksC1023x2).w0();
        }
    }

    @Override // android.view.View.OnClickListener
    public final void onClick(View view) {
        String string;
        CharSequence charSequenceSubSequence;
        Integer numValueOf = view != null ? Integer.valueOf(view.getId()) : null;
        if (numValueOf != null && numValueOf.intValue() == R.id.buttonSearch) {
            return;
        }
        if (numValueOf != null && numValueOf.intValue() == R.id.buttonSpecialCharacter) {
            A aU0 = u0();
            aU0.e(false, new w(aU0, null));
            return;
        }
        if (numValueOf != null && numValueOf.intValue() == R.id.buttonSpace) {
            P0 p02 = this.f11206Z0;
            if (p02 == null) {
                P6.g.h("keyboardBinding");
                throw null;
            }
            String strL = AbstractC0844a.l(p02.f17641P.getText().toString(), " ");
            P0 p03 = this.f11206Z0;
            if (p03 != null) {
                p03.f17641P.setText(strL);
                return;
            } else {
                P6.g.h("keyboardBinding");
                throw null;
            }
        }
        if (numValueOf != null && numValueOf.intValue() == R.id.buttonDelete) {
            P0 p04 = this.f11206Z0;
            if (p04 == null) {
                P6.g.h("keyboardBinding");
                throw null;
            }
            String string2 = p04.f17641P.getText().toString();
            if (string2.length() > 0) {
                int length = string2.length() - 1;
                int length2 = string2.length();
                if (length2 < length) {
                    throw new IndexOutOfBoundsException("End index (" + length2 + ") is less than start index (" + length + ").");
                }
                if (length2 == length) {
                    charSequenceSubSequence = string2.subSequence(0, string2.length());
                } else {
                    StringBuilder sb = new StringBuilder(string2.length() - (length2 - length));
                    sb.append((CharSequence) string2, 0, length);
                    sb.append((CharSequence) string2, length2, string2.length());
                    charSequenceSubSequence = sb;
                }
                string = charSequenceSubSequence.toString();
            } else {
                string = "";
            }
            P0 p05 = this.f11206Z0;
            if (p05 != null) {
                p05.f17641P.setText(string);
            } else {
                P6.g.h("keyboardBinding");
                throw null;
            }
        }
    }

    public final C0455b t0() {
        return (C0455b) this.f11208c1.getValue();
    }

    public final A u0() {
        return (A) this.f11205Y0.getValue();
    }
}
