package com.google.android.material.appbar;

import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.f3;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.search.SearchBar;
import java.util.List;
import kotlinx.coroutines.y;

/* JADX INFO: loaded from: classes2.dex */
public abstract class q extends r {

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

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

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

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

    public q() {
        this.f22213c = new Rect();
        this.f22214d = new Rect();
        this.f22215e = 0;
    }

    @Nullable
    public abstract View findFirstDependency(List<View> list);

    @Override // d0.b
    public final boolean m(CoordinatorLayout coordinatorLayout, View view, int i10, int i11, int i12) {
        View viewFindFirstDependency;
        f3 lastWindowInsets;
        int i13 = view.getLayoutParams().height;
        if ((i13 != -1 && i13 != -2) || (viewFindFirstDependency = findFirstDependency(coordinatorLayout.i(view))) == null) {
            return false;
        }
        int size = View.MeasureSpec.getSize(i12);
        if (size <= 0) {
            size = coordinatorLayout.getHeight();
        } else if (viewFindFirstDependency.getFitsSystemWindows() && (lastWindowInsets = coordinatorLayout.getLastWindowInsets()) != null) {
            size += lastWindowInsets.a() + lastWindowInsets.d();
        }
        int totalScrollRange = size + (viewFindFirstDependency instanceof AppBarLayout ? ((AppBarLayout) viewFindFirstDependency).getTotalScrollRange() : viewFindFirstDependency.getMeasuredHeight());
        int measuredHeight = viewFindFirstDependency.getMeasuredHeight();
        if (this instanceof SearchBar.ScrollingViewBehavior) {
            view.setTranslationY(-measuredHeight);
        } else {
            view.setTranslationY(0.0f);
            totalScrollRange -= measuredHeight;
        }
        coordinatorLayout.q(view, i10, i11, View.MeasureSpec.makeMeasureSpec(totalScrollRange, i13 == -1 ? 1073741824 : Integer.MIN_VALUE));
        return true;
    }

    @Override // com.google.android.material.appbar.r
    public final void x(CoordinatorLayout coordinatorLayout, View view, int i10) {
        View viewFindFirstDependency = findFirstDependency(coordinatorLayout.i(view));
        if (viewFindFirstDependency == null) {
            coordinatorLayout.p(i10, view);
            this.f22215e = 0;
            return;
        }
        d0.d dVar = (d0.d) view.getLayoutParams();
        int paddingLeft = coordinatorLayout.getPaddingLeft() + ((ViewGroup.MarginLayoutParams) dVar).leftMargin;
        int bottom = viewFindFirstDependency.getBottom() + ((ViewGroup.MarginLayoutParams) dVar).topMargin;
        int width = (coordinatorLayout.getWidth() - coordinatorLayout.getPaddingRight()) - ((ViewGroup.MarginLayoutParams) dVar).rightMargin;
        int bottom2 = ((viewFindFirstDependency.getBottom() + coordinatorLayout.getHeight()) - coordinatorLayout.getPaddingBottom()) - ((ViewGroup.MarginLayoutParams) dVar).bottomMargin;
        Rect rect = this.f22213c;
        rect.set(paddingLeft, bottom, width, bottom2);
        f3 lastWindowInsets = coordinatorLayout.getLastWindowInsets();
        if (lastWindowInsets != null && coordinatorLayout.getFitsSystemWindows() && !view.getFitsSystemWindows()) {
            rect.left = lastWindowInsets.b() + rect.left;
            rect.right -= lastWindowInsets.c();
        }
        Rect rect2 = this.f22214d;
        int i11 = dVar.f24886c;
        if (i11 == 0) {
            i11 = 8388659;
        }
        Gravity.apply(i11, view.getMeasuredWidth(), view.getMeasuredHeight(), rect, rect2, i10);
        int iY = y(viewFindFirstDependency);
        view.layout(rect2.left, rect2.top - iY, rect2.right, rect2.bottom - iY);
        this.f22215e = rect2.top - viewFindFirstDependency.getBottom();
    }

    public final int y(View view) {
        int i10;
        if (this.f22216f == 0) {
            return 0;
        }
        float f10 = 0.0f;
        if (view instanceof AppBarLayout) {
            AppBarLayout appBarLayout = (AppBarLayout) view;
            int totalScrollRange = appBarLayout.getTotalScrollRange();
            int downNestedPreScrollRange = appBarLayout.getDownNestedPreScrollRange();
            d0.b bVar = ((d0.d) appBarLayout.getLayoutParams()).f24884a;
            int iZ = bVar instanceof AppBarLayout.BaseBehavior ? ((AppBarLayout.BaseBehavior) bVar).z() : 0;
            if ((downNestedPreScrollRange == 0 || totalScrollRange + iZ > downNestedPreScrollRange) && (i10 = totalScrollRange - downNestedPreScrollRange) != 0) {
                f10 = (iZ / i10) + 1.0f;
            }
        }
        int i11 = this.f22216f;
        return y.h((int) (f10 * i11), 0, i11);
    }

    public q(int i10) {
        super(0);
        this.f22213c = new Rect();
        this.f22214d = new Rect();
        this.f22215e = 0;
    }
}
