package com.pgl.ssdk;

import android.content.Context;
import android.text.TextUtils;
import android.util.Pair;
import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.util.Locale;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public abstract class am {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static String f24687a;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private Context f24689c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private int f24690d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f24691e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private byte[] f24692f;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private HttpURLConnection f24688b = null;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f24693g = -1;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private byte[] f24694h = null;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private int f24695i = 10000;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f24696j = 0;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f24697k = 2;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private boolean f24698l = true;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private Runnable f24699m = new a();

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            if (am.this.f24696j == 0) {
                an.c();
            }
            if (am.this.c() || am.this.f24696j >= am.this.f24697k) {
                return;
            }
            am.b(am.this);
            ar.a(this);
        }
    }

    public am(Context context) {
        this.f24689c = context;
    }

    public static /* synthetic */ int b(am amVar) {
        int i10 = amVar.f24696j;
        amVar.f24696j = i10 + 1;
        return i10;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:54:0x00db A[EXC_TOP_SPLITTER, PHI: r0
      0x00db: PHI (r0v3 java.io.InputStream) = (r0v2 java.io.InputStream), (r0v17 java.io.InputStream) binds: [B:44:0x00d8, B:38:0x00cb] A[DONT_GENERATE, DONT_INLINE], SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public boolean c() {
        InputStream inputStream;
        String strA;
        byte[] bArrA;
        int i10;
        try {
            strA = an.a(this.f24689c);
        } catch (Throwable unused) {
            inputStream = null;
        }
        if (TextUtils.isEmpty(strA)) {
            an.b(this.f24689c);
            HttpURLConnection httpURLConnection = this.f24688b;
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
                this.f24688b = null;
            }
            return false;
        }
        String strConcat = strA + a();
        if (!strConcat.startsWith("https://") && !strConcat.startsWith("http://")) {
            strConcat = "https://".concat(strConcat);
        }
        URL url = new URL(strConcat);
        HttpURLConnection httpURLConnection2 = (HttpURLConnection) (this.f24698l ? url.openConnection(Proxy.NO_PROXY) : url.openConnection());
        this.f24688b = httpURLConnection2;
        httpURLConnection2.setConnectTimeout(this.f24695i);
        this.f24688b.setReadTimeout(this.f24695i);
        a(this.f24690d);
        b(this.f24691e);
        byte[] bArr = this.f24692f;
        if (bArr != null && bArr.length > 0) {
            this.f24688b.setDoOutput(true);
            OutputStream outputStream = this.f24688b.getOutputStream();
            outputStream.write(this.f24692f);
            outputStream.flush();
            outputStream.close();
        }
        this.f24688b.connect();
        this.f24693g = this.f24688b.getResponseCode();
        inputStream = this.f24688b.getInputStream();
        try {
            bArrA = a(inputStream);
            this.f24694h = bArrA;
            i10 = this.f24693g;
        } catch (Throwable unused2) {
            HttpURLConnection httpURLConnection3 = this.f24688b;
            if (httpURLConnection3 != null) {
                httpURLConnection3.disconnect();
                this.f24688b = null;
            }
            if (inputStream != null) {
            }
        }
        if (i10 == 200) {
            a(i10, bArrA);
            HttpURLConnection httpURLConnection4 = this.f24688b;
            if (httpURLConnection4 != null) {
                httpURLConnection4.disconnect();
                this.f24688b = null;
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Throwable unused3) {
                }
            }
            return true;
        }
        HttpURLConnection httpURLConnection5 = this.f24688b;
        if (httpURLConnection5 != null) {
            httpURLConnection5.disconnect();
            this.f24688b = null;
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Throwable unused4) {
            }
        }
        an.b(this.f24689c);
        return false;
    }

    public abstract String a();

    public abstract void a(int i10, byte[] bArr);

    private void a(int i10) throws ProtocolException {
        this.f24688b.setRequestMethod(i10 != 1 ? i10 != 3 ? i10 != 4 ? i10 != 5 ? i10 != 6 ? "GET" : "TRACE" : "HEAD" : "DELETE" : "PUT" : HlsMediaPlaylist.Interstitial.CUE_TRIGGER_POST);
    }

    private void b() {
        Object obj;
        if (this.f24688b == null) {
            return;
        }
        JSONObject jSONObject = new JSONObject();
        try {
            if (!TextUtils.isEmpty(f24687a)) {
                jSONObject.put("ipv6", f24687a);
            }
            if (!TextUtils.isEmpty(com.pgl.ssdk.ces.b.c())) {
                jSONObject.put("gaid", com.pgl.ssdk.ces.b.c());
            }
            jSONObject.put("region", an.a());
            Pair<Integer, String> pairA = aq.a(jSONObject.toString());
            if (pairA == null || (obj = pairA.first) == null || pairA.second == null) {
                return;
            }
            this.f24688b.addRequestProperty("cypher", String.valueOf(obj));
            this.f24688b.addRequestProperty("transfer-param", (String) pairA.second);
        } catch (Exception unused) {
        }
    }

    private void b(int i10) {
        HttpURLConnection httpURLConnection;
        StringBuilder sb;
        String str;
        String str2 = i10 != 1 ? i10 != 2 ? "" : "application/octet-stream" : "application/json; charset=utf-8";
        if (!str2.isEmpty()) {
            this.f24688b.addRequestProperty(RtspHeaders.CONTENT_TYPE, str2);
        }
        String strB = an.b();
        if (strB != null) {
            this.f24688b.addRequestProperty("x-pangle-target-idc", strB);
        }
        b();
        try {
            String language = Locale.getDefault().getLanguage();
            if (language.equalsIgnoreCase("zh")) {
                httpURLConnection = this.f24688b;
                sb = new StringBuilder();
                sb.append(Locale.getDefault().toString());
                sb.append(",");
                sb.append(language);
                str = ";q=0.9";
            } else {
                httpURLConnection = this.f24688b;
                sb = new StringBuilder();
                sb.append(Locale.getDefault().toString());
                sb.append(",");
                sb.append(language);
                str = ";q=0.9,en-US;q=0.6,en;q=0.4";
            }
            sb.append(str);
            httpURLConnection.addRequestProperty("Accept-Language", sb.toString());
        } catch (Throwable unused) {
        }
    }

    public void a(int i10, int i11, byte[] bArr) {
        b(i10, i11, bArr);
        ar.a(this.f24699m);
    }

    public static synchronized void a(String str) {
        if (!TextUtils.isEmpty(str) && !str.equals(f24687a)) {
            f24687a = str;
        }
    }

    private void b(int i10, int i11, byte[] bArr) {
        this.f24690d = i10;
        this.f24691e = i11;
        this.f24692f = bArr;
    }

    private byte[] a(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[1024];
        while (true) {
            int i10 = inputStream.read(bArr, 0, 1024);
            if (i10 <= 0) {
                return byteArrayOutputStream.toByteArray();
            }
            byteArrayOutputStream.write(bArr, 0, i10);
        }
    }
}
