package androidx.appcompat.widget;

import X.AnonymousClass030;
import X.AnonymousClass031;
import X.AnonymousClass039;
import X.AnonymousClass190;
import X.AnonymousClass192;
import X.AnonymousClass193;
import X.AnonymousClass194;
import X.AnonymousClass196;
import X.AnonymousClass198;
import X.C005502b;
import X.C020307t;
import X.C020607w;
import X.C276818k;
import X.C277218o;
import X.C277518r;
import X.C278318z;
import X.C38991gf;
import X.C39051gl;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.bytedance.covode.number.Covode;
import java.util.ArrayList;

public class ActionMenuPresenter extends C276818k implements C020307t {
    public AnonymousClass192 LJIIIIZZ;
    public Drawable LJIIIZ;
    public boolean LJIIJ;
    public boolean LJIIJJI;
    public boolean LJIIL;
    public AnonymousClass193 LJIILIIL;
    public C278318z LJIILJJIL;
    public AnonymousClass039 LJIILL;
    public final AnonymousClass194 LJIILLIIL = new AnonymousClass194(this);
    public int LJIIZILJ;
    public boolean LJIJ;
    public int LJIJI;
    public int LJIJJ;
    public int LJIJJLI;
    public boolean LJIL;
    public boolean LJJ;
    public boolean LJJI;
    public int LJJIFFI;
    public final SparseBooleanArray LJJII = new SparseBooleanArray();
    public View LJJIII;
    public AnonymousClass190 LJJIIJ;

    static {
        Covode.recordClassIndex(398);
    }

    public static class SavedState implements Parcelable {
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
            static {
                Covode.recordClassIndex(400);
            }

            public final /* bridge */ /* synthetic */ Object[] newArray(int i) {
                return new SavedState[i];
            }

            public final /* synthetic */ Object createFromParcel(Parcel parcel) {
                return new SavedState(parcel);
            }
        };
        public int LIZ;

        public int describeContents() {
            return 0;
        }

        public SavedState() {
        }

        static {
            Covode.recordClassIndex(399);
        }

        public SavedState(Parcel parcel) {
            this.LIZ = parcel.readInt();
        }

        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(this.LIZ);
        }
    }

    public final boolean LIZ(C277518r r2) {
        return r2.LJFF();
    }

    public final boolean LIZ(ViewGroup viewGroup, int i) {
        if (viewGroup.getChildAt(i) == this.LJIIIIZZ) {
            return false;
        }
        return super.LIZ(viewGroup, i);
    }

    public final boolean LIZ(C39051gl r8) {
        boolean z = false;
        if (!r8.hasVisibleItems()) {
            return false;
        }
        C39051gl r0 = r8;
        while (r0.LJIILIIL != this.LIZJ) {
            r0 = (C39051gl) r0.LJIILIIL;
        }
        MenuItem item = r0.getItem();
        ViewGroup viewGroup = (ViewGroup) this.LJI;
        if (viewGroup != null) {
            int childCount = viewGroup.getChildCount();
            int i = 0;
            while (true) {
                if (i >= childCount) {
                    break;
                }
                View childAt = viewGroup.getChildAt(i);
                if (!(childAt instanceof AnonymousClass030) || ((AnonymousClass030) childAt).getItemData() != item) {
                    i++;
                } else if (childAt != null) {
                    this.LJIIZILJ = r8.getItem().getItemId();
                    int size = r8.size();
                    int i2 = 0;
                    while (true) {
                        if (i2 >= size) {
                            break;
                        }
                        MenuItem item2 = r8.getItem(i2);
                        if (item2.isVisible() && item2.getIcon() != null) {
                            z = true;
                            break;
                        }
                        i2++;
                    }
                    C278318z r1 = new C278318z(this, this.LIZIZ, r8, childAt);
                    this.LJIILJJIL = r1;
                    r1.LIZ(z);
                    if (this.LJIILJJIL.LIZIZ()) {
                        super.LIZ(r8);
                        return true;
                    }
                    throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
                }
            }
        }
        return false;
    }

    public final void LIZLLL() {
        this.LJIIJJI = true;
        this.LJIJ = true;
    }

    public final Parcelable LJFF() {
        SavedState savedState = new SavedState();
        savedState.LIZ = this.LJIIZILJ;
        return savedState;
    }

    public final boolean LJII() {
        return LJI() | LJIIIIZZ();
    }

    public final boolean LJIIIIZZ() {
        C278318z r0 = this.LJIILJJIL;
        if (r0 == null) {
            return false;
        }
        r0.LIZJ();
        return true;
    }

    public final boolean LJIIIZ() {
        AnonymousClass193 r0 = this.LJIILIIL;
        if (r0 == null || !r0.LJ()) {
            return false;
        }
        return true;
    }

    public final void LIZJ() {
        if (!this.LJIL) {
            this.LJIJJLI = C005502b.LIZ(this.LIZIZ).LIZ();
        }
        if (this.LIZJ != null) {
            this.LIZJ.LIZIZ(true);
        }
    }

    public final boolean LJI() {
        if (this.LJIILL == null || this.LJI == null) {
            AnonymousClass193 r0 = this.LJIILIIL;
            if (r0 == null) {
                return false;
            }
            r0.LIZJ();
            return true;
        }
        ((View) this.LJI).removeCallbacks(this.LJIILL);
        this.LJIILL = null;
        return true;
    }

    public final boolean LJ() {
        if (!this.LJIIJJI || LJIIIZ() || this.LIZJ == null || this.LJI == null || this.LJIILL != null || this.LIZJ.LJIIJ().isEmpty()) {
            return false;
        }
        this.LJIILL = new AnonymousClass039(this, new AnonymousClass193(this, this.LIZIZ, this.LIZJ, this.LJIIIIZZ));
        ((View) this.LJI).post(this.LJIILL);
        super.LIZ((C39051gl) null);
        return true;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:94:0x0137, code lost:
        if (r3 == false) goto L_0x0101;
     */
    /* JADX WARNING: Removed duplicated region for block: B:85:0x0113  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final boolean LIZ() {
        /*
            r18 = this;
            r1 = r18
            X.18o r0 = r1.LIZJ
            r3 = 0
            if (r0 == 0) goto L_0x004a
            X.18o r0 = r1.LIZJ
            java.util.ArrayList r14 = r0.LJIIIIZZ()
            int r13 = r14.size()
        L_0x0011:
            int r12 = r1.LJIJJLI
            int r11 = r1.LJIJJ
            int r10 = android.view.View.MeasureSpec.makeMeasureSpec(r3, r3)
            X.031 r9 = r1.LJI
            android.view.ViewGroup r9 = (android.view.ViewGroup) r9
            r6 = 0
            r7 = 0
            r5 = 0
            r4 = 0
        L_0x0021:
            if (r6 >= r13) goto L_0x004d
            java.lang.Object r2 = r14.get(r6)
            X.18r r2 = (X.C277518r) r2
            boolean r0 = r2.LJII()
            if (r0 == 0) goto L_0x003f
            int r5 = r5 + 1
        L_0x0031:
            boolean r0 = r1.LJIIL
            if (r0 == 0) goto L_0x003c
            boolean r0 = r2.isActionViewExpanded()
            if (r0 == 0) goto L_0x003c
            r12 = 0
        L_0x003c:
            int r6 = r6 + 1
            goto L_0x0021
        L_0x003f:
            boolean r0 = r2.LJI()
            if (r0 == 0) goto L_0x0048
            int r4 = r4 + 1
            goto L_0x0031
        L_0x0048:
            r7 = 1
            goto L_0x0031
        L_0x004a:
            r14 = 0
            r13 = 0
            goto L_0x0011
        L_0x004d:
            boolean r0 = r1.LJIIJJI
            if (r0 == 0) goto L_0x0058
            if (r7 != 0) goto L_0x0056
            int r4 = r4 + r5
            if (r4 <= r12) goto L_0x0058
        L_0x0056:
            int r12 = r12 + -1
        L_0x0058:
            int r12 = r12 - r5
            android.util.SparseBooleanArray r8 = r1.LJJII
            r8.clear()
            boolean r0 = r1.LJJ
            if (r0 == 0) goto L_0x0140
            int r7 = r1.LJJIFFI
            int r6 = r11 / r7
            int r0 = r11 % r7
            int r0 = r0 / r6
            int r7 = r7 + r0
        L_0x006a:
            r5 = 0
            r17 = 0
        L_0x006d:
            if (r5 >= r13) goto L_0x0144
            java.lang.Object r4 = r14.get(r5)
            X.18r r4 = (X.C277518r) r4
            boolean r0 = r4.LJII()
            if (r0 == 0) goto L_0x00b0
            android.view.View r0 = r1.LJJIII
            android.view.View r2 = r1.LIZ(r4, r0, r9)
            android.view.View r0 = r1.LJJIII
            if (r0 != 0) goto L_0x0087
            r1.LJJIII = r2
        L_0x0087:
            boolean r0 = r1.LJJ
            if (r0 == 0) goto L_0x00ac
            int r0 = X.AnonymousClass198.LIZ(r2, r7, r6, r10, r3)
            int r6 = r6 - r0
        L_0x0090:
            int r0 = r2.getMeasuredWidth()
            int r11 = r11 - r0
            if (r17 != 0) goto L_0x0099
            r17 = r0
        L_0x0099:
            int r1 = r4.getGroupId()
            r0 = 1
            if (r1 == 0) goto L_0x00a3
            r8.put(r1, r0)
        L_0x00a3:
            r4.LIZJ(r0)
        L_0x00a6:
            int r5 = r5 + 1
            r1 = r18
            r3 = 0
            goto L_0x006d
        L_0x00ac:
            r2.measure(r10, r10)
            goto L_0x0090
        L_0x00b0:
            boolean r0 = r4.LJI()
            if (r0 == 0) goto L_0x013a
            int r15 = r4.getGroupId()
            boolean r16 = r8.get(r15)
            if (r12 > 0) goto L_0x00c2
            if (r16 == 0) goto L_0x0110
        L_0x00c2:
            if (r11 <= 0) goto L_0x0110
            boolean r0 = r1.LJJ
            if (r0 == 0) goto L_0x00ca
            if (r6 <= 0) goto L_0x0110
        L_0x00ca:
            r3 = 1
            if (r3 == 0) goto L_0x0111
            android.view.View r0 = r1.LJJIII
            android.view.View r2 = r1.LIZ(r4, r0, r9)
            android.view.View r0 = r1.LJJIII
            if (r0 != 0) goto L_0x00d9
            r1.LJJIII = r2
        L_0x00d9:
            boolean r0 = r1.LJJ
            if (r0 == 0) goto L_0x010c
            r0 = 0
            int r0 = X.AnonymousClass198.LIZ(r2, r7, r6, r10, r0)
            int r6 = r6 - r0
            if (r0 != 0) goto L_0x00e6
            r3 = 0
        L_0x00e6:
            int r0 = r2.getMeasuredWidth()
            int r11 = r11 - r0
            if (r17 != 0) goto L_0x00ef
            r17 = r0
        L_0x00ef:
            boolean r0 = r1.LJJ
            if (r0 == 0) goto L_0x0105
            if (r11 < 0) goto L_0x010a
        L_0x00f5:
            r0 = 1
        L_0x00f6:
            r3 = r3 & r0
            if (r3 == 0) goto L_0x0111
            if (r15 == 0) goto L_0x0111
            r0 = 1
            r8.put(r15, r0)
        L_0x00ff:
            int r12 = r12 + -1
        L_0x0101:
            r4.LIZJ(r3)
            goto L_0x00a6
        L_0x0105:
            int r0 = r11 + r17
            if (r0 <= 0) goto L_0x010a
            goto L_0x00f5
        L_0x010a:
            r0 = 0
            goto L_0x00f6
        L_0x010c:
            r2.measure(r10, r10)
            goto L_0x00e6
        L_0x0110:
            r3 = 0
        L_0x0111:
            if (r16 == 0) goto L_0x0137
            r0 = 0
            r8.put(r15, r0)
            r2 = 0
        L_0x0118:
            if (r2 >= r5) goto L_0x0137
            java.lang.Object r1 = r14.get(r2)
            X.18r r1 = (X.C277518r) r1
            int r0 = r1.getGroupId()
            if (r0 != r15) goto L_0x0132
            boolean r0 = r1.LJFF()
            if (r0 == 0) goto L_0x012e
            int r12 = r12 + 1
        L_0x012e:
            r0 = 0
            r1.LIZJ(r0)
        L_0x0132:
            int r2 = r2 + 1
            r0 = r18
            goto L_0x0118
        L_0x0137:
            if (r3 == 0) goto L_0x0101
            goto L_0x00ff
        L_0x013a:
            r0 = 0
            r4.LIZJ(r0)
            goto L_0x00a6
        L_0x0140:
            r7 = 0
            r6 = 0
            goto L_0x006a
        L_0x0144:
            r0 = 1
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.appcompat.widget.ActionMenuPresenter.LIZ():boolean");
    }

    public final AnonymousClass031 LIZ(ViewGroup viewGroup) {
        AnonymousClass031 r0 = this.LJI;
        AnonymousClass031 LIZ = super.LIZ(viewGroup);
        if (r0 != LIZ) {
            ((AnonymousClass198) LIZ).setPresenter(this);
        }
        return LIZ;
    }

    public ActionMenuPresenter(Context context) {
        super(context);
    }

    public final void LIZIZ(boolean z) {
        if (z) {
            super.LIZ((C39051gl) null);
        } else if (this.LIZJ != null) {
            this.LIZJ.LIZ(false);
        }
    }

    public final void LIZ(AnonymousClass198 r2) {
        this.LJI = r2;
        r2.LIZ = this.LIZJ;
    }

    public final void LIZ(Parcelable parcelable) {
        MenuItem findItem;
        if (parcelable instanceof SavedState) {
            SavedState savedState = (SavedState) parcelable;
            if (savedState.LIZ > 0 && (findItem = this.LIZJ.findItem(savedState.LIZ)) != null) {
                LIZ((C39051gl) findItem.getSubMenu());
            }
        }
    }

    public final void LIZ(boolean z) {
        ArrayList<C277518r> arrayList;
        int size;
        super.LIZ(z);
        ((View) this.LJI).requestLayout();
        if (this.LIZJ != null) {
            C277218o r0 = this.LIZJ;
            r0.LJIIIZ();
            ArrayList<C277518r> arrayList2 = r0.LJ;
            int size2 = arrayList2.size();
            for (int i = 0; i < size2; i++) {
                C020607w r02 = arrayList2.get(i).LJFF;
                if (r02 != null) {
                    r02.LJFF = this;
                }
            }
        }
        if (this.LIZJ != null) {
            arrayList = this.LIZJ.LJIIJ();
        } else {
            arrayList = null;
        }
        if (!this.LJIIJJI || arrayList == null || ((size = arrayList.size()) != 1 ? size <= 0 : !(!arrayList.get(0).isActionViewExpanded()))) {
            AnonymousClass192 r03 = this.LJIIIIZZ;
            if (r03 != null && r03.getParent() == this.LJI) {
                ((ViewGroup) this.LJI).removeView(this.LJIIIIZZ);
            }
        } else {
            if (this.LJIIIIZZ == null) {
                this.LJIIIIZZ = new AnonymousClass192(this, this.LIZ);
            }
            ViewGroup viewGroup = (ViewGroup) this.LJIIIIZZ.getParent();
            if (viewGroup != this.LJI) {
                if (viewGroup != null) {
                    viewGroup.removeView(this.LJIIIIZZ);
                }
                AnonymousClass198 r2 = (AnonymousClass198) this.LJI;
                AnonymousClass192 r1 = this.LJIIIIZZ;
                AnonymousClass196 LIZ = r2.generateDefaultLayoutParams();
                LIZ.LIZ = true;
                r2.addView(r1, LIZ);
            }
        }
        ((AnonymousClass198) this.LJI).setOverflowReserved(this.LJIIJJI);
    }

    public final void LIZ(C277218o r1, boolean z) {
        LJII();
        super.LIZ(r1, z);
    }

    public final void LIZ(C277518r r2, AnonymousClass030 r3) {
        r3.LIZ(r2);
        C38991gf r32 = (C38991gf) r3;
        r32.setItemInvoker((AnonymousClass198) this.LJI);
        if (this.LJJIIJ == null) {
            this.LJJIIJ = new AnonymousClass190(this);
        }
        r32.setPopupCallback(this.LJJIIJ);
    }

    public final void LIZ(Context context, C277218o r8) {
        super.LIZ(context, r8);
        Resources resources = context.getResources();
        C005502b LIZ = C005502b.LIZ(context);
        if (!this.LJIJ) {
            this.LJIIJJI = true;
        }
        if (!this.LJJI) {
            this.LJIJI = LIZ.LIZ.getResources().getDisplayMetrics().widthPixels / 2;
        }
        if (!this.LJIL) {
            this.LJIJJLI = LIZ.LIZ();
        }
        int i = this.LJIJI;
        if (this.LJIIJJI) {
            if (this.LJIIIIZZ == null) {
                AnonymousClass192 r2 = new AnonymousClass192(this, this.LIZ);
                this.LJIIIIZZ = r2;
                if (this.LJIIJ) {
                    r2.setImageDrawable(this.LJIIIZ);
                    this.LJIIIZ = null;
                    this.LJIIJ = false;
                }
                int makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
                this.LJIIIIZZ.measure(makeMeasureSpec, makeMeasureSpec);
            }
            i -= this.LJIIIIZZ.getMeasuredWidth();
        } else {
            this.LJIIIIZZ = null;
        }
        this.LJIJJ = i;
        this.LJJIFFI = (int) (resources.getDisplayMetrics().density * 56.0f);
        this.LJJIII = null;
    }

    public final View LIZ(C277518r r4, View view, ViewGroup viewGroup) {
        int i;
        View actionView = r4.getActionView();
        if (actionView == null || r4.LJIIIZ()) {
            actionView = super.LIZ(r4, view, viewGroup);
        }
        if (r4.isActionViewExpanded()) {
            i = 8;
        } else {
            i = 0;
        }
        actionView.setVisibility(i);
        AnonymousClass198 r6 = (AnonymousClass198) viewGroup;
        ViewGroup.LayoutParams layoutParams = actionView.getLayoutParams();
        if (!r6.checkLayoutParams(layoutParams)) {
            actionView.setLayoutParams(r6.generateLayoutParams(layoutParams));
        }
        return actionView;
    }
}
