package com.lynx.tasm.behavior.ui;

import X.C4373QmA;
import X.C4391QmS;
import X.C4392QmT;
import X.C4398QmZ;
import X.C4453QnS;
import X.C4470Qnj;
import X.C4471Qnk;
import X.C4506QoJ;
import X.C4511QoO;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import com.bytedance.covode.number.Covode;
import com.lynx.tasm.behavior.ui.list.UIList;
import com.lynx.tasm.behavior.ui.utils.BackgroundDrawable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public abstract class UIGroup<T extends ViewGroup> extends LynxUI<T> implements C4391QmS, C4471Qnk {
    public static final float[] mEventCoords = new float[2];
    public static final Matrix mInverseMatrix = new Matrix();
    public static final float[] mMatrixTransformCoords = new float[2];
    public static final PointF mTempPoint = new PointF();
    public static WeakHashMap<View, Integer> mZIndexHash = new WeakHashMap<>();
    public int mCurrentDrawIndex;
    public LynxBaseUI mCurrentDrawUI;
    public C4453QnS mDrawingOrderHelper;
    public boolean mIsInsertViewCalled;
    public Rect mOverflowClipRect;

    public void afterDrawChild(Canvas canvas, View view, long j) {
    }

    public boolean enableAutoClipRadius() {
        return false;
    }

    public ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
        return null;
    }

    public boolean needCustomLayout() {
        return false;
    }

    public View getRealParentView() {
        return this.mView;
    }

    public boolean hasOverlappingRendering() {
        return this.mOverlappingRendering;
    }

    public int getChildCount() {
        return this.mChildren.size();
    }

    public void onAttach() {
        super.onAttach();
        dispatchOnAttach();
    }

    public void onDetach() {
        super.onDetach();
        dispatchOnDetach();
    }

    public void destroy() {
        super.destroy();
        for (LynxBaseUI destroy : this.mChildren) {
            destroy.destroy();
        }
    }

    public void dispatchOnAttach() {
        for (LynxBaseUI onAttach : this.mChildren) {
            onAttach.onAttach();
        }
    }

    public void dispatchOnDetach() {
        for (LynxBaseUI onDetach : this.mChildren) {
            onDetach.onDetach();
        }
    }

    public void initialize() {
        super.initialize();
        this.mDrawingOrderHelper = new C4453QnS((ViewGroup) this.mView);
        if (this.mView instanceof C4470Qnj) {
            ((C4470Qnj) this.mView).bindDrawChildHook(this);
        }
    }

    public void layout() {
        if (((ViewGroup) this.mView).isLayoutRequested()) {
            super.layout();
            layoutChildren();
        }
    }

    public void measure() {
        if (((ViewGroup) this.mView).isLayoutRequested()) {
            measureChildren();
            super.measure();
        }
    }

    public void measureChildren() {
        for (LynxBaseUI measure : this.mChildren) {
            measure.measure();
        }
    }

    static {
        Covode.recordClassIndex(46619);
    }

    public void layoutChildren() {
        for (int i = 0; i < this.mChildren.size(); i++) {
            LynxBaseUI lynxBaseUI = this.mChildren.get(i);
            if (!needCustomLayout()) {
                if (!lynxBaseUI.isFlatten()) {
                    lynxBaseUI.layout();
                } else {
                    ((LynxFlattenUI) lynxBaseUI).LIZ(lynxBaseUI.getOriginLeft(), lynxBaseUI.getOriginTop(), (Rect) null);
                }
            } else if (lynxBaseUI instanceof UIGroup) {
                ((UIGroup) lynxBaseUI).layoutChildren();
            }
        }
    }

    public void removeAll() {
        for (LynxBaseUI lynxBaseUI = this.mDrawHead; lynxBaseUI != null; lynxBaseUI = lynxBaseUI.mNextDrawUI) {
            lynxBaseUI.mDrawParent = null;
        }
        this.mDrawHead = null;
        for (LynxBaseUI parent : this.mChildren) {
            parent.setParent((C4471Qnk) null);
        }
        this.mChildren.clear();
        if (this.mView != null) {
            ((ViewGroup) this.mView).removeAllViews();
        }
    }

    public void updateDrawingOrder() {
        C4453QnS qnS = this.mDrawingOrderHelper;
        qnS.LIZIZ = 0;
        for (int i = 0; i < qnS.LIZ.getChildCount(); i++) {
            if (getViewZIndex(qnS.LIZ.getChildAt(i)) != null) {
                qnS.LIZIZ++;
            }
        }
        qnS.LIZJ = null;
        setChildrenDrawingOrderEnabledHelper(this.mDrawingOrderHelper.LIZ());
        invalidate();
    }

    public UIGroup(C4398QmZ qmZ) {
        this(qmZ, (Object) null);
    }

    public int getIndex(LynxBaseUI lynxBaseUI) {
        return this.mChildren.indexOf(lynxBaseUI);
    }

    public static Integer getViewZIndex(View view) {
        return mZIndexHash.get(view);
    }

    public LynxBaseUI getChildAt(int i) {
        return this.mChildren.get(i);
    }

    public boolean onRemoveChild(LynxBaseUI lynxBaseUI) {
        if (!this.mChildren.remove(lynxBaseUI)) {
            return false;
        }
        lynxBaseUI.setParent((C4471Qnk) null);
        return true;
    }

    public void removeChild(LynxBaseUI lynxBaseUI) {
        if (onRemoveChild(lynxBaseUI)) {
            removeView(lynxBaseUI);
        }
    }

    private void setChildrenDrawingOrderEnabledHelper(boolean z) {
        if (this.mView instanceof C4373QmA) {
            ((C4373QmA) this.mView).setChildrenDrawingOrderEnabled(z);
        } else if (this.mView instanceof C4506QoJ) {
            ((C4506QoJ) this.mView).setChildrenDrawingOrderEnabled(z);
        }
    }

    public void afterDispatchDraw(Canvas canvas) {
        for (LynxBaseUI lynxBaseUI = this.mCurrentDrawUI; lynxBaseUI != null; lynxBaseUI = lynxBaseUI.mNextDrawUI) {
            if (lynxBaseUI.isFlatten() && !(lynxBaseUI instanceof UIShadowProxy)) {
                drawChild((LynxFlattenUI) lynxBaseUI, canvas);
            }
        }
    }

    private void onRemoveChildUI(LynxUI lynxUI) {
        if (LynxUI.ENABLE_ZINDEX) {
            C4453QnS qnS = this.mDrawingOrderHelper;
            if (getViewZIndex(lynxUI.mView) != null) {
                qnS.LIZIZ--;
            }
            qnS.LIZJ = null;
            setChildrenDrawingOrderEnabledHelper(this.mDrawingOrderHelper.LIZ());
        }
    }

    public void insertView(LynxUI lynxUI) {
        int i = -1;
        for (LynxBaseUI lynxBaseUI = this.mDrawHead; lynxBaseUI != null; lynxBaseUI = lynxBaseUI.mNextDrawUI) {
            if (lynxBaseUI instanceof LynxUI) {
                i++;
            }
            if (lynxBaseUI == lynxUI) {
                break;
            }
        }
        if (lynxUI.mView.getParent() != null && (lynxUI.mView.getParent() instanceof ViewGroup)) {
            ((ViewGroup) lynxUI.mView.getParent()).removeView(lynxUI.mView);
            onRemoveChildUI(lynxUI);
        }
        ((ViewGroup) this.mView).addView(lynxUI.mView, i);
        onAddChildUI(lynxUI, i);
    }

    public void removeView(LynxBaseUI lynxBaseUI) {
        if (lynxBaseUI instanceof LynxUI) {
            LynxUI lynxUI = (LynxUI) lynxBaseUI;
            ((ViewGroup) this.mView).removeView(lynxUI.mView);
            if (lynxBaseUI instanceof UIList) {
                ((ViewGroup) this.mView).removeView(((UIList) lynxBaseUI).LJIIIIZZ);
            }
            onRemoveChildUI(lynxUI);
            return;
        }
        invalidate();
    }

    public void beforeDispatchDraw(Canvas canvas) {
        BackgroundDrawable backgroundDrawable;
        Path path;
        this.mCurrentDrawUI = this.mDrawHead;
        this.mCurrentDrawIndex = 0;
        if ((this.mClipToRadius || (this.mContext.LJIILL && this.mOverflow == 0 && enableAutoClipRadius())) && this.mLynxBackground != null && (backgroundDrawable = this.mLynxBackground.LJ) != null && (backgroundDrawable instanceof BackgroundDrawable) && backgroundDrawable.LJI != null && backgroundDrawable.LIZIZ() && backgroundDrawable.LIZJ != null && (path = backgroundDrawable.LIZJ.LIZJ) != null) {
            canvas.clipPath(path);
        }
    }

    public void insertChild(LynxBaseUI lynxBaseUI, int i) {
        onInsertChild(lynxBaseUI, i);
        this.mIsInsertViewCalled = true;
    }

    public static void setViewZIndex(View view, int i) {
        mZIndexHash.put(view, Integer.valueOf(i));
    }

    public void onInsertChild(LynxBaseUI lynxBaseUI, int i) {
        lynxBaseUI.setOffsetDescendantRectToLynxView(getOffsetDescendantRectToLynxView());
        this.mChildren.add(i, lynxBaseUI);
        lynxBaseUI.setParent(this);
    }

    public UIGroup(C4398QmZ qmZ, Object obj) {
        super(qmZ, obj);
        this.mCurrentDrawUI = this.mDrawHead;
        this.mOverflowClipRect = new Rect();
        this.mIsInsertViewCalled = false;
    }

    public void drawChild(LynxFlattenUI lynxFlattenUI, Canvas canvas) {
        Rect bound = lynxFlattenUI.getBound();
        if (bound == null) {
            lynxFlattenUI.LIZ(canvas);
            return;
        }
        canvas.save();
        canvas.clipRect(bound);
        lynxFlattenUI.LIZ(canvas);
        canvas.restore();
    }

    private void onAddChildUI(LynxUI lynxUI, int i) {
        if (LynxUI.ENABLE_ZINDEX) {
            C4453QnS qnS = this.mDrawingOrderHelper;
            if (getViewZIndex(lynxUI.mView) != null) {
                qnS.LIZIZ++;
            }
            qnS.LIZJ = null;
            setChildrenDrawingOrderEnabledHelper(this.mDrawingOrderHelper.LIZ());
        }
    }

    public int getChildDrawingOrder(int i, int i2) {
        C4453QnS qnS = this.mDrawingOrderHelper;
        if (qnS == null) {
            return i2;
        }
        if (qnS.LIZJ == null) {
            ArrayList arrayList = new ArrayList();
            for (int i3 = 0; i3 < i; i3++) {
                arrayList.add(qnS.LIZ.getChildAt(i3));
            }
            Collections.sort(arrayList, new C4392QmT(qnS));
            qnS.LIZJ = new int[i];
            for (int i4 = 0; i4 < i; i4++) {
                qnS.LIZJ[i4] = qnS.LIZ.indexOfChild((View) arrayList.get(i4));
            }
        }
        return qnS.LIZJ[i2];
    }

    public Rect beforeDrawChild(Canvas canvas, View view, long j) {
        return drawFlattenUIBefore(canvas, view, j);
    }

    private Rect drawFlattenUIBefore(Canvas canvas, View view, long j) {
        for (LynxBaseUI lynxBaseUI = this.mCurrentDrawUI; lynxBaseUI != null; lynxBaseUI = lynxBaseUI.mNextDrawUI) {
            if (!lynxBaseUI.isFlatten()) {
                if (((LynxUI) lynxBaseUI).mView == view) {
                    Rect bound = lynxBaseUI.getBound();
                    this.mCurrentDrawUI = lynxBaseUI.mNextDrawUI;
                    return bound;
                }
            } else if (lynxBaseUI.isFlatten()) {
                drawChild((LynxFlattenUI) lynxBaseUI, canvas);
            }
        }
        return null;
    }

    public static LynxUI findTouchTargetOnViewChian(float[] fArr, ViewGroup viewGroup, Map<View, LynxUI> map) {
        LynxUI lynxUI = null;
        for (int childCount = viewGroup.getChildCount() - 1; childCount >= 0; childCount--) {
            View childAt = viewGroup.getChildAt(childCount);
            PointF pointF = mTempPoint;
            if (isTransformedTouchPointInView(fArr[0], fArr[1], viewGroup, childAt, pointF)) {
                float f = fArr[0];
                float f2 = fArr[1];
                fArr[0] = pointF.x;
                fArr[1] = pointF.y;
                if (map.containsKey(childAt)) {
                    lynxUI = map.get(childAt);
                } else {
                    if (childAt instanceof ViewGroup) {
                        lynxUI = findTouchTargetOnViewChian(fArr, (ViewGroup) childAt, map);
                    }
                    fArr[0] = f;
                    fArr[1] = f2;
                }
                if (lynxUI != null) {
                    return lynxUI;
                }
                fArr[0] = f;
                fArr[1] = f2;
            }
        }
        return lynxUI;
    }

    public C4511QoO findUIWithCustomLayout(float f, float f2, UIGroup uIGroup) {
        HashMap hashMap = new HashMap();
        for (int childCount = uIGroup.getChildCount() - 1; childCount >= 0; childCount--) {
            LynxBaseUI childAt = uIGroup.getChildAt(childCount);
            if (childAt instanceof UIShadowProxy) {
                childAt = ((UIShadowProxy) childAt).LIZ;
            }
            if (childAt instanceof LynxUI) {
                LynxUI lynxUI = (LynxUI) childAt;
                hashMap.put(lynxUI.mView, lynxUI);
            } else {
                new RuntimeException("ui that need custom layout should not have flatten child!");
            }
        }
        float[] fArr = {f, f2};
        LynxUI findTouchTargetOnViewChian = findTouchTargetOnViewChian(fArr, (ViewGroup) uIGroup.mView, hashMap);
        if (findTouchTargetOnViewChian == null) {
            return uIGroup;
        }
        if (!findTouchTargetOnViewChian.needCustomLayout() || !(findTouchTargetOnViewChian instanceof UIGroup)) {
            return findTouchTargetOnViewChian.hitTest(fArr[0] + ((float) findTouchTargetOnViewChian.getScrollX()), fArr[1] + ((float) findTouchTargetOnViewChian.getScrollY()));
        }
        UIGroup uIGroup2 = (UIGroup) findTouchTargetOnViewChian;
        return uIGroup2.findUIWithCustomLayout(fArr[0], fArr[1], uIGroup2);
    }

    public static boolean isTransformedTouchPointInView(float f, float f2, ViewGroup viewGroup, View view, PointF pointF) {
        float scrollX = (f + ((float) viewGroup.getScrollX())) - ((float) view.getLeft());
        float scrollY = (f2 + ((float) viewGroup.getScrollY())) - ((float) view.getTop());
        Matrix matrix = view.getMatrix();
        if (!matrix.isIdentity()) {
            float[] fArr = mMatrixTransformCoords;
            fArr[0] = scrollX;
            fArr[1] = scrollY;
            Matrix matrix2 = mInverseMatrix;
            matrix.invert(matrix2);
            matrix2.mapPoints(fArr);
            scrollX = fArr[0];
            scrollY = fArr[1];
        }
        if (scrollX < 0.0f || scrollX >= ((float) (view.getRight() - view.getLeft())) || scrollY < 0.0f || scrollY >= ((float) (view.getBottom() - view.getTop()))) {
            return false;
        }
        pointF.set(scrollX, scrollY);
        return true;
    }
}
