package androidx.compose.ui.window;

import android.R;
import android.graphics.Outline;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.view.WindowManager;
import androidx.compose.ui.platform.e3;
import androidx.compose.ui.window.l;
import androidx.lifecycle.f1;
import androidx.lifecycle.g1;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import j1.u;
import java.util.UUID;
import kotlin.jvm.internal.v;
import rm.n0;
import y0.l2;
import y0.n3;
import y0.q1;
import y0.s3;
import y0.x2;
import y0.y3;

/* loaded from: classes.dex */
public final class l extends androidx.compose.ui.platform.a implements e3 {
    private static final c R = new c(null);
    public static final int S = 8;
    private static final dn.l T = b.f2983p;
    private final View A;
    private final n B;
    private final WindowManager C;
    private final WindowManager.LayoutParams D;
    private q E;
    private c3.t F;
    private final q1 G;
    private final q1 H;
    private c3.p I;
    private final y3 J;
    private final float K;
    private final Rect L;
    private final u M;
    private Object N;
    private final q1 O;
    private boolean P;
    private final int[] Q;

    /* renamed from: x, reason: collision with root package name */
    private dn.a f2980x;

    /* renamed from: y, reason: collision with root package name */
    private r f2981y;

    /* renamed from: z, reason: collision with root package name */
    private String f2982z;

    /* loaded from: classes.dex */
    public static final class a extends ViewOutlineProvider {
        a() {
        }

        @Override // android.view.ViewOutlineProvider
        public void getOutline(View view, Outline outline) {
            outline.setRect(0, 0, view.getWidth(), view.getHeight());
            outline.setAlpha(BitmapDescriptorFactory.HUE_RED);
        }
    }

    /* loaded from: classes.dex */
    static final class b extends v implements dn.l {

        /* renamed from: p, reason: collision with root package name */
        public static final b f2983p = new b();

        b() {
            super(1);
        }

        public final void a(l lVar) {
            if (lVar.isAttachedToWindow()) {
                lVar.x();
            }
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a((l) obj);
            return n0.f34904a;
        }
    }

    /* loaded from: classes.dex */
    private static final class c {
        private c() {
        }

        public /* synthetic */ c(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class d extends v implements dn.p {

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ int f2985q;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        d(int i10) {
            super(2);
            this.f2985q = i10;
        }

        public final void a(y0.m mVar, int i10) {
            l.this.a(mVar, l2.a(this.f2985q | 1));
        }

        @Override // dn.p
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2) {
            a((y0.m) obj, ((Number) obj2).intValue());
            return n0.f34904a;
        }
    }

    /* loaded from: classes.dex */
    public /* synthetic */ class e {

        /* renamed from: a, reason: collision with root package name */
        public static final /* synthetic */ int[] f2986a;

        static {
            int[] iArr = new int[c3.t.values().length];
            try {
                iArr[c3.t.Ltr.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[c3.t.Rtl.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            f2986a = iArr;
        }
    }

    /* loaded from: classes.dex */
    static final class f extends v implements dn.a {
        f() {
            super(0);
        }

        @Override // dn.a
        public final Boolean invoke() {
            h2.s parentLayoutCoordinates = l.this.getParentLayoutCoordinates();
            if (parentLayoutCoordinates == null || !parentLayoutCoordinates.N()) {
                parentLayoutCoordinates = null;
            }
            return Boolean.valueOf((parentLayoutCoordinates == null || l.this.m35getPopupContentSizebOM6tXw() == null) ? false : true);
        }
    }

    /* loaded from: classes.dex */
    static final class g extends v implements dn.l {
        g() {
            super(1);
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static final void c(dn.a aVar) {
            aVar.invoke();
        }

        public final void b(final dn.a aVar) {
            Handler handler = l.this.getHandler();
            if ((handler != null ? handler.getLooper() : null) == Looper.myLooper()) {
                aVar.invoke();
                return;
            }
            Handler handler2 = l.this.getHandler();
            if (handler2 != null) {
                handler2.post(new Runnable() { // from class: androidx.compose.ui.window.m
                    @Override // java.lang.Runnable
                    public final void run() {
                        l.g.c(dn.a.this);
                    }
                });
            }
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            b((dn.a) obj);
            return n0.f34904a;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class h extends v implements dn.a {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ kotlin.jvm.internal.n0 f2989p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ l f2990q;

        /* renamed from: r, reason: collision with root package name */
        final /* synthetic */ c3.p f2991r;

        /* renamed from: s, reason: collision with root package name */
        final /* synthetic */ long f2992s;

        /* renamed from: t, reason: collision with root package name */
        final /* synthetic */ long f2993t;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        h(kotlin.jvm.internal.n0 n0Var, l lVar, c3.p pVar, long j10, long j11) {
            super(0);
            this.f2989p = n0Var;
            this.f2990q = lVar;
            this.f2991r = pVar;
            this.f2992s = j10;
            this.f2993t = j11;
        }

        @Override // dn.a
        public /* bridge */ /* synthetic */ Object invoke() {
            m37invoke();
            return n0.f34904a;
        }

        /* renamed from: invoke, reason: collision with other method in class */
        public final void m37invoke() {
            this.f2989p.f26198p = this.f2990q.getPositionProvider().a(this.f2991r, this.f2992s, this.f2990q.getParentLayoutDirection(), this.f2993t);
        }
    }

    public /* synthetic */ l(dn.a aVar, r rVar, String str, View view, c3.d dVar, q qVar, UUID uuid, n nVar, int i10, kotlin.jvm.internal.k kVar) {
        this(aVar, rVar, str, view, dVar, qVar, uuid, (i10 & 128) != 0 ? new o() : nVar);
    }

    private final dn.p getContent() {
        return (dn.p) this.O.getValue();
    }

    private final int getDisplayHeight() {
        return Math.round(getContext().getResources().getConfiguration().screenHeightDp * getContext().getResources().getDisplayMetrics().density);
    }

    private final int getDisplayWidth() {
        return Math.round(getContext().getResources().getConfiguration().screenWidthDp * getContext().getResources().getDisplayMetrics().density);
    }

    public static /* synthetic */ void getParams$ui_release$annotations() {
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final h2.s getParentLayoutCoordinates() {
        return (h2.s) this.H.getValue();
    }

    private final WindowManager.LayoutParams l() {
        int h10;
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.gravity = 8388659;
        h10 = androidx.compose.ui.window.b.h(this.f2981y, androidx.compose.ui.window.b.i(this.A));
        layoutParams.flags = h10;
        layoutParams.type = 1002;
        layoutParams.token = this.A.getApplicationWindowToken();
        layoutParams.width = -2;
        layoutParams.height = -2;
        layoutParams.format = -3;
        layoutParams.setTitle(this.A.getContext().getResources().getString(l1.n.f27406c));
        return layoutParams;
    }

    private final void n() {
        if (!this.f2981y.a() || Build.VERSION.SDK_INT < 33) {
            return;
        }
        if (this.N == null) {
            this.N = androidx.compose.ui.window.e.b(this.f2980x);
        }
        androidx.compose.ui.window.e.d(this, this.N);
    }

    private final void o() {
        if (Build.VERSION.SDK_INT >= 33) {
            androidx.compose.ui.window.e.e(this, this.N);
        }
        this.N = null;
    }

    private final void s(c3.t tVar) {
        int i10 = e.f2986a[tVar.ordinal()];
        int i11 = 1;
        if (i10 == 1) {
            i11 = 0;
        } else if (i10 != 2) {
            throw new rm.t();
        }
        super.setLayoutDirection(i11);
    }

    private final void setContent(dn.p pVar) {
        this.O.setValue(pVar);
    }

    private final void setParentLayoutCoordinates(h2.s sVar) {
        this.H.setValue(sVar);
    }

    private final void w(r rVar) {
        int h10;
        if (kotlin.jvm.internal.t.b(this.f2981y, rVar)) {
            return;
        }
        if (rVar.f() && !this.f2981y.f()) {
            WindowManager.LayoutParams layoutParams = this.D;
            layoutParams.width = -2;
            layoutParams.height = -2;
        }
        this.f2981y = rVar;
        WindowManager.LayoutParams layoutParams2 = this.D;
        h10 = androidx.compose.ui.window.b.h(rVar, androidx.compose.ui.window.b.i(this.A));
        layoutParams2.flags = h10;
        this.B.a(this.C, this, this.D);
    }

    @Override // androidx.compose.ui.platform.a
    public void a(y0.m mVar, int i10) {
        int i11;
        y0.m r10 = mVar.r(-857613600);
        if ((i10 & 6) == 0) {
            i11 = (r10.m(this) ? 4 : 2) | i10;
        } else {
            i11 = i10;
        }
        if ((i11 & 3) == 2 && r10.v()) {
            r10.A();
        } else {
            if (y0.p.H()) {
                y0.p.Q(-857613600, i11, -1, "androidx.compose.ui.window.PopupLayout.Content (AndroidPopup.android.kt:572)");
            }
            getContent().invoke(r10, 0);
            if (y0.p.H()) {
                y0.p.P();
            }
        }
        x2 y10 = r10.y();
        if (y10 != null) {
            y10.a(new d(i10));
        }
    }

    @Override // android.view.ViewGroup, android.view.View
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        KeyEvent.DispatcherState keyDispatcherState;
        if (keyEvent.getKeyCode() == 4 && this.f2981y.a()) {
            if (getKeyDispatcherState() == null) {
                return super.dispatchKeyEvent(keyEvent);
            }
            if (keyEvent.getAction() == 0 && keyEvent.getRepeatCount() == 0) {
                KeyEvent.DispatcherState keyDispatcherState2 = getKeyDispatcherState();
                if (keyDispatcherState2 != null) {
                    keyDispatcherState2.startTracking(keyEvent, this);
                }
                return true;
            }
            if (keyEvent.getAction() == 1 && (keyDispatcherState = getKeyDispatcherState()) != null && keyDispatcherState.isTracking(keyEvent) && !keyEvent.isCanceled()) {
                dn.a aVar = this.f2980x;
                if (aVar != null) {
                    aVar.invoke();
                }
                return true;
            }
        }
        return super.dispatchKeyEvent(keyEvent);
    }

    @Override // androidx.compose.ui.platform.a
    public void g(boolean z10, int i10, int i11, int i12, int i13) {
        View childAt;
        super.g(z10, i10, i11, i12, i13);
        if (this.f2981y.f() || (childAt = getChildAt(0)) == null) {
            return;
        }
        this.D.width = childAt.getMeasuredWidth();
        this.D.height = childAt.getMeasuredHeight();
        this.B.a(this.C, this, this.D);
    }

    public final boolean getCanCalculatePosition() {
        return ((Boolean) this.J.getValue()).booleanValue();
    }

    public final WindowManager.LayoutParams getParams$ui_release() {
        return this.D;
    }

    public final c3.t getParentLayoutDirection() {
        return this.F;
    }

    /* renamed from: getPopupContentSize-bOM6tXw, reason: not valid java name */
    public final c3.r m35getPopupContentSizebOM6tXw() {
        return (c3.r) this.G.getValue();
    }

    public final q getPositionProvider() {
        return this.E;
    }

    @Override // androidx.compose.ui.platform.a
    protected boolean getShouldCreateCompositionOnAttachedToWindow() {
        return this.P;
    }

    @Override // androidx.compose.ui.platform.e3
    public androidx.compose.ui.platform.a getSubCompositionView() {
        return this;
    }

    public final String getTestTag() {
        return this.f2982z;
    }

    @Override // androidx.compose.ui.platform.e3
    public /* bridge */ /* synthetic */ View getViewRoot() {
        return super.getViewRoot();
    }

    @Override // androidx.compose.ui.platform.a
    public void h(int i10, int i11) {
        if (this.f2981y.f()) {
            super.h(i10, i11);
        } else {
            super.h(View.MeasureSpec.makeMeasureSpec(getDisplayWidth(), Integer.MIN_VALUE), View.MeasureSpec.makeMeasureSpec(getDisplayHeight(), Integer.MIN_VALUE));
        }
    }

    public final void m() {
        f1.b(this, null);
        this.C.removeViewImmediate(this);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.compose.ui.platform.a, android.view.ViewGroup, android.view.View
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.M.s();
        n();
    }

    @Override // android.view.ViewGroup, android.view.View
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.M.t();
        this.M.j();
        o();
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!this.f2981y.b()) {
            return super.onTouchEvent(motionEvent);
        }
        boolean z10 = false;
        if ((motionEvent != null && motionEvent.getAction() == 0) && (motionEvent.getX() < BitmapDescriptorFactory.HUE_RED || motionEvent.getX() >= getWidth() || motionEvent.getY() < BitmapDescriptorFactory.HUE_RED || motionEvent.getY() >= getHeight())) {
            dn.a aVar = this.f2980x;
            if (aVar != null) {
                aVar.invoke();
            }
            return true;
        }
        if (motionEvent != null && motionEvent.getAction() == 4) {
            z10 = true;
        }
        if (!z10) {
            return super.onTouchEvent(motionEvent);
        }
        dn.a aVar2 = this.f2980x;
        if (aVar2 != null) {
            aVar2.invoke();
        }
        return true;
    }

    public final void p() {
        int[] iArr = this.Q;
        int i10 = iArr[0];
        int i11 = iArr[1];
        this.A.getLocationOnScreen(iArr);
        int[] iArr2 = this.Q;
        if (i10 == iArr2[0] && i11 == iArr2[1]) {
            return;
        }
        u();
    }

    public final void q(y0.r rVar, dn.p pVar) {
        setParentCompositionContext(rVar);
        setContent(pVar);
        this.P = true;
    }

    public final void r() {
        this.C.addView(this, this.D);
    }

    @Override // android.view.View
    public void setLayoutDirection(int i10) {
    }

    public final void setParentLayoutDirection(c3.t tVar) {
        this.F = tVar;
    }

    /* renamed from: setPopupContentSize-fhxjrPA, reason: not valid java name */
    public final void m36setPopupContentSizefhxjrPA(c3.r rVar) {
        this.G.setValue(rVar);
    }

    public final void setPositionProvider(q qVar) {
        this.E = qVar;
    }

    public final void setTestTag(String str) {
        this.f2982z = str;
    }

    public final void t(dn.a aVar, r rVar, String str, c3.t tVar) {
        this.f2980x = aVar;
        this.f2982z = str;
        w(rVar);
        s(tVar);
    }

    public final void u() {
        h2.s parentLayoutCoordinates = getParentLayoutCoordinates();
        if (parentLayoutCoordinates != null) {
            if (!parentLayoutCoordinates.N()) {
                parentLayoutCoordinates = null;
            }
            if (parentLayoutCoordinates == null) {
                return;
            }
            long g10 = parentLayoutCoordinates.g();
            long f10 = h2.t.f(parentLayoutCoordinates);
            c3.p a10 = c3.q.a(c3.o.a(Math.round(r1.g.m(f10)), Math.round(r1.g.n(f10))), g10);
            if (kotlin.jvm.internal.t.b(a10, this.I)) {
                return;
            }
            this.I = a10;
            x();
        }
    }

    public final void v(h2.s sVar) {
        setParentLayoutCoordinates(sVar);
        u();
    }

    public final void x() {
        c3.r m35getPopupContentSizebOM6tXw;
        c3.p j10;
        c3.p pVar = this.I;
        if (pVar == null || (m35getPopupContentSizebOM6tXw = m35getPopupContentSizebOM6tXw()) == null) {
            return;
        }
        long j11 = m35getPopupContentSizebOM6tXw.j();
        Rect rect = this.L;
        this.B.c(this.A, rect);
        j10 = androidx.compose.ui.window.b.j(rect);
        long a10 = c3.s.a(j10.k(), j10.f());
        kotlin.jvm.internal.n0 n0Var = new kotlin.jvm.internal.n0();
        n0Var.f26198p = c3.n.f7423b.a();
        this.M.o(this, T, new h(n0Var, this, pVar, a10, j11));
        this.D.x = c3.n.h(n0Var.f26198p);
        this.D.y = c3.n.i(n0Var.f26198p);
        if (this.f2981y.c()) {
            this.B.b(this, c3.r.g(a10), c3.r.f(a10));
        }
        this.B.a(this.C, this, this.D);
    }

    public l(dn.a aVar, r rVar, String str, View view, c3.d dVar, q qVar, UUID uuid, n nVar) {
        super(view.getContext(), null, 0, 6, null);
        q1 d10;
        q1 d11;
        q1 d12;
        this.f2980x = aVar;
        this.f2981y = rVar;
        this.f2982z = str;
        this.A = view;
        this.B = nVar;
        Object systemService = view.getContext().getSystemService("window");
        kotlin.jvm.internal.t.e(systemService, "null cannot be cast to non-null type android.view.WindowManager");
        this.C = (WindowManager) systemService;
        this.D = l();
        this.E = qVar;
        this.F = c3.t.Ltr;
        d10 = s3.d(null, null, 2, null);
        this.G = d10;
        d11 = s3.d(null, null, 2, null);
        this.H = d11;
        this.J = n3.d(new f());
        float t10 = c3.h.t(8);
        this.K = t10;
        this.L = new Rect();
        this.M = new u(new g());
        setId(R.id.content);
        f1.b(this, f1.a(view));
        g1.b(this, g1.a(view));
        i5.g.b(this, i5.g.a(view));
        setTag(l1.m.H, "Popup:" + uuid);
        setClipChildren(false);
        setElevation(dVar.L0(t10));
        setOutlineProvider(new a());
        d12 = s3.d(androidx.compose.ui.window.g.f2959a.a(), null, 2, null);
        this.O = d12;
        this.Q = new int[2];
    }
}
