package com.google.android.material.sidesheet;

import android.R;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.s1;
import androidx.media3.common.p;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import androidx.slidingpanelayout.widget.f;
import com.google.android.gms.common.api.internal.r;
import com.google.android.gms.measurement.internal.u1;
import com.google.android.material.shape.q;
import com.google.android.material.shape.s;
import d0.b;
import d0.d;
import i0.o;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.WeakHashMap;
import l.e;
import q0.c;
import u9.a;
import x8.k;
import x8.l;
import x8.m;
import y0.h;

/* JADX INFO: loaded from: classes2.dex */
public class SideSheetBehavior<V extends View> extends b implements n9.b {
    public static final int T = k.side_sheet_accessibility_pane_title;
    public static final int U = l.Widget_Material3_SideSheet;
    public int Q;
    public final LinkedHashSet R;
    public final f S;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public a f23186a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final s f23187b;

    @Nullable
    private ColorStateList backgroundTint;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final r f23188c;

    @Nullable
    private WeakReference<View> coplanarSiblingViewRef;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final float f23189d;

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f23192g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final float f23193h;

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

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

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

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

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

    @Nullable
    private com.google.android.material.shape.k materialShapeDrawable;

    @Nullable
    private n9.k sideContainerBackHelper;

    @Nullable
    private VelocityTracker velocityTracker;

    @Nullable
    private h viewDragHelper;

    @Nullable
    private WeakReference<V> viewRef;

    public SideSheetBehavior() {
        this.f23188c = new r(this);
        this.f23190e = true;
        this.f23191f = 5;
        this.f23193h = 0.1f;
        this.f23198m = -1;
        this.R = new LinkedHashSet();
        this.S = new f(this, 1);
    }

    @Nullable
    private ValueAnimator.AnimatorUpdateListener getCoplanarFinishAnimatorUpdateListener() {
        final ViewGroup.MarginLayoutParams marginLayoutParams;
        final int i10;
        final View coplanarSiblingView = getCoplanarSiblingView();
        if (coplanarSiblingView == null || (marginLayoutParams = (ViewGroup.MarginLayoutParams) coplanarSiblingView.getLayoutParams()) == null) {
            return null;
        }
        switch (this.f23186a.f33377a) {
            case 0:
                i10 = marginLayoutParams.leftMargin;
                break;
            default:
                i10 = marginLayoutParams.rightMargin;
                break;
        }
        return new ValueAnimator.AnimatorUpdateListener() { // from class: u9.b
            @Override // android.animation.ValueAnimator.AnimatorUpdateListener
            public final void onAnimationUpdate(ValueAnimator valueAnimator) {
                a aVar = this.f33379a.f23186a;
                int iC = y8.a.c(valueAnimator.getAnimatedFraction(), i10, 0);
                int i11 = aVar.f33377a;
                ViewGroup.MarginLayoutParams marginLayoutParams2 = marginLayoutParams;
                switch (i11) {
                    case 0:
                        marginLayoutParams2.leftMargin = iC;
                        break;
                    default:
                        marginLayoutParams2.rightMargin = iC;
                        break;
                }
                coplanarSiblingView.requestLayout();
            }
        };
    }

    @Nullable
    private d getViewLayoutParams() {
        V v10;
        WeakReference<V> weakReference = this.viewRef;
        if (weakReference == null || (v10 = weakReference.get()) == null || !(v10.getLayoutParams() instanceof d)) {
            return null;
        }
        return (d) v10.getLayoutParams();
    }

    public static /* synthetic */ void w(SideSheetBehavior sideSheetBehavior, int i10) {
        V v10 = sideSheetBehavior.viewRef.get();
        if (v10 != null) {
            sideSheetBehavior.C(v10, i10, false);
        }
    }

    public final void A(int i10) {
        V v10;
        if (this.f23191f == i10) {
            return;
        }
        this.f23191f = i10;
        WeakReference<V> weakReference = this.viewRef;
        if (weakReference == null || (v10 = weakReference.get()) == null) {
            return;
        }
        int i11 = this.f23191f == 5 ? 4 : 0;
        if (v10.getVisibility() != i11) {
            v10.setVisibility(i11);
        }
        Iterator it = this.R.iterator();
        if (it.hasNext()) {
            u1.x(it.next());
            throw null;
        }
        D();
    }

    public final boolean B() {
        return this.viewDragHelper != null && (this.f23190e || this.f23191f == 1);
    }

    public final void C(View view, int i10, boolean z10) {
        int iF;
        if (i10 == 3) {
            iF = this.f23186a.F();
        } else {
            if (i10 != 5) {
                throw new IllegalArgumentException(u1.g("Invalid state to get outer edge offset: ", i10));
            }
            iF = this.f23186a.G();
        }
        h viewDragHelper = getViewDragHelper();
        if (viewDragHelper == null || (!z10 ? viewDragHelper.s(view, iF, view.getTop()) : viewDragHelper.q(iF, view.getTop()))) {
            A(i10);
        } else {
            A(2);
            this.f23188c.b(i10);
        }
    }

    public final void D() {
        V v10;
        WeakReference<V> weakReference = this.viewRef;
        if (weakReference == null || (v10 = weakReference.get()) == null) {
            return;
        }
        s1.g(262144, v10);
        s1.e(0, v10);
        s1.g(ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES, v10);
        s1.e(0, v10);
        int i10 = 2;
        int i11 = 5;
        if (this.f23191f != 5) {
            s1.replaceAccessibilityAction(v10, c.f32066l, null, new p(i11, i10, this));
        }
        int i12 = 3;
        if (this.f23191f != 3) {
            s1.replaceAccessibilityAction(v10, c.f32064j, null, new p(i12, i10, this));
        }
    }

    @Override // n9.b
    public final void a(androidx.activity.b bVar) {
        n9.k kVar = this.sideContainerBackHelper;
        if (kVar == null) {
            return;
        }
        kVar.a(bVar);
    }

    @Override // n9.b
    public final void b(androidx.activity.b bVar) {
        ViewGroup.MarginLayoutParams marginLayoutParams;
        n9.k kVar = this.sideContainerBackHelper;
        if (kVar == null) {
            return;
        }
        a aVar = this.f23186a;
        int i10 = 5;
        if (aVar != null) {
            switch (aVar.f33377a) {
                case 0:
                    i10 = 3;
                    break;
            }
        }
        kVar.d(bVar, i10);
        WeakReference<V> weakReference = this.viewRef;
        if (weakReference == null || weakReference.get() == null) {
            return;
        }
        V v10 = this.viewRef.get();
        View coplanarSiblingView = getCoplanarSiblingView();
        if (coplanarSiblingView == null || (marginLayoutParams = (ViewGroup.MarginLayoutParams) coplanarSiblingView.getLayoutParams()) == null) {
            return;
        }
        int scaleX = (int) ((v10.getScaleX() * this.f23194i) + this.f23197l);
        switch (this.f23186a.f33377a) {
            case 0:
                marginLayoutParams.leftMargin = scaleX;
                break;
            default:
                marginLayoutParams.rightMargin = scaleX;
                break;
        }
        coplanarSiblingView.requestLayout();
    }

    @Override // n9.b
    public final void c() {
        n9.k kVar = this.sideContainerBackHelper;
        if (kVar == null) {
            return;
        }
        androidx.activity.b bVarOnHandleBackInvoked = kVar.onHandleBackInvoked();
        int i10 = 5;
        if (bVarOnHandleBackInvoked == null || Build.VERSION.SDK_INT < 34) {
            z(5);
            return;
        }
        n9.k kVar2 = this.sideContainerBackHelper;
        a aVar = this.f23186a;
        if (aVar != null) {
            switch (aVar.f33377a) {
                case 0:
                    i10 = 3;
                    break;
            }
        }
        kVar2.finishBackProgress(bVarOnHandleBackInvoked, i10, new e(this, 10), getCoplanarFinishAnimatorUpdateListener());
    }

    @Override // n9.b
    public final void d() {
        n9.k kVar = this.sideContainerBackHelper;
        if (kVar == null) {
            return;
        }
        kVar.b();
    }

    @Override // d0.b
    public final void g(d dVar) {
        this.viewRef = null;
        this.viewDragHelper = null;
        this.sideContainerBackHelper = null;
    }

    @Nullable
    @VisibleForTesting
    public n9.k getBackHelper() {
        return this.sideContainerBackHelper;
    }

    @Nullable
    public View getCoplanarSiblingView() {
        WeakReference<View> weakReference = this.coplanarSiblingViewRef;
        if (weakReference != null) {
            return weakReference.get();
        }
        return null;
    }

    @Nullable
    public h getViewDragHelper() {
        return this.viewDragHelper;
    }

    @Override // d0.b
    public final void j() {
        this.viewRef = null;
        this.viewDragHelper = null;
        this.sideContainerBackHelper = null;
    }

    @Override // d0.b
    public final boolean k(CoordinatorLayout coordinatorLayout, View view, MotionEvent motionEvent) {
        h hVar;
        VelocityTracker velocityTracker;
        if ((!view.isShown() && s1.getAccessibilityPaneTitle(view) == null) || !this.f23190e) {
            this.f23192g = true;
            return false;
        }
        int actionMasked = motionEvent.getActionMasked();
        if (actionMasked == 0 && (velocityTracker = this.velocityTracker) != null) {
            velocityTracker.recycle();
            this.velocityTracker = null;
        }
        if (this.velocityTracker == null) {
            this.velocityTracker = VelocityTracker.obtain();
        }
        this.velocityTracker.addMovement(motionEvent);
        if (actionMasked == 0) {
            this.Q = (int) motionEvent.getX();
        } else if ((actionMasked == 1 || actionMasked == 3) && this.f23192g) {
            this.f23192g = false;
            return false;
        }
        return (this.f23192g || (hVar = this.viewDragHelper) == null || !hVar.r(motionEvent)) ? false : true;
    }

    /* JADX WARN: Removed duplicated region for block: B:42:0x0093  */
    @Override // d0.b
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean l(CoordinatorLayout coordinatorLayout, View view, int i10) {
        d viewLayoutParams;
        d viewLayoutParams2;
        int right;
        int left;
        int i11;
        int right2;
        int i12;
        View viewFindViewById;
        int i13;
        int i14 = 1;
        if (coordinatorLayout.getFitsSystemWindows() && !view.getFitsSystemWindows()) {
            view.setFitsSystemWindows(true);
        }
        int iG = 0;
        if (this.viewRef == null) {
            this.viewRef = new WeakReference<>(view);
            this.sideContainerBackHelper = new n9.k(view);
            com.google.android.material.shape.k kVar = this.materialShapeDrawable;
            if (kVar != null) {
                view.setBackground(kVar);
                com.google.android.material.shape.k kVar2 = this.materialShapeDrawable;
                float elevation = this.f23189d;
                if (elevation == -1.0f) {
                    elevation = view.getElevation();
                }
                kVar2.w(elevation);
            } else {
                ColorStateList colorStateList = this.backgroundTint;
                if (colorStateList != null) {
                    s1.setBackgroundTintList(view, colorStateList);
                }
            }
            int i15 = this.f23191f == 5 ? 4 : 0;
            if (view.getVisibility() != i15) {
                view.setVisibility(i15);
            }
            D();
            if (view.getImportantForAccessibility() == 0) {
                view.setImportantForAccessibility(1);
            }
            if (s1.getAccessibilityPaneTitle(view) == null) {
                s1.setAccessibilityPaneTitle(view, view.getResources().getString(T));
            }
        }
        int i16 = Gravity.getAbsoluteGravity(((d) view.getLayoutParams()).f24886c, i10) == 3 ? 1 : 0;
        a aVar = this.f23186a;
        if (aVar != null) {
            switch (aVar.f33377a) {
                case 0:
                    i13 = 1;
                    break;
                default:
                    i13 = 0;
                    break;
            }
            if (i13 != i16) {
                s sVar = this.f23187b;
                if (i16 == 0) {
                    this.f23186a = new a(this, i14);
                    if (sVar != null && ((viewLayoutParams2 = getViewLayoutParams()) == null || ((ViewGroup.MarginLayoutParams) viewLayoutParams2).rightMargin <= 0)) {
                        q qVarH = sVar.h();
                        qVarH.g(0.0f);
                        qVarH.e(0.0f);
                        s sVarA = qVarH.a();
                        com.google.android.material.shape.k kVar3 = this.materialShapeDrawable;
                        if (kVar3 != null) {
                            kVar3.setShapeAppearanceModel(sVarA);
                        }
                    }
                } else {
                    if (i16 != 1) {
                        throw new IllegalArgumentException(defpackage.e.e("Invalid sheet edge position value: ", i16, ". Must be 0 or 1."));
                    }
                    this.f23186a = new a(this, iG);
                    if (sVar != null && ((viewLayoutParams = getViewLayoutParams()) == null || ((ViewGroup.MarginLayoutParams) viewLayoutParams).leftMargin <= 0)) {
                        q qVarH2 = sVar.h();
                        qVarH2.f(0.0f);
                        qVarH2.d(0.0f);
                        s sVarA2 = qVarH2.a();
                        com.google.android.material.shape.k kVar4 = this.materialShapeDrawable;
                        if (kVar4 != null) {
                            kVar4.setShapeAppearanceModel(sVarA2);
                        }
                    }
                }
            }
        }
        if (this.viewDragHelper == null) {
            this.viewDragHelper = new h(coordinatorLayout.getContext(), coordinatorLayout, this.S);
        }
        a aVar2 = this.f23186a;
        int i17 = aVar2.f33377a;
        SideSheetBehavior sideSheetBehavior = aVar2.f33378b;
        switch (i17) {
            case 0:
                right = view.getRight() + sideSheetBehavior.f23197l;
                break;
            default:
                right = view.getLeft() - sideSheetBehavior.f23197l;
                break;
        }
        coordinatorLayout.p(i10, view);
        this.f23195j = coordinatorLayout.getWidth();
        switch (this.f23186a.f33377a) {
            case 0:
                left = coordinatorLayout.getLeft();
                break;
            default:
                left = coordinatorLayout.getRight();
                break;
        }
        this.f23196k = left;
        this.f23194i = view.getWidth();
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        if (marginLayoutParams != null) {
            switch (this.f23186a.f33377a) {
                case 0:
                    i11 = marginLayoutParams.leftMargin;
                    break;
                default:
                    i11 = marginLayoutParams.rightMargin;
                    break;
            }
        } else {
            i11 = 0;
        }
        this.f23197l = i11;
        int i18 = this.f23191f;
        if (i18 == 1 || i18 == 2) {
            a aVar3 = this.f23186a;
            int i19 = aVar3.f33377a;
            SideSheetBehavior sideSheetBehavior2 = aVar3.f33378b;
            switch (i19) {
                case 0:
                    right2 = view.getRight() + sideSheetBehavior2.f23197l;
                    break;
                default:
                    right2 = view.getLeft() - sideSheetBehavior2.f23197l;
                    break;
            }
            iG = right - right2;
        } else if (i18 != 3) {
            if (i18 != 5) {
                throw new IllegalStateException("Unexpected value: " + this.f23191f);
            }
            iG = this.f23186a.G();
        }
        WeakHashMap weakHashMap = s1.f1815a;
        view.offsetLeftAndRight(iG);
        if (this.coplanarSiblingViewRef == null && (i12 = this.f23198m) != -1 && (viewFindViewById = coordinatorLayout.findViewById(i12)) != null) {
            this.coplanarSiblingViewRef = new WeakReference<>(viewFindViewById);
        }
        Iterator it = this.R.iterator();
        while (it.hasNext()) {
            u1.x(it.next());
        }
        return true;
    }

    @Override // d0.b
    public final boolean m(CoordinatorLayout coordinatorLayout, View view, int i10, int i11, int i12) {
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        view.measure(ViewGroup.getChildMeasureSpec(i10, coordinatorLayout.getPaddingRight() + coordinatorLayout.getPaddingLeft() + marginLayoutParams.leftMargin + marginLayoutParams.rightMargin + i11, marginLayoutParams.width), ViewGroup.getChildMeasureSpec(i12, coordinatorLayout.getPaddingBottom() + coordinatorLayout.getPaddingTop() + marginLayoutParams.topMargin + marginLayoutParams.bottomMargin, marginLayoutParams.height));
        return true;
    }

    @Override // d0.b
    public final void r(View view, Parcelable parcelable) {
        u9.d dVar = (u9.d) parcelable;
        if (dVar.getSuperState() != null) {
            dVar.getSuperState();
        }
        int i10 = dVar.f33383c;
        if (i10 == 1 || i10 == 2) {
            i10 = 5;
        }
        this.f23191f = i10;
    }

    @Override // d0.b
    public final Parcelable s(View view) {
        return new u9.d(View.BaseSavedState.EMPTY_STATE, this);
    }

    public void setCoplanarSiblingView(@Nullable View view) {
        this.f23198m = -1;
        if (view == null) {
            WeakReference<View> weakReference = this.coplanarSiblingViewRef;
            if (weakReference != null) {
                weakReference.clear();
            }
            this.coplanarSiblingViewRef = null;
            return;
        }
        this.coplanarSiblingViewRef = new WeakReference<>(view);
        WeakReference<V> weakReference2 = this.viewRef;
        if (weakReference2 != null) {
            V v10 = weakReference2.get();
            if (v10.isLaidOut()) {
                v10.requestLayout();
            }
        }
    }

    @Override // d0.b
    public final boolean v(CoordinatorLayout coordinatorLayout, View view, MotionEvent motionEvent) {
        VelocityTracker velocityTracker;
        if (!view.isShown()) {
            return false;
        }
        int actionMasked = motionEvent.getActionMasked();
        if (this.f23191f == 1 && actionMasked == 0) {
            return true;
        }
        if (B()) {
            this.viewDragHelper.k(motionEvent);
        }
        if (actionMasked == 0 && (velocityTracker = this.velocityTracker) != null) {
            velocityTracker.recycle();
            this.velocityTracker = null;
        }
        if (this.velocityTracker == null) {
            this.velocityTracker = VelocityTracker.obtain();
        }
        this.velocityTracker.addMovement(motionEvent);
        if (B() && actionMasked == 2 && !this.f23192g && B()) {
            float fAbs = Math.abs(this.Q - motionEvent.getX());
            h hVar = this.viewDragHelper;
            if (fAbs > hVar.f36679b) {
                hVar.c(motionEvent.getPointerId(motionEvent.getActionIndex()), view);
            }
        }
        return !this.f23192g;
    }

    public final void z(int i10) {
        if (i10 != 1) {
            int i11 = 2;
            if (i10 != 2) {
                WeakReference<V> weakReference = this.viewRef;
                if (weakReference == null || weakReference.get() == null) {
                    A(i10);
                    return;
                }
                V v10 = this.viewRef.get();
                o oVar = new o(i10, i11, this);
                ViewParent parent = v10.getParent();
                if (parent != null && parent.isLayoutRequested() && v10.isAttachedToWindow()) {
                    v10.post(oVar);
                    return;
                } else {
                    oVar.run();
                    return;
                }
            }
        }
        throw new IllegalArgumentException(defpackage.e.o(new StringBuilder("STATE_"), i10 == 1 ? "DRAGGING" : "SETTLING", " should not be set externally."));
    }

    public SideSheetBehavior(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        this.f23188c = new r(this);
        this.f23190e = true;
        this.f23191f = 5;
        this.f23193h = 0.1f;
        this.f23198m = -1;
        this.R = new LinkedHashSet();
        this.S = new f(this, 1);
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, m.SideSheetBehavior_Layout);
        int i10 = m.SideSheetBehavior_Layout_backgroundTint;
        if (typedArrayObtainStyledAttributes.hasValue(i10)) {
            this.backgroundTint = q9.d.getColorStateList(context, typedArrayObtainStyledAttributes, i10);
        }
        if (typedArrayObtainStyledAttributes.hasValue(m.SideSheetBehavior_Layout_shapeAppearance)) {
            this.f23187b = s.c(context, attributeSet, 0, U).a();
        }
        int i11 = m.SideSheetBehavior_Layout_coplanarSiblingViewId;
        if (typedArrayObtainStyledAttributes.hasValue(i11)) {
            int resourceId = typedArrayObtainStyledAttributes.getResourceId(i11, -1);
            this.f23198m = resourceId;
            WeakReference<View> weakReference = this.coplanarSiblingViewRef;
            if (weakReference != null) {
                weakReference.clear();
            }
            this.coplanarSiblingViewRef = null;
            WeakReference<V> weakReference2 = this.viewRef;
            if (weakReference2 != null) {
                V v10 = weakReference2.get();
                if (resourceId != -1 && v10.isLaidOut()) {
                    v10.requestLayout();
                }
            }
        }
        s sVar = this.f23187b;
        if (sVar != null) {
            com.google.android.material.shape.k kVar = new com.google.android.material.shape.k(sVar);
            this.materialShapeDrawable = kVar;
            kVar.s(context);
            ColorStateList colorStateList = this.backgroundTint;
            if (colorStateList != null) {
                this.materialShapeDrawable.setFillColor(colorStateList);
            } else {
                TypedValue typedValue = new TypedValue();
                context.getTheme().resolveAttribute(R.attr.colorBackground, typedValue, true);
                this.materialShapeDrawable.setTint(typedValue.data);
            }
        }
        this.f23189d = typedArrayObtainStyledAttributes.getDimension(m.SideSheetBehavior_Layout_android_elevation, -1.0f);
        this.f23190e = typedArrayObtainStyledAttributes.getBoolean(m.SideSheetBehavior_Layout_behavior_draggable, true);
        typedArrayObtainStyledAttributes.recycle();
        ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
    }
}
