package com.google.android.gms.internal.ads;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.WeakHashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class tg1 {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final tg1 f16881g = new tg1();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final Handler f16882h = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static Handler f16883i = null;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final ig f16884j = new ig(6);

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final ig f16885k = new ig(7);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f16891f;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ArrayList f16886a = new ArrayList();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ArrayList f16887b = new ArrayList();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final sg1 f16889d = new sg1();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final uz0 f16888c = new uz0(7, (com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b) null);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final uz0 f16890e = new uz0(new eb(14));

    public static void b() {
        if (f16883i == null) {
            Handler handler = new Handler(Looper.getMainLooper());
            f16883i = handler;
            handler.post(f16884j);
            f16883i.postDelayed(f16885k, 200L);
        }
    }

    public final void a(View view, kg1 kg1Var, JSONObject jSONObject, boolean z10) {
        Object obj;
        boolean z11;
        if (ni.d(view) == null) {
            sg1 sg1Var = this.f16889d;
            int i10 = sg1Var.f16440d.contains(view) ? 1 : sg1Var.f16446j ? 2 : 3;
            if (i10 == 3) {
                return;
            }
            jc1 jc1Var = (jc1) kg1Var;
            JSONObject jSONObjectF = jc1Var.f(view);
            qg1.c(jSONObject, jSONObjectF);
            HashMap map = sg1Var.f16437a;
            if (map.size() == 0) {
                obj = null;
            } else {
                Object obj2 = (String) map.get(view);
                if (obj2 != null) {
                    map.remove(view);
                }
                obj = obj2;
            }
            boolean z12 = false;
            if (obj != null) {
                try {
                    jSONObjectF.put("adSessionId", obj);
                } catch (JSONException unused) {
                    ni.g();
                }
                WeakHashMap weakHashMap = sg1Var.f16445i;
                if (weakHashMap.containsKey(view)) {
                    weakHashMap.put(view, Boolean.TRUE);
                } else {
                    z12 = true;
                }
                try {
                    jSONObjectF.put("hasWindowFocus", Boolean.valueOf(z12));
                } catch (JSONException unused2) {
                    ni.g();
                }
                Boolean boolValueOf = Boolean.valueOf(sg1Var.f16444h.contains(obj));
                if (boolValueOf.booleanValue()) {
                    try {
                        jSONObjectF.put("isPipActive", boolValueOf);
                    } catch (JSONException unused3) {
                        ni.g();
                    }
                }
                sg1Var.f16446j = true;
                return;
            }
            HashMap map2 = sg1Var.f16438b;
            rg1 rg1Var = (rg1) map2.get(view);
            if (rg1Var != null) {
                map2.remove(view);
            }
            if (rg1Var != null) {
                fg1 fg1Var = rg1Var.f16134a;
                JSONArray jSONArray = new JSONArray();
                ArrayList arrayList = rg1Var.f16135b;
                int size = arrayList.size();
                for (int i11 = 0; i11 < size; i11++) {
                    jSONArray.put((String) arrayList.get(i11));
                }
                try {
                    jSONObjectF.put("isFriendlyObstructionFor", jSONArray);
                    jSONObjectF.put("friendlyObstructionClass", fg1Var.f11213b);
                    jSONObjectF.put("friendlyObstructionPurpose", fg1Var.f11214c);
                    jSONObjectF.put("friendlyObstructionReason", fg1Var.f11215d);
                } catch (JSONException unused4) {
                    ni.g();
                }
                z11 = true;
            } else {
                z11 = false;
            }
            c(view, jc1Var, jSONObjectF, i10, z10 || z11);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void c(View view, kg1 kg1Var, JSONObject jSONObject, int i10, boolean z10) {
        int i11;
        int i12 = 0;
        boolean z11 = i10 == 1;
        jc1 jc1Var = (jc1) kg1Var;
        switch (jc1Var.f12807a) {
            case 3:
                ArrayList arrayList = new ArrayList();
                cg1 cg1Var = cg1.f9890c;
                if (cg1Var != null) {
                    Collection collectionUnmodifiableCollection = Collections.unmodifiableCollection(cg1Var.f9892b);
                    int size = collectionUnmodifiableCollection.size();
                    IdentityHashMap identityHashMap = new IdentityHashMap(size + size + 3);
                    Iterator it = collectionUnmodifiableCollection.iterator();
                    while (it.hasNext()) {
                        View view2 = (View) ((uf1) it.next()).f17229c.get();
                        if (view2 != null && view2.isAttachedToWindow() && view2.isShown()) {
                            View view3 = view2;
                            while (true) {
                                if (view3 == null) {
                                    View rootView = view2.getRootView();
                                    if (rootView != null && !identityHashMap.containsKey(rootView)) {
                                        identityHashMap.put(rootView, rootView);
                                        float z12 = rootView.getZ();
                                        int size2 = arrayList.size();
                                        while (size2 > 0) {
                                            int i13 = size2 - 1;
                                            if (((View) arrayList.get(i13)).getZ() > z12) {
                                                size2 = i13;
                                            } else {
                                                arrayList.add(size2, rootView);
                                            }
                                        }
                                        arrayList.add(size2, rootView);
                                    }
                                } else if (view3.getAlpha() != 0.0f) {
                                    Object parent = view3.getParent();
                                    view3 = parent instanceof View ? (View) parent : null;
                                }
                            }
                        }
                    }
                }
                int size3 = arrayList.size();
                while (i12 < size3) {
                    a((View) arrayList.get(i12), (kg1) jc1Var.f12808b, jSONObject, z10);
                    i12++;
                }
                break;
            default:
                if (view instanceof ViewGroup) {
                    ViewGroup viewGroup = (ViewGroup) view;
                    if (z11) {
                        HashMap map = new HashMap();
                        for (int i14 = 0; i14 < viewGroup.getChildCount(); i14++) {
                            View childAt = viewGroup.getChildAt(i14);
                            ArrayList arrayList2 = (ArrayList) map.get(Float.valueOf(childAt.getZ()));
                            if (arrayList2 == null) {
                                arrayList2 = new ArrayList();
                                map.put(Float.valueOf(childAt.getZ()), arrayList2);
                            }
                            arrayList2.add(childAt);
                        }
                        ArrayList arrayList3 = new ArrayList(map.keySet());
                        Collections.sort(arrayList3);
                        int size4 = arrayList3.size();
                        int i15 = 0;
                        while (i15 < size4) {
                            ArrayList arrayList4 = (ArrayList) map.get((Float) arrayList3.get(i15));
                            int size5 = arrayList4.size();
                            int i16 = 0;
                            while (true) {
                                i11 = i15 + 1;
                                if (i16 < size5) {
                                    a((View) arrayList4.get(i16), jc1Var, jSONObject, z10);
                                    i16++;
                                }
                            }
                            i15 = i11;
                        }
                    } else {
                        while (i12 < viewGroup.getChildCount()) {
                            a(viewGroup.getChildAt(i12), jc1Var, jSONObject, z10);
                            i12++;
                        }
                    }
                    break;
                }
                break;
        }
    }
}
