package com.google.android.material.datepicker;

import D1.N;
import M.J;
import N1.O;
import N1.w0;
import android.R;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.Scroller;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.button.MaterialButton;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public final class l<S> extends t {

    /* JADX INFO: renamed from: p0, reason: collision with root package name */
    public int f10239p0;
    public b q0;

    /* JADX INFO: renamed from: r0, reason: collision with root package name */
    public o f10240r0;

    /* JADX INFO: renamed from: s0, reason: collision with root package name */
    public int f10241s0;

    /* JADX INFO: renamed from: t0, reason: collision with root package name */
    public c f10242t0;

    /* JADX INFO: renamed from: u0, reason: collision with root package name */
    public RecyclerView f10243u0;

    /* JADX INFO: renamed from: v0, reason: collision with root package name */
    public RecyclerView f10244v0;

    /* JADX INFO: renamed from: w0, reason: collision with root package name */
    public View f10245w0;

    /* JADX INFO: renamed from: x0, reason: collision with root package name */
    public View f10246x0;

    @Override // h0.ComponentCallbacksC1023x
    public final void F(Bundle bundle) {
        super.F(bundle);
        if (bundle == null) {
            bundle = this.f13298v;
        }
        this.f10239p0 = bundle.getInt("THEME_RES_ID_KEY");
        if (bundle.getParcelable("GRID_SELECTOR_KEY") != null) {
            throw new ClassCastException();
        }
        this.q0 = (b) bundle.getParcelable("CALENDAR_CONSTRAINTS_KEY");
        this.f10240r0 = (o) bundle.getParcelable("CURRENT_MONTH_KEY");
    }

    @Override // h0.ComponentCallbacksC1023x
    public final View G(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        int i;
        int i5;
        O o8;
        RecyclerView recyclerView;
        RecyclerView recyclerView2;
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(o(), this.f10239p0);
        this.f10242t0 = new c(contextThemeWrapper, 0);
        LayoutInflater layoutInflaterCloneInContext = layoutInflater.cloneInContext(contextThemeWrapper);
        o oVar = this.q0.f10211q;
        if (m.k0(contextThemeWrapper, R.attr.windowFullscreen)) {
            i = com.martsnew.tv.R.layout.mtrl_calendar_vertical;
            i5 = 1;
        } else {
            i = com.martsnew.tv.R.layout.mtrl_calendar_horizontal;
            i5 = 0;
        }
        View viewInflate = layoutInflaterCloneInContext.inflate(i, viewGroup, false);
        GridView gridView = (GridView) viewInflate.findViewById(com.martsnew.tv.R.id.mtrl_calendar_days_of_week);
        J.j(gridView, new P.e(1));
        gridView.setAdapter((ListAdapter) new f());
        gridView.setNumColumns(oVar.f10263t);
        gridView.setEnabled(false);
        this.f10244v0 = (RecyclerView) viewInflate.findViewById(com.martsnew.tv.R.id.mtrl_calendar_months);
        this.f10244v0.setLayoutManager(new g(this, i5, i5));
        this.f10244v0.setTag("MONTHS_VIEW_GROUP_TAG");
        s sVar = new s(contextThemeWrapper, this.q0, new P.g(25, this));
        this.f10244v0.setAdapter(sVar);
        int integer = contextThemeWrapper.getResources().getInteger(com.martsnew.tv.R.integer.mtrl_calendar_year_selector_span);
        RecyclerView recyclerView3 = (RecyclerView) viewInflate.findViewById(com.martsnew.tv.R.id.mtrl_calendar_year_selector_frame);
        this.f10243u0 = recyclerView3;
        if (recyclerView3 != null) {
            recyclerView3.setHasFixedSize(true);
            this.f10243u0.setLayoutManager(new GridLayoutManager(integer));
            this.f10243u0.setAdapter(new y(this));
            this.f10243u0.h(new h(this));
        }
        if (viewInflate.findViewById(com.martsnew.tv.R.id.month_navigation_fragment_toggle) != null) {
            MaterialButton materialButton = (MaterialButton) viewInflate.findViewById(com.martsnew.tv.R.id.month_navigation_fragment_toggle);
            materialButton.setTag("SELECTOR_TOGGLE_TAG");
            J.j(materialButton, new i(0, this));
            MaterialButton materialButton2 = (MaterialButton) viewInflate.findViewById(com.martsnew.tv.R.id.month_navigation_previous);
            materialButton2.setTag("NAVIGATION_PREV_TAG");
            MaterialButton materialButton3 = (MaterialButton) viewInflate.findViewById(com.martsnew.tv.R.id.month_navigation_next);
            materialButton3.setTag("NAVIGATION_NEXT_TAG");
            this.f10245w0 = viewInflate.findViewById(com.martsnew.tv.R.id.mtrl_calendar_year_selector_frame);
            this.f10246x0 = viewInflate.findViewById(com.martsnew.tv.R.id.mtrl_calendar_day_selector_frame);
            g0(1);
            materialButton.setText(this.f10240r0.q(viewInflate.getContext()));
            this.f10244v0.i(new j(this, sVar, materialButton));
            materialButton.setOnClickListener(new N(2, this));
            materialButton3.setOnClickListener(new k(this, sVar, 0));
            materialButton2.setOnClickListener(new k(this, sVar, 1));
        }
        if (!m.k0(contextThemeWrapper, R.attr.windowFullscreen) && (recyclerView2 = (o8 = new O()).f4482a) != (recyclerView = this.f10244v0)) {
            w0 w0Var = o8.f4483b;
            if (recyclerView2 != null) {
                ArrayList arrayList = recyclerView2.f9016A0;
                if (arrayList != null) {
                    arrayList.remove(w0Var);
                }
                o8.f4482a.setOnFlingListener(null);
            }
            o8.f4482a = recyclerView;
            if (recyclerView != null) {
                if (recyclerView.getOnFlingListener() != null) {
                    throw new IllegalStateException("An instance of OnFlingListener already set.");
                }
                o8.f4482a.i(w0Var);
                o8.f4482a.setOnFlingListener(o8);
                new Scroller(o8.f4482a.getContext(), new DecelerateInterpolator());
                o8.f();
            }
        }
        this.f10244v0.k0(sVar.f10276e.f10211q.r(this.f10240r0));
        return viewInflate;
    }

    @Override // h0.ComponentCallbacksC1023x
    public final void R(Bundle bundle) {
        bundle.putInt("THEME_RES_ID_KEY", this.f10239p0);
        bundle.putParcelable("GRID_SELECTOR_KEY", null);
        bundle.putParcelable("CALENDAR_CONSTRAINTS_KEY", this.q0);
        bundle.putParcelable("CURRENT_MONTH_KEY", this.f10240r0);
    }

    public final void f0(o oVar) {
        s sVar = (s) this.f10244v0.getAdapter();
        int iR = sVar.f10276e.f10211q.r(oVar);
        int iR2 = iR - sVar.f10276e.f10211q.r(this.f10240r0);
        boolean z8 = Math.abs(iR2) > 3;
        boolean z9 = iR2 > 0;
        this.f10240r0 = oVar;
        if (z8 && z9) {
            this.f10244v0.k0(iR - 3);
            this.f10244v0.post(new J.a(iR, 4, this));
        } else if (!z8) {
            this.f10244v0.post(new J.a(iR, 4, this));
        } else {
            this.f10244v0.k0(iR + 3);
            this.f10244v0.post(new J.a(iR, 4, this));
        }
    }

    public final void g0(int i) {
        this.f10241s0 = i;
        if (i == 2) {
            this.f10243u0.getLayoutManager().P0(this.f10240r0.f10262s - ((y) this.f10243u0.getAdapter()).f10282d.q0.f10211q.f10262s);
            this.f10245w0.setVisibility(0);
            this.f10246x0.setVisibility(8);
            return;
        }
        if (i == 1) {
            this.f10245w0.setVisibility(8);
            this.f10246x0.setVisibility(0);
            f0(this.f10240r0);
        }
    }
}
