package com.bytedance.sdk.component.adexpress.YFl.tN;

import android.text.TextUtils;
import android.util.Pair;
import com.facebook.react.uimanager.events.VMXo.nBTxWTaH;
import com.ironsource.y8;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONArray;
import org.json.JSONObject;
/* loaded from: classes7.dex */
public class YFl {
    private List<C0109YFl> AlY;
    private String Sg;
    private String YFl;
    private String tN;
    private Map<String, YFl> vc = new ConcurrentHashMap();
    private Sg wN;

    public String AlY() {
        return this.tN;
    }

    public String Sg() {
        return this.YFl;
    }

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

    public Map<String, YFl> YFl() {
        return this.vc;
    }

    public void YFl(Sg sg) {
        this.wN = sg;
    }

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

    public String tN() {
        return this.Sg;
    }

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

    public Sg wN() {
        return this.wN;
    }

    public List<C0109YFl> vc() {
        if (this.AlY == null) {
            this.AlY = new ArrayList();
        }
        return this.AlY;
    }

    public void YFl(List<C0109YFl> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.AlY = list;
    }

    public boolean DSW() {
        return (TextUtils.isEmpty(AlY()) || TextUtils.isEmpty(tN()) || TextUtils.isEmpty(Sg())) ? false : true;
    }

    public JSONObject qsH() {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.putOpt("name", Sg());
            jSONObject.putOpt("version", tN());
            jSONObject.putOpt(y8.h.Z, AlY());
            JSONArray jSONArray = new JSONArray();
            if (vc() != null) {
                for (C0109YFl c0109YFl : vc()) {
                    JSONObject jSONObject2 = new JSONObject();
                    jSONObject2.putOpt("url", c0109YFl.YFl());
                    jSONObject2.putOpt("md5", c0109YFl.Sg());
                    jSONObject2.putOpt("level", Integer.valueOf(c0109YFl.tN()));
                    jSONArray.put(jSONObject2);
                }
            }
            jSONObject.putOpt("resources", jSONArray);
            if (!this.vc.isEmpty()) {
                JSONObject jSONObject3 = new JSONObject();
                boolean z = false;
                for (String str : this.vc.keySet()) {
                    YFl yFl = this.vc.get(str);
                    if (yFl != null) {
                        jSONObject3.put(str, yFl.qsH());
                        z = true;
                    }
                }
                if (z) {
                    jSONObject.put("engines", jSONObject3);
                }
            }
            Sg wN = wN();
            if (wN != null) {
                JSONObject jSONObject4 = new JSONObject();
                jSONObject4.put("url", wN.YFl);
                jSONObject4.put("md5", wN.Sg);
                JSONObject jSONObject5 = new JSONObject();
                List<Pair<String, String>> Sg2 = wN.Sg();
                if (Sg2 != null) {
                    for (Pair<String, String> pair : Sg2) {
                        jSONObject5.put((String) pair.first, pair.second);
                    }
                }
                jSONObject4.put(nBTxWTaH.gUeyiSvPu, jSONObject5);
                jSONObject.putOpt("resources_archive", jSONObject4);
            }
            return jSONObject;
        } catch (Throwable unused) {
            return null;
        }
    }

    public String NjR() {
        JSONObject qsH;
        if (!DSW() || (qsH = qsH()) == null) {
            return null;
        }
        return qsH.toString();
    }

    /* renamed from: com.bytedance.sdk.component.adexpress.YFl.tN.YFl$YFl  reason: collision with other inner class name */
    /* loaded from: classes2.dex */
    public static class C0109YFl {
        private String Sg;
        private String YFl;
        private int tN;

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

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

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

        public void YFl(int i) {
            this.tN = i;
        }

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

        public int tN() {
            return this.tN;
        }

        public boolean equals(Object obj) {
            String str;
            if (obj instanceof C0109YFl) {
                String str2 = this.YFl;
                if (str2 != null) {
                    C0109YFl c0109YFl = (C0109YFl) obj;
                    if (str2.equals(c0109YFl.YFl()) && (str = this.Sg) != null && str.equals(c0109YFl.Sg())) {
                        return true;
                    }
                }
                return false;
            }
            return super.equals(obj);
        }
    }

    /* loaded from: classes2.dex */
    public static class Sg {
        private String Sg;
        private String YFl;
        private List<Pair<String, String>> tN;

        public List<Pair<String, String>> Sg() {
            return this.tN;
        }

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

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

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

        public void YFl(List<Pair<String, String>> list) {
            this.tN = list;
        }
    }

    public static YFl AlY(String str) {
        if (str == null) {
            return null;
        }
        try {
            return YFl(new JSONObject(str));
        } catch (Exception unused) {
            return null;
        }
    }

    public static YFl YFl(JSONObject jSONObject) {
        JSONObject optJSONObject;
        if (jSONObject == null) {
            return null;
        }
        YFl yFl = new YFl();
        yFl.YFl(jSONObject.optString("name"));
        yFl.Sg(jSONObject.optString("version"));
        yFl.tN(jSONObject.optString(y8.h.Z));
        JSONArray optJSONArray = jSONObject.optJSONArray("resources");
        ArrayList arrayList = new ArrayList();
        if (optJSONArray != null && optJSONArray.length() > 0) {
            for (int i = 0; i < optJSONArray.length(); i++) {
                JSONObject optJSONObject2 = optJSONArray.optJSONObject(i);
                C0109YFl c0109YFl = new C0109YFl();
                c0109YFl.YFl(optJSONObject2.optString("url"));
                c0109YFl.Sg(optJSONObject2.optString("md5"));
                c0109YFl.YFl(optJSONObject2.optInt("level"));
                arrayList.add(c0109YFl);
            }
        }
        yFl.YFl(arrayList);
        try {
            JSONObject optJSONObject3 = jSONObject.optJSONObject("engines");
            if (optJSONObject3 != null) {
                Iterator<String> keys = optJSONObject3.keys();
                while (keys.hasNext()) {
                    String next = keys.next();
                    YFl YFl = YFl(optJSONObject3.optJSONObject(next));
                    if (YFl != null) {
                        yFl.YFl().put(next, YFl);
                    }
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
        if (jSONObject.has("resources_archive") && (optJSONObject = jSONObject.optJSONObject("resources_archive")) != null) {
            Sg sg = new Sg();
            sg.YFl(optJSONObject.optString("url"));
            sg.Sg(optJSONObject.optString("md5"));
            JSONObject optJSONObject4 = optJSONObject.optJSONObject("map");
            if (optJSONObject4 != null) {
                Iterator<String> keys2 = optJSONObject4.keys();
                ArrayList arrayList2 = new ArrayList();
                while (keys2.hasNext()) {
                    String next2 = keys2.next();
                    arrayList2.add(new Pair<>(next2, optJSONObject4.optString(next2)));
                }
                sg.YFl(arrayList2);
            }
            yFl.YFl(sg);
        }
        if (yFl.DSW()) {
            return yFl;
        }
        return null;
    }
}
