package epson.print.inkrpln;

import android.net.Uri;


import com.epson.iprint.prtlogger.Analytics;
import epson.print.CommonDefine;
import epson.print.IprintApplication;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class GoEpsonClient {
    private static final int CONNECTION_TIMEOUT = 10000;
    private static final String ERROR_HOST_NAME = "www.epson.com";
    private static final int READ_TIMEOUT = 10000;

    public static class ReadyInkParams {
        public String androidModel;
        public String country;
        public String ctc;
        public String language;
        public String printerName;
        public String printerSerial;
    }

    public String getRedirectLocation(@NonNull ReadyInkParams readyInkParams, boolean z) {
        try {
            String goEpsonRedirectLocation = getGoEpsonRedirectLocation(new URL(makeUrl(readyInkParams, z)));
            if (checkGoEpsonResultUrl(goEpsonRedirectLocation)) {
                return goEpsonRedirectLocation;
            }
            return null;
        } catch (MalformedURLException unused) {
            return null;
        }
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public boolean checkGoEpsonResultUrl(String str) {
        if (str == null) {
            return false;
        }
        try {
            URL url = new URL(str);
            if (!ERROR_HOST_NAME.equals(url.getHost())) {
                return true;
            }
            if (CommonDefine.SLASH.equals(url.getPath()) || url.getPath().length() <= 0) {
                return false;
            }
            return true;
        } catch (MalformedURLException unused) {
            return false;
        }
    }

    public String makeUrl(@NonNull ReadyInkParams readyInkParams, boolean z) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(z ? "https" : Analytics.EXTENSION_STRING_WEB);
        builder.encodedAuthority(getServerAndPortString());
        builder.path("redirect.aspx");
        if (readyInkParams.language != null) {
            builder.appendQueryParameter("LG2", readyInkParams.language);
        }
        if (readyInkParams.country != null) {
            builder.appendQueryParameter("CN2", readyInkParams.country);
        }
        builder.appendQueryParameter("CTC", "ReadyInkInvitation");
        if (readyInkParams.printerName != null) {
            builder.appendQueryParameter("PRN", goEpsonEncodeString(readyInkParams.printerName));
        }
        builder.appendQueryParameter("OSC", CommonDefine.ARD);
        builder.appendQueryParameter("OSV", "ARDAPI_1.0.26");
        if (readyInkParams.androidModel != null) {
            builder.appendQueryParameter("OATR", goEpsonEncodeString(readyInkParams.androidModel));
        }
        if (readyInkParams.printerSerial != null && readyInkParams.printerSerial.length() >= 4) {
            builder.appendQueryParameter("SID", readyInkParams.printerSerial.substring(0, 4));
        }
        return builder.build().toString();
    }

    /* access modifiers changed from: protected */
    @VisibleForTesting
    public String getServerAndPortString() {
        return IprintApplication.getInstance().getGoEpsonServerName();
    }

    @NonNull
    private static String goEpsonEncodeString(@NonNull String str) {
        return str.replace(" ", CommonDefine.UNDER_BAR);
    }

    /* access modifiers changed from: package-private */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x003d  */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x0044  */
    @android.support.annotation.Nullable
    @android.support.annotation.VisibleForTesting
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.String getGoEpsonRedirectLocation(@android.support.annotation.NonNull java.net.URL r4) {
        /*
            r3 = this;
            r0 = 0
            java.net.HttpURLConnection r4 = r3.getGoEpsonConnection(r4)     // Catch:{ IOException | IllegalArgumentException -> 0x0041, all -> 0x0037 }
            r1 = 10000(0x2710, float:1.4013E-41)
            r4.setReadTimeout(r1)     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            r4.setConnectTimeout(r1)     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            r1 = 0
            r4.setInstanceFollowRedirects(r1)     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            java.lang.String r1 = "GET"
            r4.setRequestMethod(r1)     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            r4.connect()     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            int r1 = r4.getResponseCode()     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            switch(r1) {
                case 301: goto L_0x0026;
                case 302: goto L_0x0026;
                default: goto L_0x0020;
            }
        L_0x0020:
            if (r4 == 0) goto L_0x0032
            r4.disconnect()
            goto L_0x0032
        L_0x0026:
            java.lang.String r1 = "Location"
            java.lang.String r0 = r4.getHeaderField(r1)     // Catch:{ IOException | IllegalArgumentException -> 0x0035, all -> 0x0033 }
            if (r4 == 0) goto L_0x0031
            r4.disconnect()
        L_0x0031:
            return r0
        L_0x0032:
            return r0
        L_0x0033:
            r0 = move-exception
            goto L_0x003b
        L_0x0035:
            goto L_0x0042
        L_0x0037:
            r4 = move-exception
            r2 = r0
            r0 = r4
            r4 = r2
        L_0x003b:
            if (r4 == 0) goto L_0x0040
            r4.disconnect()
        L_0x0040:
            throw r0
        L_0x0041:
            r4 = r0
        L_0x0042:
            if (r4 == 0) goto L_0x0047
            r4.disconnect()
        L_0x0047:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.GoEpsonClient.getGoEpsonRedirectLocation(java.net.URL):java.lang.String");
    }

    private HttpURLConnection getGoEpsonConnection(@NonNull URL url) throws IOException {
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        httpsURLConnection.setSSLSocketFactory(new LocalSSLSocketFactory(httpsURLConnection.getSSLSocketFactory()));
        return httpsURLConnection;
    }
}
