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

import X.0Gf;
import X.0Gp;
import X.2tE;
import X.7he;
import X.9EH;
import X.9c7;
import X.BYK;
import X.C1897Kk4;
import X.C2140Knz;
import X.C2276KqB;
import X.C2406KsH;
import X.C2408KsJ;
import X.C2409KsK;
import X.C2410KsL;
import X.C2413KsO;
import X.C2415KsQ;
import X.C2417KsS;
import X.C2418KsT;
import X.C2420KsV;
import X.C2421KsW;
import X.C4329LiC;
import X.Crj;
import X.M12;
import X.TSt;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.webkit.WebView;
import com.bytedance.covode.number.Covode;
import com.google.gson.g;
import com.google.gson.j;
import com.google.gson.m;
import com.ss.android.common.util.NetworkUtils;
import java.lang.ref.WeakReference;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;
import kotlin.h.b.n;
import kotlin.o.y;
import kotlin.o.z;

public final class PassBackWebInfoBusiness extends BulletBusinessService$Business {
    public boolean LIZ = true;
    public int LIZIZ;
    public int LIZJ;
    public int LIZLLL = 2000;
    public String LJ = "https://ads.tiktok.com/inspect/aegis/client/page/";
    public String LJFF;
    public final Crj LJI = 7he.LIZ(C2418KsT.LIZ);
    public final Crj LJII = 7he.LIZ(C2413KsO.LIZ);
    public final Crj LJIIIIZZ = 7he.LIZ(C2415KsQ.LIZ);
    public final Crj LJIIIZ = 7he.LIZ(C2408KsJ.LIZ);

    static {
        Covode.recordClassIndex(55169);
    }

    private final Handler LIZLLL() {
        return (Handler) this.LJI.getValue();
    }

    private final Map<String, Map<String, String>> LJ() {
        return (Map) this.LJIIIIZZ.getValue();
    }

    public final Map<String, String> LIZ() {
        return (Map) this.LJII.getValue();
    }

    private final void LJFF() {
        C1897Kk4 kk4 = this.LJIIJ.LIZ;
        if (!(kk4 instanceof C2276KqB)) {
            kk4 = null;
        }
        C2276KqB kqB = (C2276KqB) kk4;
        if (kqB instanceof C2276KqB) {
            0Gp.LIZ(new C2406KsH(this, kqB), 0Gp.LIZ, (0Gf) null);
        }
    }

    public final void LIZJ() {
        if (LJI()) {
            0Gp.LIZ(new C2417KsS(this));
        }
        LIZLLL().removeCallbacksAndMessages((Object) null);
    }

    private final boolean LJI() {
        String str;
        C1897Kk4 kk4 = this.LJIIJ.LIZ;
        if (!(kk4 instanceof C2276KqB)) {
            kk4 = null;
        }
        C2276KqB kqB = (C2276KqB) kk4;
        if (!(kqB instanceof C2276KqB) || (!n.LIZ(kqB.LJJL.LIZIZ(), true))) {
            return false;
        }
        if (this.LIZ && NetworkUtils.getNetworkType(9EH.LJJIFFI.LIZ()) != 9c7.WIFI) {
            return false;
        }
        if ((this.LIZJ > 0 && C4329LiC.LIZ.get() >= this.LIZJ) || (str = this.LJFF) == null || str.length() == 0) {
            return false;
        }
        return true;
    }

    public final void LIZIZ() {
        Long LJI2;
        C1897Kk4 kk4 = this.LJIIJ.LIZ;
        if (!(kk4 instanceof C2276KqB)) {
            kk4 = null;
        }
        C2276KqB kqB = (C2276KqB) kk4;
        if ((kqB instanceof C2276KqB) && !LIZ().isEmpty()) {
            m mVar = new m();
            g gVar = new g();
            for (Map.Entry next : LIZ().entrySet()) {
                m mVar2 = new m();
                String LIZ2 = LIZ((String) next.getKey());
                mVar2.LIZ("url", LIZ2);
                mVar2.LIZ("html", LIZ((String) next.getValue()));
                Map map = LJ().get(BYK.LIZ(LIZ2));
                if (map != null && !map.isEmpty()) {
                    m mVar3 = new m();
                    for (Map.Entry entry : map.entrySet()) {
                        String str = (String) entry.getKey();
                        String str2 = (String) entry.getValue();
                        if ((!n.LIZ(str, "x-Tt-Token")) && (!n.LIZ(str, "Cookie")) && (!n.LIZ(str, "x-common-params-v2"))) {
                            mVar3.LIZ(str, str2);
                        }
                    }
                    mVar2.LIZ("headers", mVar3);
                }
                gVar.LIZ((j) mVar2);
            }
            mVar.LIZ("pages", gVar);
            mVar.LIZ("ad_id", (Number) kqB.LJJJJLL.LIZIZ());
            long j = 0;
            try {
                String LIZIZ2 = kqB.LJJLIIIJLLLLLLLZ.LIZIZ();
                if (!(LIZIZ2 == null || (LJI2 = 2tE.LJI(LIZIZ2)) == null)) {
                    j = LJI2.longValue();
                }
            } catch (Exception e) {
                TSt.LIZ(e);
            }
            mVar.LIZ("cid", Long.valueOf(j));
            mVar.LIZ("log_extra", kqB.LJJLIIIJJIZ.LIZIZ());
            mVar.LIZ("timestamp", Long.valueOf(new Date().getTime()));
            9c7 networkType = NetworkUtils.getNetworkType(9EH.LJJIFFI.LIZ());
            n.LIZIZ(networkType, "");
            mVar.LIZ("network_type", Integer.valueOf(networkType.getValue()));
            String LIZ3 = C4329LiC.LIZIZ.LIZ(mVar, this.LJFF);
            if (!TextUtils.isEmpty(LIZ3)) {
                m mVar4 = new m();
                mVar4.LIZ("content", LIZ3);
                ((PassBackApi) this.LJIIIZ.getValue()).executePost(this.LJ, mVar4).enqueue(new C2409KsK());
                LIZ().clear();
                LJ().clear();
                C4329LiC.LIZ.addAndGet(1);
            }
        }
    }

    public final String LIZ(String str) {
        String decode = URLDecoder.decode(str, "UTF-8");
        n.LIZIZ(decode, "");
        return decode;
    }

    /* JADX INFO: super call moved to the top of the method (can break code semantics) */
    public PassBackWebInfoBusiness(C2140Knz knz) {
        super(knz);
        M12.LIZ((Object) knz);
    }

    public final void LIZ(Uri uri) {
        if (LJI() && uri != null) {
            String queryParameter = uri.getQueryParameter("url");
            String queryParameter2 = uri.getQueryParameter("html");
            if (!TextUtils.isEmpty(queryParameter) && !TextUtils.isEmpty(queryParameter2)) {
                if (queryParameter == null) {
                    n.LIZIZ();
                }
                if (queryParameter2 == null) {
                    n.LIZIZ();
                }
                LIZ(queryParameter, queryParameter2);
            }
        }
    }

    public final void LIZ(WeakReference<WebView> weakReference) {
        WebView webView = (WebView) weakReference.get();
        if (webView != null) {
            n.LIZIZ(webView, "");
            String url = webView.getUrl();
            if (url == null) {
                url = "";
            }
            n.LIZIZ(url, "");
            if (TextUtils.isEmpty(url)) {
                LJFF();
            } else {
                webView.evaluateJavascript("encodeURIComponent(document.documentElement.outerHTML)", new C2410KsL(this, url));
            }
        }
    }

    public final void LIZ(WebView webView, boolean z) {
        String url;
        if (LJI() && LIZ().size() < this.LIZIZ && webView != null && (url = webView.getUrl()) != null && !y.LIZ(url) && !n.LIZ("about:blank", webView.getUrl())) {
            WeakReference weakReference = new WeakReference(webView);
            if (z) {
                LIZLLL().postDelayed(new C2420KsV(this, weakReference), (long) this.LIZLLL);
                LIZLLL().postDelayed(new C2421KsW(this, weakReference), 200);
                return;
            }
            LIZ((WeakReference<WebView>) weakReference);
        }
    }

    public final void LIZ(String str, String str2) {
        if (str2 != null) {
            LIZ().put(str, z.LIZJ(str2, (CharSequence) "\""));
        }
    }

    public final void LIZ(String str, Map<String, String> map) {
        String str2;
        M12.LIZ((Object) str, (Object) map);
        if (LJI() && LIZ().size() < this.LIZIZ && !y.LIZ(str) && !n.LIZ("about:blank", str) && !y.LIZJ(str, ".js", false) && !y.LIZJ(str, ".css", false) && (str2 = map.get("Accept")) != null && z.LIZ((CharSequence) str2, (CharSequence) "html", false)) {
            Map<String, Map<String, String>> LJ2 = LJ();
            String LIZ2 = BYK.LIZ(str);
            n.LIZIZ(LIZ2, "");
            LJ2.put(LIZ2, map);
        }
    }
}
