package com.ss.android.ugc.aweme.live.livehostimpl;

import X.0HH;
import X.0UV;
import X.0cm;
import X.0yy;
import X.8qV;
import X.8uj;
import X.95y;
import X.9EH;
import X.C0911PTw;
import X.C1076Pa5;
import X.C1077Pa6;
import X.C1079Pa8;
import X.C1082PaB;
import X.C1083PaC;
import X.C1084PaD;
import X.C1085PaE;
import X.C2554Pxv;
import X.ESC;
import X.FKo;
import X.HHP;
import X.JLw;
import X.Ksm;
import X.Ksn;
import X.PDB;
import X.PSE;
import X.PTE;
import X.TXP;
import android.content.Context;
import android.net.Uri;
import com.bytedance.android.livesdk.comp.api.network.INetworkService;
import com.bytedance.android.livesdkapi.host.IHostContext;
import com.bytedance.android.livesdkapi.host.IHostNetwork;
import com.bytedance.android.livesdkapi.ws.LiveWsMessage;
import com.bytedance.common.wschannel.model.WsChannelMsg;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.ugc.aweme.network.RetrofitFactory;
import com.bytedance.retrofit2.mime.TypedByteArray;
import com.bytedance.retrofit2.mime.TypedOutput;
import com.ss.android.common.applog.AppLog;
import com.ss.android.deviceregister.DeviceRegisterManager;
import com.ss.android.ugc.aweme.live.ILiveInnerService;
import com.ss.android.ugc.aweme.ttlive.ILiveApi;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class LiveHostNetwork implements IHostNetwork {
    public volatile ILiveApi LIZ;

    static {
        Covode.recordClassIndex(91221);
    }

    public void onInit() {
    }

    public Map<String, String> getCommonParams() {
        HashMap hashMap = new HashMap();
        8qV.LIZIZ(hashMap, false);
        return hashMap;
    }

    public String getHostDomain() {
        return 0cm.LIZ(ILiveInnerService.class).LIZ();
    }

    private ILiveApi LIZ() {
        if (this.LIZ == null) {
            this.LIZ = (ILiveApi) RetrofitFactory.LIZ().LIZIZ("https://api-va.tiktokv.com").LIZJ().LIZ(ILiveApi.class);
        }
        return this.LIZ;
    }

    public int getVersionCode() {
        Long.valueOf(9EH.LJJIFFI.LJII()).intValue();
        int i = 0;
        try {
            String[] split = 9EH.LJJIFFI.LJIIIIZZ().split("\\.");
            int length = split.length;
            int i2 = 0;
            while (i < length) {
                try {
                    i2 = Integer.parseInt(split[i]) + (i2 * 100);
                    i++;
                } catch (Throwable unused) {
                    i = i2;
                    return i;
                }
            }
            return i2;
        } catch (Throwable unused2) {
            return i;
        }
    }

    /* renamed from: com.ss.android.ugc.aweme.live.livehostimpl.LiveHostNetwork$5  reason: invalid class name */
    public static /* synthetic */ class AnonymousClass5 {
        public static final /* synthetic */ int[] LIZ;

        /* JADX WARNING: Can't wrap try/catch for region: R(10:0|1|2|3|4|5|6|7|8|(3:9|10|12)) */
        /* JADX WARNING: Can't wrap try/catch for region: R(12:0|1|2|3|4|5|6|7|8|9|10|12) */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0018 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x0023 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x002e */
        /* JADX WARNING: Missing exception handler attribute for start block: B:9:0x0039 */
        static {
            /*
                r0 = 91226(0x1645a, float:1.27835E-40)
                com.bytedance.covode.number.Covode.recordClassIndex(r0)
                X.PaH[] r0 = X.C1088PaH.values()
                int r0 = r0.length
                int[] r2 = new int[r0]
                LIZ = r2
                X.PaH r0 = X.C1088PaH.CONNECTION_UNKNOWN     // Catch:{ NoSuchFieldError -> 0x0018 }
                int r1 = r0.ordinal()     // Catch:{ NoSuchFieldError -> 0x0018 }
                r0 = 1
                r2[r1] = r0     // Catch:{ NoSuchFieldError -> 0x0018 }
            L_0x0018:
                int[] r2 = LIZ     // Catch:{ NoSuchFieldError -> 0x0023 }
                X.PaH r0 = X.C1088PaH.CONNECTING     // Catch:{ NoSuchFieldError -> 0x0023 }
                int r1 = r0.ordinal()     // Catch:{ NoSuchFieldError -> 0x0023 }
                r0 = 2
                r2[r1] = r0     // Catch:{ NoSuchFieldError -> 0x0023 }
            L_0x0023:
                int[] r2 = LIZ     // Catch:{ NoSuchFieldError -> 0x002e }
                X.PaH r0 = X.C1088PaH.CONNECT_FAILED     // Catch:{ NoSuchFieldError -> 0x002e }
                int r1 = r0.ordinal()     // Catch:{ NoSuchFieldError -> 0x002e }
                r0 = 3
                r2[r1] = r0     // Catch:{ NoSuchFieldError -> 0x002e }
            L_0x002e:
                int[] r2 = LIZ     // Catch:{ NoSuchFieldError -> 0x0039 }
                X.PaH r0 = X.C1088PaH.CONNECT_CLOSED     // Catch:{ NoSuchFieldError -> 0x0039 }
                int r1 = r0.ordinal()     // Catch:{ NoSuchFieldError -> 0x0039 }
                r0 = 4
                r2[r1] = r0     // Catch:{ NoSuchFieldError -> 0x0039 }
            L_0x0039:
                int[] r2 = LIZ     // Catch:{ NoSuchFieldError -> 0x0044 }
                X.PaH r0 = X.C1088PaH.CONNECTED     // Catch:{ NoSuchFieldError -> 0x0044 }
                int r1 = r0.ordinal()     // Catch:{ NoSuchFieldError -> 0x0044 }
                r0 = 5
                r2[r1] = r0     // Catch:{ NoSuchFieldError -> 0x0044 }
            L_0x0044:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.live.livehostimpl.LiveHostNetwork.AnonymousClass5.<clinit>():void");
        }
    }

    public PTE getLiveRetrofit() {
        0yy.LIZ();
        List<PSE> liveCallAdapter = 0cm.LIZ(INetworkService.class).getLiveCallAdapter();
        0yy.LIZ();
        List<PDB> liveConverter = 0cm.LIZ(INetworkService.class).getLiveConverter();
        C0911PTw LIZIZ = RetrofitFactory.LIZ().LIZIZ("https://api-va.tiktokv.com");
        for (PSE LIZ2 : liveCallAdapter) {
            LIZIZ.LIZ(LIZ2);
        }
        for (PDB LIZ3 : liveConverter) {
            LIZIZ.LIZ(LIZ3);
        }
        0yy.LIZ();
        LIZIZ.LIZ(0cm.LIZ(INetworkService.class).getLiveInterceptor());
        return LIZIZ.LIZJ().LIZ;
    }

    public static Map<String, String> LIZ(List<0UV> list) {
        HashMap hashMap = new HashMap();
        if (list != null) {
            for (0UV next : list) {
                hashMap.put(next.getName(), next.getValue());
            }
        }
        return hashMap;
    }

    public void minorModeInterceptMonitor(String str) {
        if (ESC.LIZLLL()) {
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.putOpt("url", str);
                jSONObject.putOpt("source", "livesdk");
            } catch (JSONException e) {
                0HH.LIZ(e);
            }
            95y.LIZ("ttlive_minor_mode_live", 1, jSONObject);
        }
    }

    public static byte[] LIZ(InputStream inputStream) {
        MethodCollector.i(9881);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        if (inputStream != null) {
            byte[] bArr = new byte[4096];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
        }
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        MethodCollector.o(9881);
        return byteArray;
    }

    public HHP<Ksm> get(String str, List<0UV> list, Object obj) {
        JLw jLw = LIZ().get(str, LIZ(list), obj);
        minorModeInterceptMonitor(str);
        return new Ksn(jLw);
    }

    public TXP registerWsChannel(Context context, String str, Map<String, String> map, final C1077Pa6 pa6) {
        map.put("sid", AppLog.getSessionKey());
        try {
            Uri.Builder buildUpon = Uri.parse(str).buildUpon();
            for (String next : map.keySet()) {
                buildUpon.appendQueryParameter(next, map.get(next));
            }
            str = buildUpon.build().toString();
        } catch (Throwable unused) {
        }
        ArrayList arrayList = new ArrayList();
        arrayList.add(str);
        HashMap hashMap = new HashMap();
        hashMap.put("ttnet_ignore_offline", "1");
        8uj LIZ2 = 8uj.LIZ(10001);
        LIZ2.LJI = "e1bd35ec9db7b8d846de66ed140b1ad9";
        LIZ2.LJ = 9;
        LIZ2.LIZLLL = 9EH.LJIILJJIL;
        LIZ2.LJFF = getVersionCode();
        LIZ2.LJII = DeviceRegisterManager.getDeviceId();
        LIZ2.LJIIIIZZ = DeviceRegisterManager.getInstallId();
        LIZ2.LIZ(arrayList);
        LIZ2.LIZ(hashMap);
        LIZ2.LIZIZ.putAll(new 2(this, 0cm.LIZ(IHostContext.class)));
        final C1082PaB LIZ3 = C2554Pxv.LIZ(context, LIZ2.LIZ(), (FKo) new FKo() {
            static {
                Covode.recordClassIndex(91224);
            }

            public final void LIZ(WsChannelMsg wsChannelMsg) {
                if (wsChannelMsg != null && wsChannelMsg.LJIIL == 10001) {
                    C1076Pa5 pa5 = new C1076Pa5(wsChannelMsg.LJIIL);
                    pa5.LIZIZ = wsChannelMsg.LJ;
                    pa5.LIZLLL = wsChannelMsg.LJI;
                    pa5.LJII = wsChannelMsg.LIZLLL;
                    pa5.LIZJ = wsChannelMsg.LJFF;
                    pa5.LJFF = wsChannelMsg.LJIIIZ;
                    pa5.LJI = wsChannelMsg.LJIIIIZZ;
                    pa5.LJ = wsChannelMsg.LIZ();
                    pa5.LJIIIIZZ = wsChannelMsg.LJIIJJI;
                    if (wsChannelMsg.LJII != null) {
                        for (WsChannelMsg.MsgHeader next : wsChannelMsg.LJII) {
                            pa5.LIZ.put(next.LIZ, next.LIZIZ);
                        }
                    }
                    pa6.LIZ(pa5.LIZIZ());
                }
            }

            public final void LIZ(C1084PaD paD, JSONObject jSONObject) {
                if (paD != null && paD.LIZJ == 10001) {
                    C1085PaE paE = C1085PaE.CONNECTION_UNKNOWN;
                    int i = AnonymousClass5.LIZ[paD.LIZIZ.ordinal()];
                    if (i == 1) {
                        paE = C1085PaE.CONNECTION_UNKNOWN;
                    } else if (i == 2) {
                        paE = C1085PaE.CONNECTING;
                    } else if (i == 3) {
                        paE = C1085PaE.CONNECT_FAILED;
                    } else if (i == 4) {
                        paE = C1085PaE.CONNECT_CLOSED;
                    } else if (i == 5) {
                        paE = C1085PaE.CONNECTED;
                    }
                    pa6.LIZ(paE, jSONObject);
                }
            }
        });
        return new TXP() {
            static {
                Covode.recordClassIndex(91225);
            }

            public final boolean LIZ() {
                return LIZ3.LIZJ();
            }

            public final void LIZIZ() {
                LIZ3.LIZIZ();
            }

            public final void LIZ(LiveWsMessage liveWsMessage) {
                C1079Pa8 pa8 = new C1079Pa8(liveWsMessage.LJIIJ);
                pa8.LIZ = liveWsMessage.LIZJ;
                pa8.LIZJ = liveWsMessage.LJ;
                pa8.LJI = liveWsMessage.LIZIZ;
                pa8.LIZIZ = liveWsMessage.LIZLLL;
                pa8.LJ = liveWsMessage.LJII;
                pa8.LJFF = liveWsMessage.LJI;
                pa8.LIZLLL = liveWsMessage.LIZ();
                pa8.LJII = liveWsMessage.LJIIIZ;
                if (liveWsMessage.LJFF != null) {
                    for (LiveWsMessage.MsgHeader next : liveWsMessage.LJFF) {
                        pa8.LIZ(next.LIZ, next.LIZIZ);
                    }
                }
                LIZ3.LIZ(pa8.LIZIZ(), (C1083PaC) null);
            }
        };
    }

    public HHP<Ksm> downloadFile(boolean z, int i, String str, List<0UV> list, Object obj) {
        boolean z2 = z;
        String str2 = str;
        JLw downloadFile = LIZ().downloadFile(z2, i, str2, LIZ(list), obj);
        minorModeInterceptMonitor(str2);
        return new Ksn(downloadFile);
    }

    public HHP<Ksm> post(String str, List<0UV> list, String str2, byte[] bArr, Object obj) {
        JLw post = LIZ().post(str, new TypedByteArray(str2, bArr, new String[0]), LIZ(list), obj);
        minorModeInterceptMonitor(str);
        return new Ksn(post);
    }

    public HHP<Ksm> uploadFile(int i, String str, List<0UV> list, String str2, byte[] bArr, long j, String str3) {
        final byte[] bArr2 = bArr;
        final String str4 = str2;
        final String str5 = str3;
        final long j2 = j;
        JLw postMultiPart = LIZ().postMultiPart(i, str, LIZ(list), new TypedOutput() {
            static {
                Covode.recordClassIndex(91222);
            }

            public final String fileName() {
                return null;
            }

            public final long length() {
                return j2;
            }

            public final String md5Stub() {
                return str5;
            }

            public final String mimeType() {
                return str4;
            }

            public final void writeTo(OutputStream outputStream) {
                MethodCollector.i(8942);
                outputStream.write(bArr2);
                MethodCollector.o(8942);
            }
        });
        minorModeInterceptMonitor(str);
        return new Ksn(postMultiPart);
    }
}
