package com.bytedance.sdk.component.DSW.tN;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import com.bytedance.sdk.component.Sg.YFl.EH;
import com.bytedance.sdk.component.Sg.YFl.GA;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/* loaded from: classes2.dex */
public class vc {
    private boolean AlY;
    private Context DSW;
    private int qO;
    private wN qsH;
    private YFl tN;
    private Sg wN;
    private long Sg = 0;
    private boolean vc = false;
    private int NjR = 0;
    private long nc = 19700101000L;
    private int eT = 0;
    private HashMap<String, Integer> YoT = new HashMap<>();
    private HashMap<String, Integer> GA = new HashMap<>();
    private int pDU = 0;
    private HashMap<String, Integer> EH = new HashMap<>();
    private HashMap<String, Integer> rkt = new HashMap<>();
    private boolean lG = true;
    private Map<String, Integer> Wwa = new HashMap();
    Handler YFl = new Handler(Looper.getMainLooper()) { // from class: com.bytedance.sdk.component.DSW.tN.vc.1
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (message.what != 10000) {
                return;
            }
            vc.this.Sg(message.arg1 != 0);
        }
    };

    private boolean YFl(int i) {
        return i >= 200 && i < 400;
    }

    public Sg AlY() {
        return this.wN;
    }

    public YFl Sg() {
        return this.tN;
    }

    public void YFl(Sg sg) {
        this.wN = sg;
    }

    public void YFl(boolean z) {
        this.AlY = z;
    }

    public wN qsH() {
        return this.qsH;
    }

    public boolean tN() {
        return this.AlY;
    }

    private vc() {
    }

    public vc(int i) {
        this.qO = i;
    }

    public String YFl() {
        return "ttnet_tnc_config" + this.qO;
    }

    public void wN() {
        this.Wwa.clear();
    }

    public synchronized void YFl(Context context, boolean z) {
        if (!this.vc) {
            this.DSW = context;
            this.lG = z;
            this.qsH = new wN(context, z, this.qO);
            if (z) {
                NjR();
            }
            this.tN = qsH.YFl().YFl(this.qO, this.DSW);
            this.vc = true;
        }
    }

    private void NjR() {
        SharedPreferences sharedPreferences = this.DSW.getSharedPreferences(YFl(), 0);
        this.NjR = sharedPreferences.getInt("tnc_probe_cmd", 0);
        this.nc = sharedPreferences.getLong("tnc_probe_version", 19700101000L);
    }

    public AlY vc() {
        wN wNVar = this.qsH;
        if (wNVar != null) {
            return wNVar.tN();
        }
        return null;
    }

    public Map<String, String> DSW() {
        AlY vc = vc();
        if (vc != null) {
            return vc.AlY;
        }
        return null;
    }

    public String YFl(String str) {
        String str2;
        Map<String, String> DSW;
        if (TextUtils.isEmpty(str) || str.contains("/network/get_network") || str.contains("/get_domains/v4") || str.contains("/ies/speed")) {
            return str;
        }
        String str3 = null;
        try {
            URL url = new URL(str);
            str2 = url.getProtocol();
            try {
                str3 = url.getHost();
            } catch (Throwable unused) {
            }
        } catch (Throwable unused2) {
            str2 = null;
        }
        if (TextUtils.isEmpty(str2)) {
            return str;
        }
        if ((!"http".equals(str2) && !"https".equals(str2)) || TextUtils.isEmpty(str3) || AlY(str3) || (DSW = DSW()) == null || !DSW.containsKey(str3)) {
            return str;
        }
        String str4 = DSW.get(str3);
        if (TextUtils.isEmpty(str4)) {
            return str;
        }
        String str5 = str2 + "://" + str3;
        return str.startsWith(str5) ? str.replaceFirst(str5, str2 + "://" + str4) : str;
    }

    public synchronized void YFl(GA ga, EH eh) {
        URL url;
        if (ga == null || eh == null) {
            return;
        }
        if (this.lG) {
            if (com.bytedance.sdk.component.DSW.AlY.wN.YFl(this.DSW)) {
                try {
                    url = ga.Sg().YFl();
                } catch (Exception unused) {
                    url = null;
                }
                if (url == null) {
                    return;
                }
                String protocol = url.getProtocol();
                String host = url.getHost();
                String path = url.getPath();
                String YFl = YFl(ga);
                int tN = eh.tN();
                if ("http".equals(protocol) || "https".equals(protocol)) {
                    if (TextUtils.isEmpty(YFl)) {
                        return;
                    }
                    AlY vc = vc();
                    if (vc != null && vc.Sg) {
                        YFl(eh, host);
                    }
                    if (vc == null) {
                        return;
                    }
                    this.YoT.size();
                    this.GA.size();
                    this.EH.size();
                    this.rkt.size();
                    if (tN > 0) {
                        if (YFl(tN)) {
                            if (this.eT > 0 || this.pDU > 0) {
                                nc();
                            }
                            tN(host);
                        } else if (!Sg(tN)) {
                            this.pDU++;
                            this.EH.put(path, 0);
                            this.rkt.put(YFl, 0);
                            if (this.pDU >= vc.qsH && this.EH.size() >= vc.NjR && this.rkt.size() >= vc.nc) {
                                YFl(false, 0L);
                                nc();
                            }
                            Sg(host);
                        }
                    }
                }
            }
        }
    }

    private String YFl(GA ga) {
        if (ga == null || ga.Sg() == null) {
            return "";
        }
        if (ga.Sg().YFl() != null) {
            try {
            } catch (Exception unused) {
                return "";
            }
        }
        return InetAddress.getByName(ga.Sg().YFl().getHost()).getHostAddress();
    }

    private void Sg(String str) {
        Map<String, String> DSW;
        if (TextUtils.isEmpty(str) || (DSW = DSW()) == null || !DSW.containsValue(str)) {
            return;
        }
        if (this.Wwa.get(str) == null) {
            this.Wwa.put(str, 1);
        } else {
            this.Wwa.put(str, Integer.valueOf(this.Wwa.get(str).intValue() + 1));
        }
    }

    private void tN(String str) {
        if (!TextUtils.isEmpty(str) && this.Wwa.containsKey(str)) {
            this.Wwa.put(str, 0);
        }
    }

    private boolean AlY(String str) {
        Map<String, String> DSW = DSW();
        if (DSW == null) {
            return false;
        }
        String str2 = DSW.get(str);
        return (TextUtils.isEmpty(str2) || this.Wwa.get(str2) == null || this.Wwa.get(str2).intValue() < 3) ? false : true;
    }

    public synchronized void YFl(GA ga, Exception exc) {
        URL url;
        if (ga != null) {
            if (ga.Sg() != null && exc != null) {
                if (this.lG) {
                    if (com.bytedance.sdk.component.DSW.AlY.wN.YFl(this.DSW)) {
                        try {
                            url = ga.Sg().YFl();
                        } catch (Exception unused) {
                            url = null;
                        }
                        if (url == null) {
                            return;
                        }
                        String protocol = url.getProtocol();
                        String host = url.getHost();
                        String path = url.getPath();
                        String YFl = YFl(ga);
                        if ("http".equals(protocol) || "https".equals(protocol)) {
                            AlY vc = vc();
                            if (vc == null) {
                                return;
                            }
                            this.YoT.size();
                            this.GA.size();
                            this.EH.size();
                            this.rkt.size();
                            this.eT++;
                            this.YoT.put(path, 0);
                            this.GA.put(YFl, 0);
                            if (this.eT >= vc.wN && this.YoT.size() >= vc.vc && this.GA.size() >= vc.DSW) {
                                YFl(false, 0L);
                                nc();
                            }
                            Sg(host);
                        }
                    }
                }
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x003c A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:24:0x003d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private void YFl(com.bytedance.sdk.component.Sg.YFl.EH r9, java.lang.String r10) {
        /*
            r8 = this;
            if (r9 != 0) goto L3
            return
        L3:
            boolean r10 = r8.lG
            if (r10 != 0) goto L8
            return
        L8:
            java.lang.String r10 = "tnc-cmd"
            r0 = 0
            java.lang.String r9 = r9.YFl(r10, r0)
            boolean r10 = android.text.TextUtils.isEmpty(r9)
            if (r10 == 0) goto L16
            return
        L16:
            java.lang.String r10 = "@"
            java.lang.String[] r9 = r9.split(r10)
            if (r9 == 0) goto L85
            int r10 = r9.length
            r0 = 2
            if (r10 == r0) goto L23
            goto L85
        L23:
            r10 = 1
            r0 = 0
            r2 = 0
            r3 = r9[r2]     // Catch: java.lang.Throwable -> L34
            int r3 = java.lang.Integer.parseInt(r3)     // Catch: java.lang.Throwable -> L34
            r9 = r9[r10]     // Catch: java.lang.Throwable -> L35
            long r4 = java.lang.Long.parseLong(r9)     // Catch: java.lang.Throwable -> L35
            goto L36
        L34:
            r3 = r2
        L35:
            r4 = r0
        L36:
            long r6 = r8.nc
            int r9 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1))
            if (r9 > 0) goto L3d
            return
        L3d:
            r8.NjR = r3
            r8.nc = r4
            android.content.Context r9 = r8.DSW
            java.lang.String r6 = r8.YFl()
            android.content.SharedPreferences r9 = r9.getSharedPreferences(r6, r2)
            android.content.SharedPreferences$Editor r9 = r9.edit()
            java.lang.String r2 = "tnc_probe_cmd"
            android.content.SharedPreferences$Editor r9 = r9.putInt(r2, r3)
            java.lang.String r2 = "tnc_probe_version"
            android.content.SharedPreferences$Editor r9 = r9.putLong(r2, r4)
            r9.apply()
            int r9 = r8.NjR
            r2 = 10000(0x2710, float:1.4013E-41)
            if (r9 != r2) goto L85
            com.bytedance.sdk.component.DSW.tN.AlY r9 = r8.vc()
            if (r9 != 0) goto L6b
            return
        L6b:
            java.util.Random r2 = new java.util.Random
            long r3 = java.lang.System.currentTimeMillis()
            r2.<init>(r3)
            int r3 = r9.YoT
            if (r3 <= 0) goto L82
            int r9 = r9.YoT
            int r9 = r2.nextInt(r9)
            long r0 = (long) r9
            r2 = 1000(0x3e8, double:4.94E-321)
            long r0 = r0 * r2
        L82:
            r8.YFl(r10, r0)
        L85:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sdk.component.DSW.tN.vc.YFl(com.bytedance.sdk.component.Sg.YFl.EH, java.lang.String):void");
    }

    private void YFl(boolean z, long j) {
        if (this.YFl.hasMessages(10000)) {
            return;
        }
        Message obtainMessage = this.YFl.obtainMessage();
        obtainMessage.what = 10000;
        obtainMessage.arg1 = z ? 1 : 0;
        if (j > 0) {
            this.YFl.sendMessageDelayed(obtainMessage, j);
        } else {
            this.YFl.sendMessage(obtainMessage);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void Sg(boolean z) {
        AlY vc = vc();
        if (vc == null) {
            return;
        }
        long elapsedRealtime = SystemClock.elapsedRealtime();
        if (z || this.Sg + (vc.eT * 1000) <= elapsedRealtime) {
            this.Sg = elapsedRealtime;
            qsH.YFl().YFl(this.qO, this.DSW).tN();
        }
    }

    private void nc() {
        this.eT = 0;
        this.YoT.clear();
        this.GA.clear();
        this.pDU = 0;
        this.EH.clear();
        this.rkt.clear();
    }

    private boolean Sg(int i) {
        if (i < 100 || i >= 1000) {
            return true;
        }
        AlY vc = vc();
        return (vc == null || TextUtils.isEmpty(vc.GA) || !vc.GA.contains(String.valueOf(i))) ? false : true;
    }
}
