package com.bytedance.sdk.component.fm;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.bytedance.sdk.component.fm.ajl;
import defpackage.e;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public abstract class fm {
    protected Context fm;
    protected String jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    protected wsy f6620lb;
    protected ku ro;
    ajl wsy;
    protected Handler yz = new Handler(Looper.getMainLooper());
    protected volatile boolean ajl = false;
    private final Map<String, ajl> wu = new HashMap();

    public abstract Context fm(ef efVar);

    public abstract String fm();

    public abstract void fm(String str);

    public void invokeMethod(final String str) {
        if (this.ajl) {
            return;
        }
        this.yz.post(new Runnable() { // from class: com.bytedance.sdk.component.fm.fm.1
            @Override // java.lang.Runnable
            public void run() {
                onz onzVarFm;
                if (fm.this.ajl) {
                    return;
                }
                try {
                    onzVarFm = fm.this.fm(new JSONObject(str));
                } catch (Exception unused) {
                    onzVarFm = null;
                }
                if (!onz.fm(onzVarFm)) {
                    fm.this.fm(onzVarFm);
                    return;
                }
                Objects.toString(onzVarFm);
                if (onzVarFm != null) {
                    fm.this.ro(gqi.fm(new irt(onzVarFm.fm, "Failed to parse invocation.")), onzVarFm);
                }
            }
        });
    }

    public final void lb(ef efVar) {
        this.fm = fm(efVar);
        this.f6620lb = efVar.yz;
        this.ro = efVar.wu;
        this.wsy = new ajl(efVar, this);
        this.jnr = efVar.vt;
        ro(efVar);
    }

    public void ro() {
        this.wsy.fm();
        Iterator<ajl> it = this.wu.values().iterator();
        while (it.hasNext()) {
            it.next().fm();
        }
        this.yz.removeCallbacksAndMessages(null);
        this.ajl = true;
    }

    public abstract void ro(ef efVar);

    public void fm(String str, onz onzVar) {
        fm(str);
    }

    public final void fm(onz onzVar) {
        String strFm;
        if (this.ajl || (strFm = fm()) == null) {
            return;
        }
        ajl ajlVarRo = ro(onzVar.wsy);
        if (ajlVarRo == null) {
            onzVar.toString();
            if (this.ro != null) {
                fm();
            }
            ro(gqi.fm(new irt(-4, e.o(new StringBuilder("Namespace "), onzVar.wsy, " unknown."))), onzVar);
            return;
        }
        jnr jnrVar = new jnr();
        jnrVar.ro = strFm;
        jnrVar.fm = this.fm;
        jnrVar.f6621lb = ajlVarRo;
        try {
            ajl.fm fmVarFm = ajlVarRo.fm(onzVar, jnrVar);
            if (fmVarFm == null) {
                onzVar.toString();
                if (this.ro != null) {
                    fm();
                }
                ro(gqi.fm(new irt(-2, "Function " + onzVar.yz + " is not registered.")), onzVar);
                return;
            }
            if (fmVarFm.fm) {
                ro(fmVarFm.ro, onzVar);
            }
            if (this.ro != null) {
                fm();
            }
        } catch (Exception e10) {
            onzVar.toString();
            ro(gqi.fm(e10), onzVar);
        }
    }

    public final void ro(String str, onz onzVar) {
        JSONObject jSONObject;
        if (this.ajl || TextUtils.isEmpty(onzVar.ajl)) {
            return;
        }
        if (!str.startsWith("{") || !str.endsWith("}")) {
            wu.fm(new IllegalArgumentException("Illegal callback data: ".concat(str)));
        }
        try {
            jSONObject = new JSONObject(str);
        } catch (Exception unused) {
            jSONObject = new JSONObject();
        }
        fm(dsz.fm().fm("__msg_type", "callback").fm("__callback_id", onzVar.ajl).fm("__params", jSONObject).ro(), onzVar);
    }

    private ajl ro(String str) {
        return (TextUtils.equals(str, this.jnr) || TextUtils.isEmpty(str)) ? this.wsy : this.wu.get(str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public onz fm(JSONObject jSONObject) {
        String strOptString;
        if (this.ajl) {
            return null;
        }
        String strOptString2 = jSONObject.optString("__callback_id");
        String strOptString3 = jSONObject.optString("func");
        if (fm() == null) {
            return null;
        }
        try {
            String string = jSONObject.getString("__msg_type");
            String strValueOf = "";
            try {
                Object objOpt = jSONObject.opt("params");
                if (objOpt == null) {
                    strOptString = strValueOf;
                } else if (objOpt instanceof JSONObject) {
                    strOptString = String.valueOf((JSONObject) objOpt);
                } else {
                    if (objOpt instanceof String) {
                        strValueOf = (String) objOpt;
                    } else {
                        strValueOf = String.valueOf(objOpt);
                    }
                    strOptString = strValueOf;
                }
            } catch (Throwable unused) {
                strOptString = jSONObject.optString("params");
            }
            String string2 = jSONObject.getString("JSSDK");
            String strOptString4 = jSONObject.optString("namespace");
            return onz.fm().fm(string2).ro(string).lb(strOptString3).yz(strOptString).jnr(strOptString2).ajl(strOptString4).wsy(jSONObject.optString("__iframe_url")).fm();
        } catch (JSONException unused2) {
            return onz.fm(strOptString2, -1);
        }
    }
}
