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

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.media3.common.MimeTypes;
import java.util.List;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class zzcvr extends FrameLayout implements ViewTreeObserver.OnScrollChangedListener, ViewTreeObserver.OnGlobalLayoutListener {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f19617a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public View f19618b;

    public zzcvr(Context context) {
        super(context);
        this.f19617a = context;
    }

    public static zzcvr a(Context context, View view, wa1 wa1Var) {
        Resources resources;
        DisplayMetrics displayMetrics;
        zzcvr zzcvrVar = new zzcvr(context);
        List list = wa1Var.f18007s;
        boolean zIsEmpty = list.isEmpty();
        Context context2 = zzcvrVar.f19617a;
        if (!zIsEmpty && (resources = context2.getResources()) != null && (displayMetrics = resources.getDisplayMetrics()) != null) {
            float f10 = ((xa1) list.get(0)).f18391a;
            float f11 = displayMetrics.density;
            zzcvrVar.setLayoutParams(new FrameLayout.LayoutParams((int) (f10 * f11), (int) (r5.f18392b * f11)));
        }
        zzcvrVar.f19618b = view;
        zzcvrVar.addView(view);
        g30 g30Var = m7.n.C.B;
        m30 m30Var = new m30(zzcvrVar, zzcvrVar);
        ViewTreeObserver viewTreeObserverZze = m30Var.zze();
        if (viewTreeObserverZze != null) {
            m30Var.a(viewTreeObserverZze);
        }
        l30 l30Var = new l30(zzcvrVar, zzcvrVar);
        ViewTreeObserver viewTreeObserverZze2 = l30Var.zze();
        if (viewTreeObserverZze2 != null) {
            l30Var.a(viewTreeObserverZze2);
        }
        RelativeLayout relativeLayout = new RelativeLayout(context2);
        JSONObject jSONObject = wa1Var.f17976c0;
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("header");
        if (jSONObjectOptJSONObject != null) {
            zzcvrVar.b(jSONObjectOptJSONObject, relativeLayout, 10);
        }
        JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("footer");
        if (jSONObjectOptJSONObject2 != null) {
            zzcvrVar.b(jSONObjectOptJSONObject2, relativeLayout, 12);
        }
        zzcvrVar.addView(relativeLayout);
        return zzcvrVar;
    }

    public final void b(JSONObject jSONObject, RelativeLayout relativeLayout, int i10) {
        Context context = this.f19617a;
        TextView textView = new TextView(context);
        textView.setTextColor(-1);
        textView.setBackgroundColor(-16777216);
        textView.setGravity(17);
        textView.setText(jSONObject.optString(MimeTypes.BASE_TYPE_TEXT, ""));
        textView.setTextSize((float) jSONObject.optDouble("text_size", 11.0d));
        double dOptDouble = jSONObject.optDouble("padding", 0.0d);
        n7.x xVar = n7.x.f31085g;
        q7.e eVar = xVar.f31086a;
        int iG = q7.e.g(context.getResources().getDisplayMetrics(), (int) dOptDouble);
        textView.setPadding(0, iG, 0, iG);
        double dOptDouble2 = jSONObject.optDouble("height", 15.0d);
        q7.e eVar2 = xVar.f31086a;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1, q7.e.g(context.getResources().getDisplayMetrics(), (int) dOptDouble2));
        layoutParams.addRule(i10);
        relativeLayout.addView(textView, layoutParams);
    }

    @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
    public final void onGlobalLayout() {
        getLocationInWindow(new int[2]);
        this.f19618b.setY(-r0[1]);
    }

    @Override // android.view.ViewTreeObserver.OnScrollChangedListener
    public final void onScrollChanged() {
        getLocationInWindow(new int[2]);
        this.f19618b.setY(-r0[1]);
    }
}
