package com.bytedance.ttnet.diagnosis;

import X.0HH;
import X.C53119i2;
import X.C53309iL;
import X.R5k;
import com.bytedance.common.utility.reflect.Reflect;
import com.bytedance.covode.number.Covode;
import com.bytedance.ttnet.TTNetInit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TTNetDiagnosisService {
    public static final Map<Integer, String> sTypeStringMap;

    public static C53309iL getCronetHttpClient() {
        if (C53119i2.LIZ()) {
            return C53309iL.LIZ(TTNetInit.getTTNetDepend().LIZIZ());
        }
        throw new UnsupportedOperationException("Cronet is not enabled");
    }

    static {
        Covode.recordClassIndex(38901);
        HashMap hashMap = new HashMap();
        sTypeStringMap = hashMap;
        hashMap.put(0, "DNS_RESOLVE_TARGET");
        hashMap.put(1, "RACE_TARGETS");
        hashMap.put(2, "ACCELERATE_TARGET");
        hashMap.put(3, "DIAGNOSE_TARGET");
        hashMap.put(4, "DIAGNOSE_V2_TARGET");
    }

    public static String requestTypeToString(int i) {
        return sTypeStringMap.get(Integer.valueOf(i));
    }

    public static boolean isOneTargetRequestType(int i) {
        if (i != 0) {
            if (i == 1) {
                return false;
            }
            if (!(i == 2 || i == 3 || i == 4)) {
                throw new IllegalArgumentException("Illegal request type: ".concat(String.valueOf(i)));
            }
        }
        return true;
    }

    public static void reportUserLog(String str) {
        if (str != null && !str.isEmpty()) {
            try {
                if (getCronetHttpClient() == null) {
                    return;
                }
                if (C53309iL.LIZJ != null) {
                    Reflect.on((Object) C53309iL.LIZJ).call("reportNetDiagnosisUserLog", new Class[]{String.class}, str);
                    return;
                }
                throw new UnsupportedOperationException("CronetEngine has not been initialized.");
            } catch (Exception e) {
                0HH.LIZ(e);
            }
        }
    }

    public static boolean isValidNetDetect(int i, long j) {
        if (i == 0) {
            return true;
        }
        if (i != 1) {
            if (i != 2) {
                if (i == 3 || i == 4) {
                    return true;
                }
                throw new IllegalArgumentException("Illegal request type: ".concat(String.valueOf(i)));
            } else if (j == 1 || j == 2 || j == 64) {
                return true;
            } else {
                return false;
            }
        } else if ((67 & j) == 0 || (j & -68) != 0) {
            return false;
        } else {
            return true;
        }
    }

    public static IDiagnosisRequest createRequest(int i, String str, int i2, int i3) {
        return createRequest(i, str, i2, 0, i3);
    }

    public static IDiagnosisRequest createRequest(int i, List<String> list, int i2, int i3) {
        if (!isOneTargetRequestType(i)) {
            int i4 = i2;
            if (!isValidNetDetect(i, (long) i4)) {
                throw new IllegalArgumentException("Illegal netDetectType: ".concat(String.valueOf(i4)));
            } else if (i != 1) {
                return null;
            } else {
                return new R5k(1, list, i4, 0, i3);
            }
        } else {
            throw new IllegalArgumentException("Type " + requestTypeToString(i) + " of request should be created with only one target.");
        }
    }

    public static IDiagnosisRequest createRequest(int i, String str, int i2, int i3, int i4) {
        int i5 = i;
        if (isOneTargetRequestType(i5)) {
            int i6 = i2;
            if (!isValidNetDetect(i5, (long) i6)) {
                throw new IllegalArgumentException("Illegal netDetectType: ".concat(String.valueOf(i6)));
            } else if (i5 != 0 && i5 != 2 && i5 != 3 && i5 != 4) {
                return null;
            } else {
                ArrayList arrayList = new ArrayList();
                arrayList.add(str);
                return new R5k(i5, arrayList, i6, i3, i4);
            }
        } else {
            throw new IllegalArgumentException("Type " + requestTypeToString(i5) + " of request should be created with multiple targets.");
        }
    }
}
