package android.support.v7.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.s;
import android.support.v4.widget.Space;
import android.support.v4.widget.j;
import android.support.v7.e.a.a;
import android.util.AttributeSet;
import android.util.LogPrinter;
import android.util.Printer;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import com.google.android.gms.common.util.CrashUtils.ErrorDialogData;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;

public class GridLayout extends ViewGroup {
    public static final a ajA = ajy;
    public static final a ajB = ajx;
    public static final a ajC = ajy;
    public static final a ajD = a(ajB, ajC);
    public static final a ajE = a(ajC, ajB);
    public static final a ajF = new a() {
        /* Access modifiers changed, original: final */
        public final int w(View view, int i) {
            return i >> 1;
        }

        public final int k(View view, int i, int i2) {
            return i >> 1;
        }

        /* Access modifiers changed, original: final */
        public final String in() {
            return "CENTER";
        }
    };
    public static final a ajG = new 7();
    public static final a ajH = new 8();
    static final Printer ajh = new LogPrinter(3, GridLayout.class.getName());
    static final Printer aji = new 1();
    private static final int ajj = 0;
    private static final int ajk = 1;
    private static final int ajl = 2;
    private static final int ajm = 3;
    private static final int ajn = 4;
    private static final int ajo = 5;
    private static final int ajp = 6;
    static final a ajw = new 2();
    private static final a ajx = new 3();
    private static final a ajy = new 4();
    public static final a ajz = ajx;
    final d ajq;
    final d ajr;
    boolean ajs;
    int ajt;
    int aju;
    int ajv;
    int mOrientation;
    Printer mPrinter;

    static final class h<K, V> {
        public final int[] akN;
        public final K[] akO;
        public final V[] akP;

        h(K[] kArr, V[] vArr) {
            this.akN = c(kArr);
            this.akO = a(kArr, this.akN);
            this.akP = a(vArr, this.akN);
        }

        public final V bR(int i) {
            return this.akP[this.akN[i]];
        }

        private static <K> int[] c(K[] kArr) {
            int length = kArr.length;
            int[] iArr = new int[length];
            HashMap hashMap = new HashMap();
            for (int i = 0; i < length; i++) {
                Object obj = kArr[i];
                Integer num = (Integer) hashMap.get(obj);
                if (num == null) {
                    num = Integer.valueOf(hashMap.size());
                    hashMap.put(obj, num);
                }
                iArr[i] = num.intValue();
            }
            return iArr;
        }

        private static <K> K[] a(K[] kArr, int[] iArr) {
            int length = kArr.length;
            Object[] objArr = (Object[]) Array.newInstance(kArr.getClass().getComponentType(), GridLayout.g(iArr) + 1);
            for (int i = 0; i < length; i++) {
                objArr[iArr[i]] = kArr[i];
            }
            return objArr;
        }
    }

    public GridLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ajq = new d(this, true);
        this.ajr = new d(this, false);
        this.mOrientation = 0;
        this.ajs = false;
        this.ajt = 1;
        this.ajv = 0;
        this.mPrinter = ajh;
        this.aju = context.getResources().getDimensionPixelOffset(2131428264);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, a.GridLayout);
        try {
            setRowCount(obtainStyledAttributes.getInt(ajk, j.INVALID_ID));
            setColumnCount(obtainStyledAttributes.getInt(ajl, j.INVALID_ID));
            setOrientation(obtainStyledAttributes.getInt(ajj, 0));
            setUseDefaultMargins(obtainStyledAttributes.getBoolean(ajm, false));
            setAlignmentMode(obtainStyledAttributes.getInt(ajn, 1));
            setRowOrderPreserved(obtainStyledAttributes.getBoolean(ajo, true));
            setColumnOrderPreserved(obtainStyledAttributes.getBoolean(ajp, true));
        } finally {
            obtainStyledAttributes.recycle();
        }
    }

    public GridLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

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

    public void setOrientation(int i) {
        if (this.mOrientation != i) {
            this.mOrientation = i;
            ii();
            requestLayout();
        }
    }

    public int getRowCount() {
        return this.ajr.getCount();
    }

    public void setRowCount(int i) {
        this.ajr.setCount(i);
        ii();
        requestLayout();
    }

    public int getColumnCount() {
        return this.ajq.getCount();
    }

    public void setColumnCount(int i) {
        this.ajq.setCount(i);
        ii();
        requestLayout();
    }

    public boolean getUseDefaultMargins() {
        return this.ajs;
    }

    public void setUseDefaultMargins(boolean z) {
        this.ajs = z;
        requestLayout();
    }

    public int getAlignmentMode() {
        return this.ajt;
    }

    public void setAlignmentMode(int i) {
        this.ajt = i;
        requestLayout();
    }

    public void setRowOrderPreserved(boolean z) {
        this.ajr.ad(z);
        ii();
        requestLayout();
    }

    public void setColumnOrderPreserved(boolean z) {
        this.ajq.ad(z);
        ii();
        requestLayout();
    }

    public Printer getPrinter() {
        return this.mPrinter;
    }

    public void setPrinter(Printer printer) {
        if (printer == null) {
            printer = aji;
        }
        this.mPrinter = printer;
    }

    static int g(int[] iArr) {
        int i = -1;
        for (int max : iArr) {
            i = Math.max(i, max);
        }
        return i;
    }

    static <T> T[] a(T[] tArr, T[] tArr2) {
        Object[] objArr = (Object[]) Array.newInstance(tArr.getClass().getComponentType(), tArr.length + tArr2.length);
        System.arraycopy(tArr, 0, objArr, 0, tArr.length);
        System.arraycopy(tArr2, 0, objArr, tArr.length, tArr2.length);
        return objArr;
    }

    static a n(int i, boolean z) {
        switch (((z ? 7 : 112) & i) >> (z ? 0 : 4)) {
            case 1:
                return ajF;
            case 3:
                return z ? ajD : ajz;
            case 5:
                return z ? ajE : ajA;
            case 7:
                return ajH;
            case 8388611:
                return ajB;
            case 8388613:
                return ajC;
            default:
                return ajw;
        }
    }

    /* Access modifiers changed, original: final */
    public final int a(View view, boolean z, boolean z2) {
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        int i = z ? z2 ? layoutParams.leftMargin : layoutParams.rightMargin : z2 ? layoutParams.topMargin : layoutParams.bottomMargin;
        if (i != j.INVALID_ID) {
            return i;
        }
        if (!this.ajs) {
            return 0;
        }
        i iVar = z ? layoutParams.akM : layoutParams.akL;
        d dVar = z ? this.ajq : this.ajr;
        f fVar = iVar.ajL;
        if (z && ih()) {
            if (z2) {
                z2 = false;
            } else {
                z2 = true;
            }
        }
        if (!z2) {
            dVar.getCount();
        } else if (fVar.min != 0) {
        }
        if (view.getClass() == Space.class || view.getClass() == android.widget.Space.class) {
            return 0;
        }
        return this.aju / 2;
    }

    private boolean ih() {
        return s.T(this) == 1;
    }

    private int b(View view, boolean z, boolean z2) {
        if (this.ajt == 1) {
            return a(view, z, z2);
        }
        int[] iArr;
        d dVar = z ? this.ajq : this.ajr;
        if (z2) {
            if (dVar.ajZ == null) {
                dVar.ajZ = new int[(dVar.getCount() + 1)];
            }
            if (!dVar.aka) {
                dVar.af(true);
                dVar.aka = true;
            }
            iArr = dVar.ajZ;
        } else {
            if (dVar.akb == null) {
                dVar.akb = new int[(dVar.getCount() + 1)];
            }
            if (!dVar.akc) {
                dVar.af(false);
                dVar.akc = true;
            }
            iArr = dVar.akb;
        }
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        i iVar = z ? layoutParams.akM : layoutParams.akL;
        return iArr[z2 ? iVar.ajL.min : iVar.ajL.max];
    }

    /* Access modifiers changed, original: final */
    public final int g(View view, boolean z) {
        return b(view, z, true) + b(view, z, false);
    }

    private static void a(LayoutParams layoutParams, int i, int i2, int i3, int i4) {
        layoutParams.akL = layoutParams.akL.a(new f(i, i + i2));
        layoutParams.akM = layoutParams.akM.a(new f(i3, i3 + i4));
    }

    private void ii() {
        this.ajv = 0;
        if (this.ajq != null) {
            this.ajq.ii();
        }
        if (this.ajr != null) {
            this.ajr.ii();
        }
        ij();
    }

    private void ij() {
        if (this.ajq != null && this.ajr != null) {
            this.ajq.ij();
            this.ajr.ij();
        }
    }

    static LayoutParams bc(View view) {
        return (LayoutParams) view.getLayoutParams();
    }

    static void S(String str) {
        throw new IllegalArgumentException(str + ". ");
    }

    private void a(LayoutParams layoutParams, boolean z) {
        String str = z ? "column" : "row";
        f fVar = (z ? layoutParams.akM : layoutParams.akL).ajL;
        if (fVar.min != j.INVALID_ID && fVar.min < 0) {
            S(str + " indices must be positive");
        }
        int i = (z ? this.ajq : this.ajr).ajR;
        if (i != j.INVALID_ID) {
            if (fVar.max > i) {
                S(str + " indices (start + span) mustn't exceed the " + str + " count");
            }
            if (fVar.size() > i) {
                S(str + " span mustn't exceed the " + str + " count");
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public boolean checkLayoutParams(LayoutParams layoutParams) {
        if (!(layoutParams instanceof LayoutParams)) {
            return false;
        }
        LayoutParams layoutParams2 = (LayoutParams) layoutParams;
        a(layoutParams2, true);
        a(layoutParams2, false);
        return true;
    }

    private int ik() {
        int i = 1;
        int childCount = getChildCount();
        int i2 = 0;
        while (i2 < childCount) {
            int hashCode;
            View childAt = getChildAt(i2);
            if (childAt.getVisibility() != 8) {
                hashCode = ((LayoutParams) childAt.getLayoutParams()).hashCode() + (i * 31);
            } else {
                hashCode = i;
            }
            i2++;
            i = hashCode;
        }
        return i;
    }

    private void il() {
        while (this.ajv != 0) {
            if (this.ajv != ik()) {
                this.mPrinter.println("The fields of some layout parameters were modified in between layout operations. Check the javadoc for GridLayout.LayoutParams#rowSpec.");
                ii();
            } else {
                return;
            }
        }
        Object obj = this.mOrientation == 0 ? 1 : null;
        d dVar = obj != null ? this.ajq : this.ajr;
        int i = dVar.ajR != j.INVALID_ID ? dVar.ajR : 0;
        int i2 = 0;
        int i3 = 0;
        int[] iArr = new int[i];
        int childCount = getChildCount();
        for (int i4 = 0; i4 < childCount; i4++) {
            int i5;
            LayoutParams layoutParams = (LayoutParams) getChildAt(i4).getLayoutParams();
            i iVar = obj != null ? layoutParams.akL : layoutParams.akM;
            f fVar = iVar.ajL;
            boolean z = iVar.akR;
            int size = fVar.size();
            if (z) {
                i2 = fVar.min;
            }
            iVar = obj != null ? layoutParams.akM : layoutParams.akL;
            f fVar2 = iVar.ajL;
            boolean z2 = iVar.akR;
            int size2 = fVar2.size();
            if (i != 0) {
                size2 = Math.min(size2, i - (z2 ? Math.min(fVar2.min, i) : 0));
            }
            if (z2) {
                i5 = fVar2.min;
            } else {
                i5 = i3;
            }
            if (i != 0) {
                int i6;
                if (!z || !z2) {
                    while (true) {
                        Object obj2;
                        i6 = i5 + size2;
                        if (i6 > iArr.length) {
                            obj2 = null;
                        } else {
                            for (i3 = i5; i3 < i6; i3++) {
                                if (iArr[i3] > i2) {
                                    obj2 = null;
                                    break;
                                }
                            }
                            obj2 = 1;
                        }
                        if (obj2 != null) {
                            break;
                        } else if (z2) {
                            i2++;
                        } else if (i5 + size2 <= i) {
                            i5++;
                        } else {
                            i5 = 0;
                            i2++;
                        }
                    }
                }
                i3 = i5 + size2;
                i6 = i2 + size;
                int length = iArr.length;
                Arrays.fill(iArr, Math.min(i5, length), Math.min(i3, length), i6);
            }
            if (obj != null) {
                a(layoutParams, i2, size, i5, size2);
            } else {
                a(layoutParams, i5, size2, i2, size);
            }
            i3 = i5 + size2;
        }
        this.ajv = ik();
    }

    private void g(View view, int i, int i2, int i3, int i4) {
        view.measure(getChildMeasureSpec(i, g(view, true), i3), getChildMeasureSpec(i2, g(view, false), i4));
    }

    private void d(int i, int i2, boolean z) {
        int childCount = getChildCount();
        for (int i3 = 0; i3 < childCount; i3++) {
            View childAt = getChildAt(i3);
            if (childAt.getVisibility() != 8) {
                LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();
                if (z) {
                    g(childAt, i, i2, layoutParams.width, layoutParams.height);
                } else {
                    boolean z2 = this.mOrientation == 0;
                    i iVar = z2 ? layoutParams.akM : layoutParams.akL;
                    if (iVar.ag(z2) == ajH) {
                        f fVar = iVar.ajL;
                        int[] iz = (z2 ? this.ajq : this.ajr).iz();
                        int g = (iz[fVar.max] - iz[fVar.min]) - g(childAt, z2);
                        if (z2) {
                            g(childAt, i, i2, g, layoutParams.height);
                        } else {
                            g(childAt, i, i2, layoutParams.width, g);
                        }
                    }
                }
            }
        }
    }

    private static int U(int i, int i2) {
        return MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(i + i2), MeasureSpec.getMode(i));
    }

    /* Access modifiers changed, original: protected */
    public void onMeasure(int i, int i2) {
        int bO;
        int bO2;
        il();
        ij();
        int paddingLeft = getPaddingLeft() + getPaddingRight();
        int paddingTop = getPaddingTop() + getPaddingBottom();
        int U = U(i, -paddingLeft);
        int U2 = U(i2, -paddingTop);
        d(U, U2, true);
        if (this.mOrientation == 0) {
            bO = this.ajq.bO(U);
            d(U, U2, false);
            bO2 = this.ajr.bO(U2);
        } else {
            bO2 = this.ajr.bO(U2);
            d(U, U2, false);
            bO = this.ajq.bO(U);
        }
        setMeasuredDimension(View.resolveSizeAndState(Math.max(bO + paddingLeft, getSuggestedMinimumWidth()), i, 0), View.resolveSizeAndState(Math.max(bO2 + paddingTop, getSuggestedMinimumHeight()), i2, 0));
    }

    static int h(View view, boolean z) {
        return z ? view.getMeasuredWidth() : view.getMeasuredHeight();
    }

    public void requestLayout() {
        super.requestLayout();
        ii();
    }

    /* Access modifiers changed, original: protected */
    public void onLayout(boolean z, int i, int i2, int i3, int i4) {
        il();
        int i5 = i3 - i;
        int i6 = i4 - i2;
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        this.ajq.bP((i5 - paddingLeft) - paddingRight);
        this.ajr.bP((i6 - paddingTop) - paddingBottom);
        int[] iz = this.ajq.iz();
        int[] iz2 = this.ajr.iz();
        i6 = 0;
        int childCount = getChildCount();
        while (true) {
            int i7 = i6;
            if (i7 < childCount) {
                View childAt = getChildAt(i7);
                if (childAt.getVisibility() != 8) {
                    LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();
                    i iVar = layoutParams.akM;
                    i iVar2 = layoutParams.akL;
                    f fVar = iVar.ajL;
                    f fVar2 = iVar2.ajL;
                    int i8 = iz[fVar.min];
                    int i9 = iz2[fVar2.min];
                    int i10 = iz[fVar.max] - i8;
                    int i11 = iz2[fVar2.max] - i9;
                    int h = h(childAt, true);
                    int h2 = h(childAt, false);
                    a ag = iVar.ag(true);
                    a ag2 = iVar2.ag(false);
                    e eVar = (e) this.ajq.it().bR(i7);
                    e eVar2 = (e) this.ajr.it().bR(i7);
                    int w = ag.w(childAt, i10 - eVar.ac(true));
                    int w2 = ag2.w(childAt, i11 - eVar2.ac(true));
                    int b = b(childAt, true, true);
                    int b2 = b(childAt, false, true);
                    int b3 = b(childAt, true, false);
                    int i12 = b + b3;
                    int b4 = b2 + b(childAt, false, false);
                    i6 = eVar.a(this, childAt, ag, h + i12, true);
                    paddingBottom = eVar2.a(this, childAt, ag2, h2 + b4, false);
                    int W = ag.W(h, i10 - i12);
                    int W2 = ag2.W(h2, i11 - b4);
                    i6 += i8 + w;
                    i6 = !ih() ? i6 + (paddingLeft + b) : (((i5 - W) - paddingRight) - b3) - i6;
                    paddingBottom = (paddingBottom + ((paddingTop + i9) + w2)) + b2;
                    if (!(W == childAt.getMeasuredWidth() && W2 == childAt.getMeasuredHeight())) {
                        childAt.measure(MeasureSpec.makeMeasureSpec(W, ErrorDialogData.SUPPRESSED), MeasureSpec.makeMeasureSpec(W2, ErrorDialogData.SUPPRESSED));
                    }
                    childAt.layout(i6, paddingBottom, W + i6, W2 + paddingBottom);
                }
                i6 = i7 + 1;
            } else {
                return;
            }
        }
    }

    public static i a(int i, int i2, a aVar, float f) {
        return new i(i != j.INVALID_ID, i, i2, aVar, f);
    }

    public static i im() {
        return a(j.INVALID_ID, 1, ajw, 0.0f);
    }

    private static a a(a aVar, a aVar2) {
        return new 5(aVar, aVar2);
    }

    static boolean bN(int i) {
        return (i & 2) != 0;
    }

    /* Access modifiers changed, original: protected|synthetic */
    public /* synthetic */ LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams();
    }
}
