package com.bytedance.sdk.openadsdk.yz;

import android.text.TextUtils;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.core.model.ai;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.utils.mq;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import xyz.stream.download.config.InnerConstant;

/* JADX INFO: loaded from: classes2.dex */
public class fm implements com.bytedance.sdk.component.ajl.fm.yz.fm.ro {
    private final String ajl;
    private final AtomicBoolean dsz;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private int f7610ef;
    private String fhx;
    public final String fm;
    private String gof;
    private String gqi;
    private String hi;
    private int hlt;
    private String irt;
    private long jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private boolean f7611lb;
    private String lse;
    private String maa;
    private List<String> nt;
    private JSONObject onz;
    private String qhl;
    protected final JSONObject ro;
    private com.bytedance.sdk.openadsdk.yz.ro.fm sds;
    private int vt;
    private String wey;
    private int wsy;
    private int wu;
    private long yz;
    private static final Set<String> ku = new HashSet(Arrays.asList("insight_log"));
    private static final Map<String, String> duv = new HashMap<String, String>() { // from class: com.bytedance.sdk.openadsdk.yz.fm.1
        {
            put(InnerConstant.Db.f36069id, "extra_id");
            put("source", "extra_source");
            put(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL, "extra_url");
            put("toolType", "extra_tool_type");
            put("storeOpenType", "store_open_type");
            put("errorCode", "error_code");
            put("md5", "extra_md5");
            put("areaType", "area_type");
            put("rectInfo", "rect_info");
        }
    };

    /* JADX INFO: renamed from: com.bytedance.sdk.openadsdk.yz.fm$fm, reason: collision with other inner class name */
    public static final class C0111fm {
        private String ajl;
        private String dsz;
        private final int duv;

        /* JADX INFO: renamed from: ef, reason: collision with root package name */
        private String f7612ef;
        private com.bytedance.sdk.openadsdk.yz.ro.fm fhx;
        public int fm;
        private int gof;
        private boolean gqi;
        private final long irt;
        private String jnr;
        private String ku;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private String f7613lb;
        private com.bytedance.sdk.openadsdk.yz.ro.ro onz;
        private int qhl;
        private String ro;
        private List<String> sds;
        private JSONObject vt;
        private String wey;
        private String wsy;
        private String wu;
        private String yz;

        public C0111fm(long j10, lse lseVar) {
            this.qhl = -1;
            this.gof = -1;
            this.fm = -1;
            if (lseVar != null) {
                this.gqi = ai.ro(lseVar);
                this.qhl = lseVar.pcm();
                this.gof = lseVar.ywr();
                this.fm = lseVar.siu();
            }
            this.irt = j10;
            this.duv = com.bytedance.sdk.component.utils.gof.lb(com.bytedance.sdk.openadsdk.core.gof.fm());
        }

        public C0111fm ajl(String str) {
            this.f7612ef = str;
            return this;
        }

        public C0111fm fm(String str) {
            this.dsz = str;
            return this;
        }

        public C0111fm jnr(String str) {
            this.wu = str;
            return this;
        }

        public C0111fm lb(String str) {
            this.yz = str;
            return this;
        }

        public C0111fm ro(String str) {
            this.f7613lb = str;
            return this;
        }

        public C0111fm wsy(String str) {
            this.wsy = str;
            return this;
        }

        public C0111fm wu(String str) {
            this.wey = str;
            return this;
        }

        public C0111fm yz(String str) {
            this.jnr = str;
            return this;
        }

        public C0111fm fm(List<String> list) {
            this.sds = list;
            return this;
        }

        public C0111fm fm(JSONObject jSONObject) {
            if (jSONObject == null) {
                return this;
            }
            this.vt = jSONObject;
            return this;
        }

        public void fm(com.bytedance.sdk.openadsdk.yz.ro.fm fmVar) {
            com.bytedance.sdk.openadsdk.ajl.ro.fm().fm(this.yz, this.wey, this.wsy, this.f7613lb);
            this.fhx = fmVar;
            fm fmVar2 = new fm(this);
            try {
                com.bytedance.sdk.openadsdk.yz.ro.ro roVar = this.onz;
                if (roVar != null) {
                    roVar.fm(fmVar2.ro, this.irt);
                } else {
                    new com.bytedance.sdk.openadsdk.yz.ro.lb().fm(fmVar2.ro, this.irt);
                }
            } catch (Throwable unused) {
            }
            com.bytedance.sdk.openadsdk.yz.fm.ro.fm(fmVar2);
        }
    }

    public fm(String str, JSONObject jSONObject) {
        this.ajl = "adiff";
        this.dsz = new AtomicBoolean(false);
        this.onz = new JSONObject();
        this.fm = str;
        this.ro = jSONObject;
    }

    private void ef() {
        JSONObject jSONObject = this.onz;
        if (jSONObject != null) {
            String strOptString = jSONObject.optString("value");
            String strOptString2 = this.onz.optString("category");
            String strOptString3 = this.onz.optString("log_extra");
            if (fm(this.gof, this.qhl, this.lse)) {
                if (!TextUtils.isEmpty(strOptString) && TextUtils.equals(strOptString, SessionDescription.SUPPORTED_SDP_VERSION)) {
                    return;
                }
                if (!TextUtils.isEmpty(strOptString2) && !ro(strOptString2)) {
                    return;
                }
            } else {
                if ((TextUtils.isEmpty(strOptString) || TextUtils.equals(strOptString, SessionDescription.SUPPORTED_SDP_VERSION)) && (TextUtils.isEmpty(this.gof) || TextUtils.equals(this.gof, SessionDescription.SUPPORTED_SDP_VERSION))) {
                    return;
                }
                if ((TextUtils.isEmpty(this.qhl) || !ro(this.qhl)) && (TextUtils.isEmpty(strOptString2) || !ro(strOptString2))) {
                    return;
                }
                if (TextUtils.isEmpty(this.lse) && TextUtils.isEmpty(strOptString3)) {
                    return;
                }
            }
        } else if (!fm(this.gof, this.qhl, this.lse)) {
            return;
        }
        this.yz = com.bytedance.sdk.openadsdk.yz.fm.ro.f7626lb.incrementAndGet();
    }

    private void vt() throws JSONException {
        this.ro.putOpt("app_log_url", this.hi);
        List<String> list = this.nt;
        if (list != null && !list.isEmpty()) {
            try {
                this.ro.putOpt("app_log_url_back", new JSONArray((Collection) this.nt));
            } catch (Throwable th) {
                wey.lb("AdEvent", th.getMessage());
            }
        }
        this.ro.putOpt("tag", this.fhx);
        this.ro.putOpt("label", this.irt);
        this.ro.putOpt("category", this.qhl);
        if (!TextUtils.isEmpty(this.gof)) {
            try {
                this.ro.putOpt("value", Long.valueOf(Long.parseLong(this.gof)));
            } catch (NumberFormatException unused) {
                this.ro.putOpt("value", 0L);
            }
        }
        if (!TextUtils.isEmpty(this.wey)) {
            try {
                this.ro.putOpt("ext_value", Long.valueOf(Long.parseLong(this.wey)));
            } catch (Exception unused2) {
            }
        }
        if (!TextUtils.isEmpty(this.lse)) {
            this.ro.putOpt("log_extra", this.lse);
        }
        if (!TextUtils.isEmpty(this.maa)) {
            try {
                this.ro.putOpt("ua_policy", Integer.valueOf(Integer.parseInt(this.maa)));
            } catch (NumberFormatException unused3) {
            }
        }
        fm(this.ro, this.irt);
        try {
            if (!this.ro.has("nt")) {
                this.ro.putOpt("nt", Integer.valueOf(this.hlt));
            }
        } catch (Exception unused4) {
        }
        Iterator<String> itKeys = this.onz.keys();
        while (itKeys.hasNext()) {
            String next = itKeys.next();
            this.ro.putOpt(next, this.onz.opt(next));
        }
    }

    public String ajl() {
        if (!TextUtils.isEmpty(this.irt)) {
            return this.irt;
        }
        JSONObject jSONObject = this.ro;
        return jSONObject != null ? jSONObject.optString("label") : "";
    }

    @Override // com.bytedance.sdk.component.ajl.fm.yz.fm.ro
    public long fm() {
        return this.jnr;
    }

    public JSONObject jnr() {
        JSONObject jSONObjectYz = yz();
        try {
            JSONObject jSONObject = new JSONObject(jSONObjectYz.toString());
            JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("params");
            if (jSONObjectOptJSONObject != null) {
                jSONObjectOptJSONObject.remove("app_log_url");
                jSONObjectOptJSONObject.remove("app_log_url_back");
            }
            return jSONObject;
        } catch (JSONException e10) {
            wey.lb("AdEvent", e10.getMessage());
            return jSONObjectYz;
        }
    }

    public int lb() {
        return this.vt;
    }

    @Override // com.bytedance.sdk.component.ajl.fm.yz.fm.ro
    public long ro() {
        return this.yz;
    }

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

    public boolean wu() {
        Set<String> setDuv;
        if (this.ro == null || (setDuv = com.bytedance.sdk.openadsdk.core.gof.yz().duv()) == null) {
            return false;
        }
        String strOptString = this.ro.optString("label");
        if (!TextUtils.isEmpty(strOptString)) {
            return setDuv.contains(strOptString);
        }
        if (TextUtils.isEmpty(this.irt)) {
            return false;
        }
        return setDuv.contains(this.irt);
    }

    public JSONObject yz() {
        if (this.dsz.get()) {
            return this.ro;
        }
        try {
            vt();
            if (this.ro.has("ad_extra_data")) {
                Object objOpt = this.ro.opt("ad_extra_data");
                if (objOpt != null) {
                    try {
                        if (objOpt instanceof JSONObject) {
                            this.ro.put("ad_extra_data", fm((JSONObject) objOpt).toString());
                        } else if (objOpt instanceof String) {
                            this.ro.put("ad_extra_data", fm(new JSONObject((String) objOpt)).toString());
                        }
                    } catch (JSONException e10) {
                        wey.lb("AdEvent", "json error", e10.getMessage());
                    }
                }
            } else {
                JSONObject jSONObject = new JSONObject();
                try {
                    jSONObject.put("adiff", this.fm);
                    if (this.f7611lb) {
                        jSONObject.put("interaction_method", this.wsy);
                        jSONObject.put("real_interaction_method", this.wu);
                        jSONObject.put("image_mode", this.f7610ef);
                    }
                    this.ro.put("ad_extra_data", jSONObject.toString());
                } catch (JSONException e11) {
                    wey.lb("AdEvent", "json error", e11.getMessage());
                }
            }
            this.dsz.set(true);
        } catch (Throwable unused) {
        }
        return this.ro;
    }

    private boolean fm(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.equals(str, SessionDescription.SUPPORTED_SDP_VERSION) || TextUtils.isEmpty(str3)) {
            return false;
        }
        str2.getClass();
        switch (str2) {
            case "umeng":
            case "event_v1":
            case "event_v3":
            case "app_union":
                return true;
            default:
                return false;
        }
    }

    private boolean ro(String str) {
        str.getClass();
        switch (str) {
            case "umeng":
            case "event_v1":
            case "event_v3":
            case "app_union":
                return true;
            default:
                return false;
        }
    }

    private void ro(JSONObject jSONObject) {
        if (jSONObject == null) {
            return;
        }
        for (String str : duv.keySet()) {
            try {
                if (jSONObject.has(str)) {
                    Object objOpt = jSONObject.opt(str);
                    jSONObject.remove(str);
                    jSONObject.put(duv.get(str), objOpt);
                }
            } catch (Throwable unused) {
            }
        }
    }

    public fm(C0111fm c0111fm) {
        this.ajl = "adiff";
        this.dsz = new AtomicBoolean(false);
        this.onz = new JSONObject();
        if (!TextUtils.isEmpty(c0111fm.ro)) {
            this.fm = c0111fm.ro;
        } else {
            this.fm = mq.fm();
        }
        this.sds = c0111fm.fhx;
        this.lse = c0111fm.ajl;
        this.fhx = c0111fm.f7613lb;
        this.irt = c0111fm.yz;
        if (TextUtils.isEmpty(c0111fm.jnr)) {
            this.qhl = "app_union";
        } else {
            this.qhl = c0111fm.jnr;
        }
        this.maa = c0111fm.ku;
        this.gof = c0111fm.wu;
        this.wey = c0111fm.f7612ef;
        this.gqi = c0111fm.wsy;
        this.hlt = c0111fm.duv;
        this.hi = c0111fm.dsz;
        this.onz = c0111fm.vt = c0111fm.vt != null ? c0111fm.vt : new JSONObject();
        JSONObject jSONObject = new JSONObject();
        this.ro = jSONObject;
        if (!TextUtils.isEmpty(c0111fm.dsz)) {
            try {
                jSONObject.put("app_log_url", c0111fm.dsz);
            } catch (JSONException e10) {
                wey.lb("AdEvent", e10.getMessage());
            }
        }
        this.nt = c0111fm.sds;
        if (c0111fm.sds != null && !c0111fm.sds.isEmpty()) {
            try {
                this.ro.putOpt("app_log_url_back", new JSONArray((Collection) c0111fm.sds));
            } catch (Throwable th) {
                wey.lb("AdEvent", th.getMessage());
            }
        }
        this.wsy = c0111fm.qhl;
        this.wu = c0111fm.gof;
        this.f7610ef = c0111fm.fm;
        this.f7611lb = c0111fm.gqi;
        this.jnr = System.currentTimeMillis();
        ef();
    }

    private JSONObject fm(JSONObject jSONObject) {
        try {
            if (!jSONObject.has("adiff")) {
                jSONObject.put("adiff", this.fm);
            }
            if (this.f7611lb) {
                if (!jSONObject.has("interaction_method")) {
                    jSONObject.put("interaction_method", this.wsy);
                }
                if (!jSONObject.has("real_interaction_method")) {
                    jSONObject.put("real_interaction_method", this.wu);
                }
                if (!jSONObject.has("image_mode")) {
                    jSONObject.put("image_mode", this.f7610ef);
                }
            }
            ro(jSONObject);
            if (!jSONObject.has("pangle_client_unique_id")) {
                jSONObject.put("pangle_client_unique_id", "pangle-" + this.fm + "-" + System.currentTimeMillis());
            }
            if (com.bytedance.sdk.openadsdk.yz.fm.fm.ajl.ro()) {
                String strOptString = jSONObject.optString("pag_json_data");
                if (!TextUtils.isEmpty(strOptString)) {
                    this.vt = new JSONObject(strOptString).optInt("_l_s_t");
                }
            }
            return jSONObject;
        } catch (Throwable th) {
            wey.lb("AdEvent", th.getMessage() == null ? "error " : th.getMessage());
            return jSONObject;
        }
    }

    @Override // com.bytedance.sdk.component.ajl.fm.yz.fm.ro
    public JSONObject fm(String str) {
        return yz();
    }

    private static void fm(JSONObject jSONObject, String str) {
        try {
            Set<String> set = ku;
            if (!set.contains(str) && !set.contains(jSONObject.get("label"))) {
                jSONObject.putOpt("is_ad_event", "1");
            }
        } catch (Throwable th) {
            wey.lb("AdEvent", th);
        }
    }
}
