package com.bytedance.sdk.component.YFl;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.bytedance.sdk.component.YFl.DSW;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes2.dex */
public abstract class YFl {
    DSW DSW;
    protected GA Sg;
    protected Context YFl;
    protected qsH tN;
    protected String wN;
    protected Handler AlY = new Handler(Looper.getMainLooper());
    protected volatile boolean vc = false;
    private final Map<String, DSW> qsH = new HashMap();

    protected abstract void Sg(nc ncVar);

    protected abstract Context YFl(nc ncVar);

    protected abstract String YFl();

    protected abstract void YFl(String str);

    /* JADX INFO: Access modifiers changed from: protected */
    public void invokeMethod(final String str) {
        if (this.vc) {
            return;
        }
        this.AlY.post(new Runnable() { // from class: com.bytedance.sdk.component.YFl.YFl.1
            @Override // java.lang.Runnable
            public void run() {
                rkt rktVar;
                if (YFl.this.vc) {
                    return;
                }
                try {
                    rktVar = YFl.this.YFl(new JSONObject(str));
                } catch (Exception unused) {
                    rktVar = null;
                }
                if (rkt.YFl(rktVar)) {
                    new StringBuilder("By pass invalid call: ").append(rktVar);
                    if (rktVar != null) {
                        YFl.this.Sg(bZ.YFl(new Wwa(rktVar.YFl, "Failed to parse invocation.")), rktVar);
                        return;
                    }
                    return;
                }
                YFl.this.YFl(rktVar);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void YFl(String str, rkt rktVar) {
        YFl(str);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void Sg() {
        this.DSW.YFl();
        for (DSW dsw : this.qsH.values()) {
            dsw.YFl();
        }
        this.AlY.removeCallbacksAndMessages(null);
        this.vc = true;
    }

    protected final void YFl(rkt rktVar) {
        String YFl;
        if (this.vc || (YFl = YFl()) == null) {
            return;
        }
        DSW Sg = Sg(rktVar.DSW);
        if (Sg == null) {
            new StringBuilder("Received call with unknown namespace, ").append(rktVar);
            if (this.Sg != null) {
                YFl();
            }
            Sg(bZ.YFl(new Wwa(-4, "Namespace " + rktVar.DSW + " unknown.")), rktVar);
            return;
        }
        vc vcVar = new vc();
        vcVar.Sg = YFl;
        vcVar.YFl = this.YFl;
        vcVar.tN = Sg;
        try {
            DSW.YFl YFl2 = Sg.YFl(rktVar, vcVar);
            if (YFl2 == null) {
                new StringBuilder("Received call but not registered, ").append(rktVar);
                if (this.Sg != null) {
                    YFl();
                }
                Sg(bZ.YFl(new Wwa(-2, "Function " + rktVar.AlY + " is not registered.")), rktVar);
                return;
            }
            if (YFl2.YFl) {
                Sg(YFl2.Sg, rktVar);
            }
            if (this.Sg != null) {
                YFl();
            }
        } catch (Exception e) {
            new StringBuilder("call finished with error, ").append(rktVar);
            Sg(bZ.YFl(e), rktVar);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void YFl(nc ncVar, wXo wxo) {
        this.YFl = YFl(ncVar);
        this.tN = ncVar.AlY;
        this.Sg = ncVar.NjR;
        this.DSW = new DSW(ncVar, this, wxo);
        this.wN = ncVar.eT;
        Sg(ncVar);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void Sg(String str, rkt rktVar) {
        JSONObject jSONObject;
        if (this.vc || TextUtils.isEmpty(rktVar.vc)) {
            return;
        }
        if (!str.startsWith("{") || !str.endsWith("}")) {
            NjR.YFl(new IllegalArgumentException("Illegal callback data: ".concat(String.valueOf(str))));
        }
        String str2 = rktVar.vc;
        try {
            jSONObject = new JSONObject(str);
        } catch (Exception unused) {
            jSONObject = new JSONObject();
        }
        YFl(EH.YFl().YFl("__msg_type", "callback").YFl("__callback_id", rktVar.vc).YFl("__params", jSONObject).Sg(), rktVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public rkt YFl(JSONObject jSONObject) {
        String str;
        if (this.vc) {
            return null;
        }
        String optString = jSONObject.optString("__callback_id");
        String optString2 = jSONObject.optString("func");
        if (YFl() == null) {
            return null;
        }
        try {
            String string = jSONObject.getString("__msg_type");
            String str2 = "";
            Object opt = jSONObject.opt("params");
            if (opt != null) {
                if (opt instanceof JSONObject) {
                    str = String.valueOf((JSONObject) opt);
                    String string2 = jSONObject.getString("JSSDK");
                    String optString3 = jSONObject.optString("namespace");
                    return rkt.YFl().YFl(string2).Sg(string).tN(optString2).AlY(str).wN(optString).vc(optString3).DSW(jSONObject.optString("__iframe_url")).YFl();
                } else if (opt instanceof String) {
                    str2 = (String) opt;
                } else {
                    str2 = String.valueOf(opt);
                }
            }
            str = str2;
            String string22 = jSONObject.getString("JSSDK");
            String optString32 = jSONObject.optString("namespace");
            return rkt.YFl().YFl(string22).Sg(string).tN(optString2).AlY(str).wN(optString).vc(optString32).DSW(jSONObject.optString("__iframe_url")).YFl();
        } catch (JSONException unused) {
            return rkt.YFl(optString, -1);
        }
    }

    private DSW Sg(String str) {
        return (TextUtils.equals(str, this.wN) || TextUtils.isEmpty(str)) ? this.DSW : this.qsH.get(str);
    }
}
