package com.bytedance.adsdk.ugeno.core;

import android.text.TextUtils;
import defpackage.e;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import xyz.stream.download.config.InnerConstant;

/* JADX INFO: loaded from: classes.dex */
public class wsy {
    private JSONObject ajl;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private float f6324ef;
    private JSONObject fm;
    private String jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private String f6325lb;
    private JSONObject ro;
    private boolean vt;
    private boolean wsy;
    private float wu;
    private JSONObject yz;

    public static class fm {
        private fm ajl;

        /* JADX INFO: renamed from: ef, reason: collision with root package name */
        private boolean f6326ef;
        private String fm;
        private LinkedList<fm> jnr;
        private boolean ku;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private JSONObject f6327lb;
        private String ro;
        private boolean vt;
        private String wsy;
        private String wu;
        private JSONObject yz;

        public List<fm> ajl() {
            return this.jnr;
        }

        public String fm() {
            return this.fm;
        }

        public JSONObject jnr() {
            return this.f6327lb;
        }

        public boolean lb() {
            return this.f6326ef;
        }

        public String ro() {
            return this.wsy;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder("UGNode{id='");
            sb.append(this.fm);
            sb.append("', name='");
            return e.o(sb, this.ro, "'}");
        }

        public JSONObject wsy() {
            return this.yz;
        }

        public String yz() {
            return this.ro;
        }

        public void fm(String str) {
            this.ro = str;
        }

        public void ro(boolean z10) {
            this.ku = z10;
        }

        public void fm(boolean z10) {
            this.vt = z10;
        }

        public void ro(fm fmVar) {
            if (this.jnr == null) {
                this.jnr = new LinkedList<>();
            }
            this.jnr.addLast(fmVar);
        }

        public void fm(fm fmVar) {
            if (this.jnr == null) {
                this.jnr = new LinkedList<>();
            }
            this.jnr.add(fmVar);
        }

        public void fm(int i10, fm fmVar) {
            if (this.jnr == null) {
                this.jnr = new LinkedList<>();
            }
            this.jnr.add(i10, fmVar);
        }
    }

    public wsy(JSONObject jSONObject, JSONObject jSONObject2) {
        this(jSONObject, jSONObject2, null);
    }

    private fm jnr() {
        if (!yz()) {
            return fm(this.fm, (fm) null);
        }
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("flexDirection", "row");
            jSONObject.put("justifyContent", "flex_start");
            jSONObject.put("alignItems", "flex_start");
            jSONObject.put("clickable", false);
            jSONObject.put("width", "match_parent");
            jSONObject.put("height", "wrap_content");
            float f10 = this.wu;
            if (f10 > 0.0f) {
                jSONObject.put("width", f10);
            }
            float f11 = this.f6324ef;
            if (f11 > 0.0f) {
                jSONObject.put("height", f11);
            }
            JSONObject jSONObject2 = this.yz;
            if (jSONObject2 != null) {
                String strOptString = jSONObject2.optString("xSize");
                if (!TextUtils.isEmpty(strOptString)) {
                    JSONObject jSONObject3 = new JSONObject(strOptString);
                    if (jSONObject3.optInt("width") > 0) {
                        jSONObject.put("width", jSONObject3.optInt("width"));
                    }
                    if (jSONObject3.optInt("height") > 0) {
                        jSONObject.put("height", jSONObject3.optInt("height"));
                    }
                }
            }
        } catch (JSONException unused) {
        }
        fm fmVar = new fm();
        fmVar.ro = "View";
        fmVar.fm = "virtualNode";
        fmVar.f6327lb = jSONObject;
        fmVar.ajl = null;
        fmVar.wsy = this.f6325lb;
        fmVar.f6326ef = this.vt;
        fmVar.wu = this.jnr;
        fmVar.fm(fm(this.fm, fmVar));
        return fmVar;
    }

    public void fm(float f10, float f11) {
        this.wu = f10;
        this.f6324ef = f11;
    }

    public List<fm> lb() {
        if (this.ro == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        Iterator<String> itKeys = this.ro.keys();
        while (itKeys.hasNext()) {
            fm fmVarFm = fm(this.ro.optJSONObject(itKeys.next()), (fm) null);
            if (fmVarFm != null) {
                arrayList.add(fmVarFm);
            }
        }
        return arrayList;
    }

    public String ro() {
        return this.f6325lb;
    }

    public boolean yz() {
        return this.wsy;
    }

    public wsy(JSONObject jSONObject, JSONObject jSONObject2, JSONObject jSONObject3) {
        this.vt = true;
        if (jSONObject != null) {
            if (jSONObject.has("body")) {
                this.fm = jSONObject.optJSONObject("body");
            } else {
                this.fm = jSONObject.optJSONObject("main_template");
            }
            this.ro = jSONObject.optJSONObject("sub_templates");
            JSONObject jSONObjectOptJSONObject = jSONObject.has("meta") ? jSONObject.optJSONObject("meta") : jSONObject.optJSONObject("template_info");
            if (jSONObjectOptJSONObject != null) {
                if (jSONObject.has("body")) {
                    this.wsy = true;
                    String strOptString = jSONObjectOptJSONObject.optString("version");
                    this.f6325lb = strOptString;
                    if (TextUtils.isEmpty(strOptString)) {
                        this.f6325lb = "3.0";
                    }
                } else {
                    this.f6325lb = jSONObjectOptJSONObject.optString("sdk_version");
                }
                if (jSONObjectOptJSONObject.has("adType")) {
                    this.jnr = jSONObjectOptJSONObject.optString("adType");
                }
                if (jSONObjectOptJSONObject.has("gestureThrough")) {
                    this.vt = com.bytedance.adsdk.ugeno.wsy.lb.fm(jSONObjectOptJSONObject.optString("gestureThrough"), true);
                }
            } else if (jSONObject.has("body")) {
                this.f6325lb = "3.0";
                this.wsy = true;
            }
            this.yz = jSONObject2;
            this.ajl = jSONObject3;
        }
    }

    public static boolean yz(fm fmVar) {
        return (fmVar == null || fmVar.f6327lb == null) ? false : true;
    }

    public fm fm() {
        return jnr();
    }

    public boolean ro(fm fmVar) {
        JSONObject jSONObjectJnr;
        if (fmVar == null || (jSONObjectJnr = fmVar.jnr()) == null) {
            return false;
        }
        return TextUtils.equals(jSONObjectJnr.optString("height"), "match_parent");
    }

    private fm fm(JSONObject jSONObject, fm fmVar) {
        String strOptString;
        String strOptString2;
        fm fmVarFm;
        if (jSONObject == null) {
            return null;
        }
        if (jSONObject.has("type")) {
            strOptString = jSONObject.optString("type");
        } else {
            strOptString = jSONObject.optString("name");
        }
        String strOptString3 = jSONObject.optString(InnerConstant.Db.f36069id);
        JSONObject jSONObject2 = new JSONObject();
        Iterator<String> itKeys = jSONObject.keys();
        while (itKeys.hasNext()) {
            String next = itKeys.next();
            if (!TextUtils.equals(next, "children")) {
                try {
                    jSONObject2.put(next, jSONObject.opt(next));
                } catch (JSONException unused) {
                }
            }
        }
        fm fmVar2 = new fm();
        fmVar2.fm = strOptString3;
        if (!this.wsy || !TextUtils.equals("Video", strOptString)) {
            fmVar2.ro = strOptString;
        } else {
            fmVar2.ro = strOptString + "V3";
        }
        fmVar2.f6327lb = jSONObject2;
        fmVar2.ajl = fmVar;
        fmVar2.wsy = this.f6325lb;
        fmVar2.f6326ef = this.vt;
        fmVar2.wu = this.jnr;
        if (jSONObject2.has("i18n")) {
            fmVar2.yz = jSONObject2.optJSONObject("i18n");
        }
        if (TextUtils.equals(strOptString, "CustomComponent")) {
            fm(jSONObject, fmVar2.f6327lb);
        }
        JSONArray jSONArrayOptJSONArray = jSONObject.optJSONArray("children");
        if (jSONArrayOptJSONArray != null && jSONArrayOptJSONArray.length() > 0) {
            int i10 = 0;
            for (int i11 = 0; i11 < jSONArrayOptJSONArray.length(); i11++) {
                JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i11);
                if (jSONObject.has("type")) {
                    strOptString2 = jSONObject.optString("type");
                } else {
                    strOptString2 = jSONObject.optString("name");
                }
                String strFm = com.bytedance.adsdk.ugeno.lb.ro.fm(jSONObjectOptJSONObject.optString(InnerConstant.Db.f36069id), this.yz);
                if (TextUtils.equals(strOptString2, "Template")) {
                    JSONObject jSONObject3 = this.ro;
                    if (jSONObject3 != null) {
                        jSONObjectOptJSONObject = jSONObject3.optJSONObject(strFm);
                        fmVarFm = fm(jSONObjectOptJSONObject, fmVar2);
                    } else {
                        fmVarFm = null;
                    }
                } else {
                    fmVarFm = fm(jSONObjectOptJSONObject, fmVar2);
                }
                if (fmVarFm != null) {
                    fmVarFm.ro(ro(fmVarFm));
                    fmVarFm.fm(fm(fmVarFm));
                }
                if (lb(fmVarFm)) {
                    i10++;
                    fmVar2.ro(fmVarFm);
                } else if (fmVarFm != null) {
                    fmVar2.fm(i11 - i10, fmVarFm);
                }
            }
        }
        return fmVar2;
    }

    public boolean lb(fm fmVar) {
        JSONObject jSONObjectJnr;
        if (fmVar == null || (jSONObjectJnr = fmVar.jnr()) == null) {
            return false;
        }
        return TextUtils.equals(jSONObjectJnr.optString("position"), "absolute");
    }

    public boolean fm(fm fmVar) {
        JSONObject jSONObjectJnr;
        if (fmVar == null || (jSONObjectJnr = fmVar.jnr()) == null) {
            return false;
        }
        return TextUtils.equals(jSONObjectJnr.optString("width"), "match_parent");
    }

    private void fm(JSONObject jSONObject, JSONObject jSONObject2) {
        if (this.ajl == null || jSONObject2 == null) {
            return;
        }
        try {
            String strOptString = this.ajl.optString(jSONObject2.optString("targetId"));
            if (TextUtils.isEmpty(strOptString)) {
                return;
            }
            JSONObject jSONObject3 = new JSONObject(strOptString);
            JSONObject jSONObjectOptJSONObject = jSONObject2.optJSONObject("targetProps");
            if (jSONObjectOptJSONObject != null) {
                Iterator<String> itKeys = jSONObjectOptJSONObject.keys();
                while (itKeys.hasNext()) {
                    String next = itKeys.next();
                    Object objOpt = jSONObjectOptJSONObject.opt(next);
                    if (TextUtils.equals(next, "events") && jSONObject3.has("events")) {
                        if (objOpt instanceof JSONArray) {
                            com.bytedance.adsdk.ugeno.wsy.ro.fm(jSONObject3.optJSONArray("events"), (JSONArray) objOpt);
                        }
                    } else {
                        jSONObject3.put(next, objOpt);
                    }
                }
                JSONArray jSONArrayOptJSONArray = jSONObject.optJSONArray("children");
                if (jSONArrayOptJSONArray == null) {
                    jSONArrayOptJSONArray = new JSONArray();
                }
                jSONArrayOptJSONArray.put(jSONObject3);
                if (jSONObject.has("children")) {
                    return;
                }
                jSONObject.put("children", jSONArrayOptJSONArray);
            }
        } catch (JSONException unused) {
        }
    }
}
