package o;

import android.R;
import android.content.res.Resources;
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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class VN<S> extends RW {
    public int m;
    public C0576Wf n;

    /* renamed from: o, reason: collision with root package name */
    public TP f374o;
    public int p;
    public Gm0 q;
    public RecyclerView r;
    public RecyclerView s;
    public View t;
    public View u;
    public View v;
    public View w;

    public final void f(TP tp) {
        com.google.android.material.datepicker.c cVar = (com.google.android.material.datepicker.c) this.s.getAdapter();
        int iD = cVar.c.l.d(tp);
        int iD2 = iD - cVar.c.l.d(this.f374o);
        boolean z = Math.abs(iD2) > 3;
        boolean z2 = iD2 > 0;
        this.f374o = tp;
        if (z && z2) {
            this.s.Y(iD - 3);
            this.s.post(new RunnableC1212fg(iD, 2, this));
        } else if (!z) {
            this.s.post(new RunnableC1212fg(iD, 2, this));
        } else {
            this.s.Y(iD + 3);
            this.s.post(new RunnableC1212fg(iD, 2, this));
        }
    }

    public final void g(int i) {
        this.p = i;
        if (i == 2) {
            this.r.getLayoutManager().n0(this.f374o.n - ((Xm0) this.r.getAdapter()).c.n.l.n);
            this.v.setVisibility(0);
            this.w.setVisibility(8);
            this.t.setVisibility(8);
            this.u.setVisibility(8);
            return;
        }
        if (i == 1) {
            this.v.setVisibility(8);
            this.w.setVisibility(0);
            this.t.setVisibility(0);
            this.u.setVisibility(0);
            f(this.f374o);
        }
    }

    @Override // androidx.fragment.app.l
    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (bundle == null) {
            bundle = getArguments();
        }
        this.m = bundle.getInt("THEME_RES_ID_KEY");
        if (bundle.getParcelable("GRID_SELECTOR_KEY") != null) {
            throw new ClassCastException();
        }
        this.n = (C0576Wf) bundle.getParcelable("CALENDAR_CONSTRAINTS_KEY");
        if (bundle.getParcelable("DAY_VIEW_DECORATOR_KEY") != null) {
            throw new ClassCastException();
        }
        this.f374o = (TP) bundle.getParcelable("CURRENT_MONTH_KEY");
    }

    @Override // androidx.fragment.app.l
    public final View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) throws Resources.NotFoundException {
        int i;
        int i2;
        C2621uV c2621uV;
        RecyclerView recyclerView;
        RecyclerView recyclerView2;
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), this.m);
        this.q = new Gm0(contextThemeWrapper);
        LayoutInflater layoutInflaterCloneInContext = layoutInflater.cloneInContext(contextThemeWrapper);
        TP tp = this.n.l;
        if (C0806bO.i(contextThemeWrapper, R.attr.windowFullscreen)) {
            i = com.google.android.gms.common.R.layout.mtrl_calendar_vertical;
            i2 = 1;
        } else {
            i = com.google.android.gms.common.R.layout.mtrl_calendar_horizontal;
            i2 = 0;
        }
        View viewInflate = layoutInflaterCloneInContext.inflate(i, viewGroup, false);
        Resources resources = requireContext().getResources();
        int dimensionPixelOffset = resources.getDimensionPixelOffset(com.google.android.gms.common.R.dimen.mtrl_calendar_navigation_bottom_padding) + resources.getDimensionPixelOffset(com.google.android.gms.common.R.dimen.mtrl_calendar_navigation_top_padding) + resources.getDimensionPixelSize(com.google.android.gms.common.R.dimen.mtrl_calendar_navigation_height);
        int dimensionPixelSize = resources.getDimensionPixelSize(com.google.android.gms.common.R.dimen.mtrl_calendar_days_of_week_height);
        int i3 = UP.d;
        viewInflate.setMinimumHeight(dimensionPixelOffset + dimensionPixelSize + (resources.getDimensionPixelOffset(com.google.android.gms.common.R.dimen.mtrl_calendar_month_vertical_padding) * (i3 - 1)) + (resources.getDimensionPixelSize(com.google.android.gms.common.R.dimen.mtrl_calendar_day_height) * i3) + resources.getDimensionPixelOffset(com.google.android.gms.common.R.dimen.mtrl_calendar_bottom_padding));
        GridView gridView = (GridView) viewInflate.findViewById(com.google.android.gms.common.R.id.mtrl_calendar_days_of_week);
        Oj0.m(gridView, new RN(0));
        int i4 = this.n.p;
        gridView.setAdapter((ListAdapter) (i4 > 0 ? new C0171Gp(i4) : new C0171Gp()));
        gridView.setNumColumns(tp.f340o);
        gridView.setEnabled(false);
        this.s = (RecyclerView) viewInflate.findViewById(com.google.android.gms.common.R.id.mtrl_calendar_months);
        getContext();
        this.s.setLayoutManager(new SN(this, i2, i2));
        this.s.setTag("MONTHS_VIEW_GROUP_TAG");
        com.google.android.material.datepicker.c cVar = new com.google.android.material.datepicker.c(contextThemeWrapper, this.n, new C0247Jn(24, this));
        this.s.setAdapter(cVar);
        int integer = contextThemeWrapper.getResources().getInteger(com.google.android.gms.common.R.integer.mtrl_calendar_year_selector_span);
        RecyclerView recyclerView3 = (RecyclerView) viewInflate.findViewById(com.google.android.gms.common.R.id.mtrl_calendar_year_selector_frame);
        this.r = recyclerView3;
        if (recyclerView3 != null) {
            recyclerView3.setHasFixedSize(true);
            this.r.setLayoutManager(new GridLayoutManager(integer));
            this.r.setAdapter(new Xm0(this));
            RecyclerView recyclerView4 = this.r;
            TN tn = new TN();
            AbstractC1789li0.c(null);
            AbstractC1789li0.c(null);
            recyclerView4.g(tn);
        }
        if (viewInflate.findViewById(com.google.android.gms.common.R.id.month_navigation_fragment_toggle) != null) {
            MaterialButton materialButton = (MaterialButton) viewInflate.findViewById(com.google.android.gms.common.R.id.month_navigation_fragment_toggle);
            materialButton.setTag("SELECTOR_TOGGLE_TAG");
            Oj0.m(materialButton, new C2545th(2, this));
            View viewFindViewById = viewInflate.findViewById(com.google.android.gms.common.R.id.month_navigation_previous);
            this.t = viewFindViewById;
            viewFindViewById.setTag("NAVIGATION_PREV_TAG");
            View viewFindViewById2 = viewInflate.findViewById(com.google.android.gms.common.R.id.month_navigation_next);
            this.u = viewFindViewById2;
            viewFindViewById2.setTag("NAVIGATION_NEXT_TAG");
            this.v = viewInflate.findViewById(com.google.android.gms.common.R.id.mtrl_calendar_year_selector_frame);
            this.w = viewInflate.findViewById(com.google.android.gms.common.R.id.mtrl_calendar_day_selector_frame);
            g(1);
            materialButton.setText(this.f374o.c());
            this.s.h(new UN(this, cVar, materialButton));
            materialButton.setOnClickListener(new V0(2, this));
            this.u.setOnClickListener(new QN(this, cVar, 1));
            this.t.setOnClickListener(new QN(this, cVar, 0));
        }
        if (!C0806bO.i(contextThemeWrapper, R.attr.windowFullscreen) && (recyclerView2 = (c2621uV = new C2621uV()).a) != (recyclerView = this.s)) {
            C2030o90 c2030o90 = c2621uV.b;
            if (recyclerView2 != null) {
                ArrayList arrayList = recyclerView2.p0;
                if (arrayList != null) {
                    arrayList.remove(c2030o90);
                }
                c2621uV.a.setOnFlingListener(null);
            }
            c2621uV.a = recyclerView;
            if (recyclerView != null) {
                if (recyclerView.getOnFlingListener() != null) {
                    throw new IllegalStateException("An instance of OnFlingListener already set.");
                }
                c2621uV.a.h(c2030o90);
                c2621uV.a.setOnFlingListener(c2621uV);
                new Scroller(c2621uV.a.getContext(), new DecelerateInterpolator());
                c2621uV.f();
            }
        }
        this.s.Y(cVar.c.l.d(this.f374o));
        Oj0.m(this.s, new RN(1));
        return viewInflate;
    }

    @Override // androidx.fragment.app.l
    public final void onSaveInstanceState(Bundle bundle) {
        bundle.putInt("THEME_RES_ID_KEY", this.m);
        bundle.putParcelable("GRID_SELECTOR_KEY", null);
        bundle.putParcelable("CALENDAR_CONSTRAINTS_KEY", this.n);
        bundle.putParcelable("DAY_VIEW_DECORATOR_KEY", null);
        bundle.putParcelable("CURRENT_MONTH_KEY", this.f374o);
    }
}
