package androidx.constraintlayout.helper.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.VirtualLayout;
import androidx.constraintlayout.widget.c;
import androidx.constraintlayout.widget.s;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;

/* JADX INFO: loaded from: classes.dex */
public class Grid extends VirtualLayout {
    public int Q;
    public int R;
    public int S;
    public int T;
    public String U;
    public String V;
    public String W;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public String f1067a0;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public float f1068b0;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public float f1069c0;

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

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

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public boolean[][] f1072f0;

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    public final HashSet f1073g0;

    /* JADX INFO: renamed from: h0, reason: collision with root package name */
    public int[] f1074h0;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public View[] f1075l;

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

    public Grid(Context context) {
        super(context);
        this.f1071e0 = 0;
        this.f1073g0 = new HashSet();
    }

    public static int[][] C(String str) {
        String[] strArrSplit = str.split(",");
        int[][] iArr = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, strArrSplit.length, 3);
        for (int i10 = 0; i10 < strArrSplit.length; i10++) {
            String[] strArrSplit2 = strArrSplit[i10].trim().split(":");
            String[] strArrSplit3 = strArrSplit2[1].split("x");
            iArr[i10][0] = Integer.parseInt(strArrSplit2[0]);
            iArr[i10][1] = Integer.parseInt(strArrSplit3[0]);
            iArr[i10][2] = Integer.parseInt(strArrSplit3[1]);
        }
        return iArr;
    }

    public static float[] D(int i10, String str) {
        float[] fArr = null;
        if (str != null && !str.trim().isEmpty()) {
            String[] strArrSplit = str.split(",");
            if (strArrSplit.length != i10) {
                return null;
            }
            fArr = new float[i10];
            for (int i11 = 0; i11 < i10; i11++) {
                fArr[i11] = Float.parseFloat(strArrSplit[i11].trim());
            }
        }
        return fArr;
    }

    private int getNextPosition() {
        boolean z10 = false;
        int i10 = 0;
        while (!z10) {
            i10 = this.f1071e0;
            if (i10 >= this.Q * this.S) {
                return -1;
            }
            int iY = y(i10);
            int iX = x(this.f1071e0);
            boolean[] zArr = this.f1072f0[iY];
            if (zArr[iX]) {
                zArr[iX] = false;
                z10 = true;
            }
            this.f1071e0++;
        }
        return i10;
    }

    public static void t(View view) {
        c cVar = (c) view.getLayoutParams();
        cVar.H = -1.0f;
        cVar.f1319f = -1;
        cVar.f1317e = -1;
        cVar.f1321g = -1;
        cVar.f1323h = -1;
        ((ViewGroup.MarginLayoutParams) cVar).leftMargin = -1;
        view.setLayoutParams(cVar);
    }

    public static void u(View view) {
        c cVar = (c) view.getLayoutParams();
        cVar.I = -1.0f;
        cVar.f1327j = -1;
        cVar.f1325i = -1;
        cVar.f1329k = -1;
        cVar.f1331l = -1;
        ((ViewGroup.MarginLayoutParams) cVar).topMargin = -1;
        view.setLayoutParams(cVar);
    }

    public final boolean A(int i10, int i11, int i12, int i13) {
        for (int i14 = i10; i14 < i10 + i12; i14++) {
            for (int i15 = i11; i15 < i11 + i13; i15++) {
                boolean[][] zArr = this.f1072f0;
                if (i14 < zArr.length && i15 < zArr[0].length) {
                    boolean[] zArr2 = zArr[i14];
                    if (zArr2[i15]) {
                        zArr2[i15] = false;
                    }
                }
                return false;
            }
        }
        return true;
    }

    public final View B() {
        View view = new View(getContext());
        view.setId(View.generateViewId());
        view.setVisibility(4);
        this.f1076m.addView(view, new c(0, 0));
        return view;
    }

    public final void E() {
        int i10;
        int i11 = this.R;
        if (i11 != 0 && (i10 = this.T) != 0) {
            this.Q = i11;
            this.S = i10;
            return;
        }
        int i12 = this.T;
        if (i12 > 0) {
            this.S = i12;
            this.Q = ((this.f1268b + i12) - 1) / i12;
        } else if (i11 > 0) {
            this.Q = i11;
            this.S = ((this.f1268b + i11) - 1) / i11;
        } else {
            int iSqrt = (int) (Math.sqrt(this.f1268b) + 1.5d);
            this.Q = iSqrt;
            this.S = ((this.f1268b + iSqrt) - 1) / iSqrt;
        }
    }

    public String getColumnWeights() {
        return this.f1067a0;
    }

    public int getColumns() {
        return this.T;
    }

    public float getHorizontalGaps() {
        return this.f1068b0;
    }

    public int getOrientation() {
        return this.f1070d0;
    }

    public String getRowWeights() {
        return this.W;
    }

    public int getRows() {
        return this.R;
    }

    public String getSkips() {
        return this.V;
    }

    public String getSpans() {
        return this.U;
    }

    public float getVerticalGaps() {
        return this.f1069c0;
    }

    @Override // androidx.constraintlayout.widget.VirtualLayout, androidx.constraintlayout.widget.ConstraintHelper
    public final void l(AttributeSet attributeSet) {
        super.l(attributeSet);
        this.f1271e = true;
        if (attributeSet != null) {
            TypedArray typedArrayObtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, s.Grid);
            int indexCount = typedArrayObtainStyledAttributes.getIndexCount();
            for (int i10 = 0; i10 < indexCount; i10++) {
                int index = typedArrayObtainStyledAttributes.getIndex(i10);
                if (index == s.Grid_grid_rows) {
                    this.R = typedArrayObtainStyledAttributes.getInteger(index, 0);
                } else if (index == s.Grid_grid_columns) {
                    this.T = typedArrayObtainStyledAttributes.getInteger(index, 0);
                } else if (index == s.Grid_grid_spans) {
                    this.U = typedArrayObtainStyledAttributes.getString(index);
                } else if (index == s.Grid_grid_skips) {
                    this.V = typedArrayObtainStyledAttributes.getString(index);
                } else if (index == s.Grid_grid_rowWeights) {
                    this.W = typedArrayObtainStyledAttributes.getString(index);
                } else if (index == s.Grid_grid_columnWeights) {
                    this.f1067a0 = typedArrayObtainStyledAttributes.getString(index);
                } else if (index == s.Grid_grid_orientation) {
                    this.f1070d0 = typedArrayObtainStyledAttributes.getInt(index, 0);
                } else if (index == s.Grid_grid_horizontalGaps) {
                    this.f1068b0 = typedArrayObtainStyledAttributes.getDimension(index, 0.0f);
                } else if (index == s.Grid_grid_verticalGaps) {
                    this.f1069c0 = typedArrayObtainStyledAttributes.getDimension(index, 0.0f);
                } else if (index == s.Grid_grid_validateInputs) {
                    typedArrayObtainStyledAttributes.getBoolean(index, false);
                } else if (index == s.Grid_grid_useRtl) {
                    typedArrayObtainStyledAttributes.getBoolean(index, false);
                }
            }
            E();
            z();
            typedArrayObtainStyledAttributes.recycle();
        }
    }

    @Override // androidx.constraintlayout.widget.VirtualLayout, androidx.constraintlayout.widget.ConstraintHelper, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.f1076m = (ConstraintLayout) getParent();
        w(false);
    }

    @Override // androidx.constraintlayout.widget.ConstraintHelper, android.view.View
    public final void onDraw(Canvas canvas) {
        if (isInEditMode()) {
            Paint paint = new Paint();
            paint.setColor(-65536);
            paint.setStyle(Paint.Style.STROKE);
            int top = getTop();
            int left = getLeft();
            int bottom = getBottom();
            int right = getRight();
            View[] viewArr = this.f1075l;
            int length = viewArr.length;
            int i10 = 0;
            while (i10 < length) {
                View view = viewArr[i10];
                int left2 = view.getLeft() - left;
                int top2 = view.getTop() - top;
                int right2 = view.getRight() - left;
                int bottom2 = view.getBottom() - top;
                canvas.drawRect(left2, 0.0f, right2, bottom - top, paint);
                canvas.drawRect(0.0f, top2, right - left, bottom2, paint);
                i10++;
                top = top;
            }
        }
    }

    public void setColumnWeights(String str) {
        String str2 = this.f1067a0;
        if (str2 == null || !str2.equals(str)) {
            this.f1067a0 = str;
            w(true);
            invalidate();
        }
    }

    public void setColumns(int i10) {
        if (i10 <= 50 && this.T != i10) {
            this.T = i10;
            E();
            z();
            w(false);
            invalidate();
        }
    }

    public void setHorizontalGaps(float f10) {
        if (f10 >= 0.0f && this.f1068b0 != f10) {
            this.f1068b0 = f10;
            w(true);
            invalidate();
        }
    }

    public void setOrientation(int i10) {
        if ((i10 == 0 || i10 == 1) && this.f1070d0 != i10) {
            this.f1070d0 = i10;
            w(true);
            invalidate();
        }
    }

    public void setRowWeights(String str) {
        String str2 = this.W;
        if (str2 == null || !str2.equals(str)) {
            this.W = str;
            w(true);
            invalidate();
        }
    }

    public void setRows(int i10) {
        if (i10 <= 50 && this.R != i10) {
            this.R = i10;
            E();
            z();
            w(false);
            invalidate();
        }
    }

    public void setSkips(String str) {
        String str2 = this.V;
        if (str2 == null || !str2.equals(str)) {
            this.V = str;
            w(true);
            invalidate();
        }
    }

    public void setSpans(CharSequence charSequence) {
        String str = this.U;
        if (str == null || !str.contentEquals(charSequence)) {
            this.U = charSequence.toString();
            w(true);
            invalidate();
        }
    }

    public void setVerticalGaps(float f10) {
        if (f10 >= 0.0f && this.f1069c0 != f10) {
            this.f1069c0 = f10;
            w(true);
            invalidate();
        }
    }

    public final void v(View view, int i10, int i11, int i12, int i13) {
        c cVar = (c) view.getLayoutParams();
        int[] iArr = this.f1074h0;
        cVar.f1317e = iArr[i11];
        cVar.f1325i = iArr[i10];
        cVar.f1323h = iArr[(i11 + i13) - 1];
        cVar.f1331l = iArr[(i10 + i12) - 1];
        view.setLayoutParams(cVar);
    }

    public final void w(boolean z10) {
        int i10;
        int i11;
        int[][] iArrC;
        int[][] iArrC2;
        if (this.f1076m == null || this.Q < 1 || this.S < 1) {
            return;
        }
        HashSet hashSet = this.f1073g0;
        if (z10) {
            for (int i12 = 0; i12 < this.f1072f0.length; i12++) {
                int i13 = 0;
                while (true) {
                    boolean[][] zArr = this.f1072f0;
                    if (i13 < zArr[0].length) {
                        zArr[i12][i13] = true;
                        i13++;
                    }
                }
            }
            hashSet.clear();
        }
        this.f1071e0 = 0;
        int iMax = Math.max(this.Q, this.S);
        View[] viewArr = this.f1075l;
        if (viewArr == null) {
            this.f1075l = new View[iMax];
            int i14 = 0;
            while (true) {
                View[] viewArr2 = this.f1075l;
                if (i14 >= viewArr2.length) {
                    break;
                }
                viewArr2[i14] = B();
                i14++;
            }
        } else if (iMax != viewArr.length) {
            View[] viewArr3 = new View[iMax];
            for (int i15 = 0; i15 < iMax; i15++) {
                View[] viewArr4 = this.f1075l;
                if (i15 < viewArr4.length) {
                    viewArr3[i15] = viewArr4[i15];
                } else {
                    viewArr3[i15] = B();
                }
            }
            int i16 = iMax;
            while (true) {
                View[] viewArr5 = this.f1075l;
                if (i16 >= viewArr5.length) {
                    break;
                }
                this.f1076m.removeView(viewArr5[i16]);
                i16++;
            }
            this.f1075l = viewArr3;
        }
        this.f1074h0 = new int[iMax];
        int i17 = 0;
        while (true) {
            View[] viewArr6 = this.f1075l;
            if (i17 >= viewArr6.length) {
                break;
            }
            this.f1074h0[i17] = viewArr6[i17].getId();
            i17++;
        }
        int id2 = getId();
        int iMax2 = Math.max(this.Q, this.S);
        float[] fArrD = D(this.Q, this.W);
        if (this.Q == 1) {
            c cVar = (c) this.f1075l[0].getLayoutParams();
            u(this.f1075l[0]);
            cVar.f1325i = id2;
            cVar.f1331l = id2;
            this.f1075l[0].setLayoutParams(cVar);
        } else {
            int i18 = 0;
            while (true) {
                i10 = this.Q;
                if (i18 >= i10) {
                    break;
                }
                c cVar2 = (c) this.f1075l[i18].getLayoutParams();
                u(this.f1075l[i18]);
                if (fArrD != null) {
                    cVar2.I = fArrD[i18];
                }
                if (i18 > 0) {
                    cVar2.f1327j = this.f1074h0[i18 - 1];
                } else {
                    cVar2.f1325i = id2;
                }
                if (i18 < this.Q - 1) {
                    cVar2.f1329k = this.f1074h0[i18 + 1];
                } else {
                    cVar2.f1331l = id2;
                }
                if (i18 > 0) {
                    ((ViewGroup.MarginLayoutParams) cVar2).topMargin = (int) this.f1068b0;
                }
                this.f1075l[i18].setLayoutParams(cVar2);
                i18++;
            }
            while (i10 < iMax2) {
                c cVar3 = (c) this.f1075l[i10].getLayoutParams();
                u(this.f1075l[i10]);
                cVar3.f1325i = id2;
                cVar3.f1331l = id2;
                this.f1075l[i10].setLayoutParams(cVar3);
                i10++;
            }
        }
        int id3 = getId();
        int iMax3 = Math.max(this.Q, this.S);
        float[] fArrD2 = D(this.S, this.f1067a0);
        c cVar4 = (c) this.f1075l[0].getLayoutParams();
        if (this.S == 1) {
            t(this.f1075l[0]);
            cVar4.f1317e = id3;
            cVar4.f1323h = id3;
            this.f1075l[0].setLayoutParams(cVar4);
        } else {
            int i19 = 0;
            while (true) {
                i11 = this.S;
                if (i19 >= i11) {
                    break;
                }
                c cVar5 = (c) this.f1075l[i19].getLayoutParams();
                t(this.f1075l[i19]);
                if (fArrD2 != null) {
                    cVar5.H = fArrD2[i19];
                }
                if (i19 > 0) {
                    cVar5.f1319f = this.f1074h0[i19 - 1];
                } else {
                    cVar5.f1317e = id3;
                }
                if (i19 < this.S - 1) {
                    cVar5.f1321g = this.f1074h0[i19 + 1];
                } else {
                    cVar5.f1323h = id3;
                }
                if (i19 > 0) {
                    ((ViewGroup.MarginLayoutParams) cVar5).leftMargin = (int) this.f1068b0;
                }
                this.f1075l[i19].setLayoutParams(cVar5);
                i19++;
            }
            while (i11 < iMax3) {
                c cVar6 = (c) this.f1075l[i11].getLayoutParams();
                t(this.f1075l[i11]);
                cVar6.f1317e = id3;
                cVar6.f1323h = id3;
                this.f1075l[i11].setLayoutParams(cVar6);
                i11++;
            }
        }
        String str = this.V;
        if (str != null && !str.trim().isEmpty() && (iArrC2 = C(this.V)) != null) {
            for (int i20 = 0; i20 < iArrC2.length; i20++) {
                int iY = y(iArrC2[i20][0]);
                int iX = x(iArrC2[i20][0]);
                int[] iArr = iArrC2[i20];
                if (!A(iY, iX, iArr[1], iArr[2])) {
                    break;
                }
            }
        }
        String str2 = this.U;
        if (str2 != null && !str2.trim().isEmpty() && (iArrC = C(this.U)) != null) {
            int[] iArr2 = this.f1267a;
            View[] viewArrK = k(this.f1076m);
            for (int i21 = 0; i21 < iArrC.length; i21++) {
                int iY2 = y(iArrC[i21][0]);
                int iX2 = x(iArrC[i21][0]);
                int[] iArr3 = iArrC[i21];
                if (!A(iY2, iX2, iArr3[1], iArr3[2])) {
                    break;
                }
                View view = viewArrK[i21];
                int[] iArr4 = iArrC[i21];
                v(view, iY2, iX2, iArr4[1], iArr4[2]);
                hashSet.add(Integer.valueOf(iArr2[i21]));
            }
        }
        View[] viewArrK2 = k(this.f1076m);
        for (int i22 = 0; i22 < this.f1268b; i22++) {
            if (!hashSet.contains(Integer.valueOf(this.f1267a[i22]))) {
                int nextPosition = getNextPosition();
                int iY3 = y(nextPosition);
                int iX3 = x(nextPosition);
                if (nextPosition == -1) {
                    return;
                } else {
                    v(viewArrK2[i22], iY3, iX3, 1, 1);
                }
            }
        }
    }

    public final int x(int i10) {
        return this.f1070d0 == 1 ? i10 / this.Q : i10 % this.S;
    }

    public final int y(int i10) {
        return this.f1070d0 == 1 ? i10 % this.Q : i10 / this.S;
    }

    public final void z() {
        boolean[][] zArr = (boolean[][]) Array.newInstance((Class<?>) Boolean.TYPE, this.Q, this.S);
        this.f1072f0 = zArr;
        for (boolean[] zArr2 : zArr) {
            Arrays.fill(zArr2, true);
        }
    }

    public Grid(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f1071e0 = 0;
        this.f1073g0 = new HashSet();
    }

    public Grid(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f1071e0 = 0;
        this.f1073g0 = new HashSet();
    }
}
