package com.ss.android.ugc.aweme.bullet;

import X.0CB;
import X.0CH;
import X.0CN;
import X.0HH;
import X.2GO;
import X.2HZ;
import X.6Is;
import X.C0659KCk;
import X.C0672KCx;
import X.C0681KDg;
import X.C0984KOx;
import X.C2239Kpa;
import X.Hx2;
import X.ISm;
import X.KT7;
import X.M12;
import X.Rc9;
import X.SvN;
import X.Swl;
import android.app.Activity;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.commerce.service.ICommerceService;
import java.util.HashMap;
import java.util.Map;
import kotlin.o.y;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

public final class BulletEventObserver implements 6Is, 2HZ, 2GO {
    public final KT7 LIZ;
    public final Activity LIZIZ;
    public boolean LIZJ;
    public long LIZLLL;

    static {
        Covode.recordClassIndex(54871);
    }

    public final Map<Integer, Object> getSubscriberEventTypes() {
        HashMap hashMap = new HashMap();
        hashMap.put(246, new Rc9(BulletEventObserver.class, "onEvent", C0681KDg.class, ThreadMode.POSTING, 0, false));
        hashMap.put(392, new Rc9(BulletEventObserver.class, "onJsBroadcastEvent", Hx2.class, ThreadMode.MAIN, 0, false));
        return hashMap;
    }

    public final void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_CREATE) {
            onCreate();
        } else if (r3 == 0CB.ON_RESUME) {
            onResume();
        } else if (r3 == 0CB.ON_PAUSE) {
            onPause();
        } else if (r3 == 0CB.ON_DESTROY) {
            onDestroy();
        }
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public final void onDestroy() {
        EventBus.LIZ().LIZIZ(this);
    }

    private final C0984KOx LIZ() {
        KT7 kt7 = this.LIZ;
        if (kt7 != null) {
            return (C0984KOx) kt7.LIZJ(C0984KOx.class);
        }
        return null;
    }

    @0CN(LIZ = 0CB.ON_CREATE)
    public final void onCreate() {
        EventBus.LIZ(EventBus.LIZ(), this);
        this.LIZLLL = System.currentTimeMillis();
    }

    @0CN(LIZ = 0CB.ON_PAUSE)
    public final void onPause() {
        C0659KCk kCk;
        this.LIZJ = false;
        C0984KOx LIZ2 = LIZ();
        if (LIZ2 != null) {
            kCk = LIZ2.LIZIZ();
        } else {
            kCk = null;
        }
        if (kCk == C0659KCk.WEB) {
            LIZ("viewDisappeared", (JSONObject) null);
            LIZ("invisible", (JSONObject) null);
        }
    }

    @0CN(LIZ = 0CB.ON_RESUME)
    public final void onResume() {
        C0659KCk kCk;
        this.LIZJ = true;
        C0984KOx LIZ2 = LIZ();
        if (LIZ2 != null) {
            kCk = LIZ2.LIZIZ();
        } else {
            kCk = null;
        }
        if (kCk == C0659KCk.WEB) {
            LIZ("viewAppeared", (JSONObject) null);
        }
    }

    @ISm
    public final void onEvent(C0681KDg kDg) {
        String str;
        String str2;
        Activity activity;
        C0672KCx LIZ2;
        if (kDg != null && (str = kDg.LIZ) != null && str.length() != 0) {
            String str3 = kDg.LIZ;
            C0984KOx LIZ3 = LIZ();
            if (LIZ3 == null || (LIZ2 = LIZ3.LIZ()) == null || (str2 = LIZ2.LIZ) == null) {
                str2 = "";
            }
            if (y.LIZ(str3, str2, true) && 1 != 0 && (activity = this.LIZIZ) != null && (!activity.isFinishing())) {
                activity.finish();
            }
        }
    }

    @ISm(LIZ = ThreadMode.MAIN)
    public final void onJsBroadcastEvent(Hx2 hx2) {
        String str;
        JSONObject jSONObject;
        M12.LIZ((Object) hx2);
        JSONObject jSONObject2 = hx2.LIZIZ;
        if (jSONObject2 != null) {
            jSONObject2.optString("eventName");
        }
        if (this.LIZJ) {
            JSONObject jSONObject3 = hx2.LIZIZ;
            if (jSONObject3 != null) {
                str = jSONObject3.optString("eventName");
            } else {
                str = null;
            }
            if (TextUtils.equals("goods_rn_page_monitor", str)) {
                try {
                    JSONObject jSONObject4 = hx2.LIZIZ;
                    if (!(jSONObject4 == null || (jSONObject = jSONObject4.getJSONObject("data")) == null)) {
                        ICommerceService LIZ2 = Swl.LIZ();
                        SvN svN = new SvN();
                        svN.LJIILJJIL = Long.valueOf(Long.valueOf(jSONObject.optString("interact_render_ts")).longValue() - this.LIZLLL);
                        svN.LJIIZILJ = jSONObject.optString("page_id");
                        svN.LJIILIIL = Long.valueOf(Long.valueOf(jSONObject.optString("ender_render_ts")).longValue() - this.LIZLLL);
                        svN.LJIILL = jSONObject.optString("session_id");
                        LIZ2.logCommerceEvents("commerce_page_render_time", svN);
                    }
                } catch (Exception e) {
                    0HH.LIZ(e);
                }
            }
        }
        LIZ("notification", hx2.LIZIZ);
    }

    public BulletEventObserver(KT7 kt7, Activity activity) {
        this.LIZ = kt7;
        this.LIZIZ = activity;
    }

    private final void LIZ(String str, JSONObject jSONObject) {
        C0984KOx LIZ2 = LIZ();
        if (LIZ2 != null) {
            LIZ2.onEvent(new C2239Kpa(str, jSONObject));
        }
    }
}
