package epson.print.inkrpln;

import android.content.Context;
import android.net.Uri;
import android.os.Build;



import android.util.Base64;
import com.epson.mobilephone.common.escpr.EPS_REDYINKINFO;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import epson.common.Utils;
import epson.print.R;
import epson.print.inkrpln.GoEpsonClient;
import epson.print.inkrpln.InkRplnInfoClient;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
import org.apache.commons.lang.CharEncoding;

public class InkReplenishSystem {
    private static InkReplenishSystem sInkReplenishSystem;
    private GoEpsonClient mGoEpsonClient;
    private InkRplnInfoClient mInkRsInfoClient;
    private InkRplnRepository mIrsRepository;
    private PrinterAdapter mPrinterAdapter;

    @VisibleForTesting
    InkReplenishSystem(PrinterAdapter printerAdapter, InkRplnRepository inkRplnRepository, GoEpsonClient goEpsonClient, InkRplnInfoClient inkRplnInfoClient) {
        this.mPrinterAdapter = printerAdapter;
        this.mIrsRepository = inkRplnRepository;
        this.mGoEpsonClient = goEpsonClient;
        this.mInkRsInfoClient = inkRplnInfoClient;
    }

    public InkReplenishSystem(PrinterAdapter printerAdapter, InkRplnRepository inkRplnRepository) {
        this.mPrinterAdapter = printerAdapter;
        this.mIrsRepository = inkRplnRepository;
        this.mGoEpsonClient = new GoEpsonClient();
        this.mInkRsInfoClient = new InkRplnInfoClient();
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    @NonNull
    public GoEpsonClient.ReadyInkParams getGoEpsonParams(@NonNull Context context) {
        GoEpsonClient.ReadyInkParams readyInkParams = new GoEpsonClient.ReadyInkParams();
        Locale locale = Locale.getDefault();
        readyInkParams.country = locale.getCountry();
        readyInkParams.language = locale.getLanguage();
        readyInkParams.printerSerial = this.mPrinterAdapter.getSerialNo(context);
        readyInkParams.printerName = this.mPrinterAdapter.getNonRemotePrinterDeviceId(context);
        readyInkParams.androidModel = Build.MODEL;
        return readyInkParams;
    }

    public synchronized boolean tryRecordPrinterInfoFromServer(@NonNull Context context) throws IOException {
        clearServerReplyPrinterInfo(context);
        if (!isInSupportedArea()) {
            return false;
        }
        if (!checkPrinterAndNetworkConnectivity(context)) {
            return false;
        }
        String serialNo = this.mPrinterAdapter.getSerialNo(context);
        if (serialNo == null) {
            return false;
        }
        EPS_REDYINKINFO readyInkInfo = this.mPrinterAdapter.getReadyInkInfo();
        if (readyInkInfo == null) {
            return false;
        }
        if (readyInkInfo.subscriptionStatus != 0) {
            this.mIrsRepository.saveBuyInkButtonOffReadyInkOff(context);
            return false;
        }
        String redirectLocation = this.mGoEpsonClient.getRedirectLocation(getGoEpsonParams(context), useGoEpsonTls(context));
        this.mIrsRepository.saveInkReplenishServerUrl(context, redirectLocation);
        if (redirectLocation == null) {
            return false;
        }
        this.mPrinterAdapter.activateEma(readyInkInfo);
        InkRplnInfoClient.InkRsInfo inkDsInfo = this.mInkRsInfoClient.getInkDsInfo(serialNo, redirectLocation);
        if (inkDsInfo == null) {
            return false;
        }
        this.mIrsRepository.saveData(context, inkDsInfo);
        return true;
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public boolean checkPrinterAndNetworkConnectivity(@NonNull Context context) {
        if (Utils.isConnectedWifi(context) && !WiFiDirectManager.isSimpleAP(context) && Utils.isOnline(context) && !this.mPrinterAdapter.isRemotePrinter(context)) {
            return true;
        }
        return false;
    }

    public synchronized void clearServerReplyPrinterInfo(Context context) {
        this.mIrsRepository.clearServerData(context);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:14:0x001c, code lost:
        return r1;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized boolean needsDisplayInvitationDialog(@android.support.annotation.NonNull android.content.Context r3) {
        /*
            r2 = this;
            monitor-enter(r2)
            epson.print.inkrpln.InkRplnRepository r0 = r2.mIrsRepository     // Catch:{ all -> 0x001d }
            boolean r0 = r0.getInvitationDialogDisabledValue(r3)     // Catch:{ all -> 0x001d }
            r1 = 0
            if (r0 == 0) goto L_0x000c
            monitor-exit(r2)
            return r1
        L_0x000c:
            epson.print.inkrpln.InkRplnRepository r0 = r2.mIrsRepository     // Catch:{ all -> 0x001d }
            epson.print.inkrpln.InkRplnInfoClient$InkRsInfo r3 = r0.getInfo(r3)     // Catch:{ all -> 0x001d }
            if (r3 == 0) goto L_0x001b
            boolean r3 = r3.needToDisplayInvitationDialog()     // Catch:{ all -> 0x001d }
            if (r3 == 0) goto L_0x001b
            r1 = 1
        L_0x001b:
            monitor-exit(r2)
            return r1
        L_0x001d:
            r3 = move-exception
            monitor-exit(r2)
            throw r3
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.InkReplenishSystem.needsDisplayInvitationDialog(android.content.Context):boolean");
    }

    public synchronized void setServerCheckDisabled(@NonNull Context context) {
        this.mIrsRepository.saveInvitationDialogDisableValue(context, true);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:24:0x0035, code lost:
        return null;
     */
    @android.support.annotation.Nullable
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized android.net.Uri refreshReplenInfoAndGetJumpUrl(@android.support.annotation.NonNull android.content.Context r5) {
        /*
            r4 = this;
            monitor-enter(r4)
            epson.print.inkrpln.InkRplnRepository r0 = r4.mIrsRepository     // Catch:{ all -> 0x0038 }
            java.lang.String r0 = r0.getInkReplenishServerUrl(r5)     // Catch:{ all -> 0x0038 }
            r1 = 0
            if (r0 != 0) goto L_0x000c
            monitor-exit(r4)
            return r1
        L_0x000c:
            epson.print.inkrpln.PrinterAdapter r2 = r4.mPrinterAdapter     // Catch:{ all -> 0x0038 }
            java.lang.String r2 = r2.getSerialNo(r5)     // Catch:{ all -> 0x0038 }
            epson.print.inkrpln.InkRplnInfoClient r3 = r4.mInkRsInfoClient     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            epson.print.inkrpln.InkRplnInfoClient$InkRsInfo r0 = r3.getInkDsInfo(r2, r0)     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            if (r0 == 0) goto L_0x0034
            java.lang.String r3 = r0.landingUrl     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            if (r3 == 0) goto L_0x0034
            if (r2 != 0) goto L_0x0021
            goto L_0x0034
        L_0x0021:
            epson.print.inkrpln.InkRplnRepository r3 = r4.mIrsRepository     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            r3.saveData(r5, r0)     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            java.lang.String r5 = r0.landingUrl     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            boolean r0 = r0.appendSerial     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            if (r0 == 0) goto L_0x002d
            goto L_0x002e
        L_0x002d:
            r2 = r1
        L_0x002e:
            android.net.Uri r5 = r4.getReadyInkUri(r5, r2)     // Catch:{ IOException | IllegalArgumentException -> 0x0036 }
            monitor-exit(r4)
            return r5
        L_0x0034:
            monitor-exit(r4)
            return r1
        L_0x0036:
            monitor-exit(r4)
            return r1
        L_0x0038:
            r5 = move-exception
            monitor-exit(r4)
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.InkReplenishSystem.refreshReplenInfoAndGetJumpUrl(android.content.Context):android.net.Uri");
    }

    /* JADX WARNING: Code restructure failed: missing block: B:15:0x0024, code lost:
        return null;
     */
    @android.support.annotation.Nullable
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized android.net.Uri getReadyInkUriFromSavedData(@android.support.annotation.NonNull android.content.Context r4) {
        /*
            r3 = this;
            monitor-enter(r3)
            epson.print.inkrpln.InkRplnRepository r0 = r3.mIrsRepository     // Catch:{ all -> 0x0025 }
            epson.print.inkrpln.InkRplnInfoClient$InkRsInfo r0 = r0.getInfo(r4)     // Catch:{ all -> 0x0025 }
            r1 = 0
            if (r0 == 0) goto L_0x0023
            java.lang.String r2 = r0.landingUrl     // Catch:{ all -> 0x0025 }
            if (r2 != 0) goto L_0x000f
            goto L_0x0023
        L_0x000f:
            epson.print.inkrpln.PrinterAdapter r2 = r3.mPrinterAdapter     // Catch:{ all -> 0x0025 }
            java.lang.String r4 = r2.getSerialNo(r4)     // Catch:{ all -> 0x0025 }
            java.lang.String r2 = r0.landingUrl     // Catch:{ all -> 0x0025 }
            boolean r0 = r0.appendSerial     // Catch:{ all -> 0x0025 }
            if (r0 == 0) goto L_0x001c
            goto L_0x001d
        L_0x001c:
            r4 = r1
        L_0x001d:
            android.net.Uri r4 = r3.getReadyInkUri(r2, r4)     // Catch:{ all -> 0x0025 }
            monitor-exit(r3)
            return r4
        L_0x0023:
            monitor-exit(r3)
            return r1
        L_0x0025:
            r4 = move-exception
            monitor-exit(r3)
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.InkReplenishSystem.getReadyInkUriFromSavedData(android.content.Context):android.net.Uri");
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public Uri getReadyInkUri(@NonNull String str, @Nullable String str2) {
        Uri.Builder buildUpon = Uri.parse(str).buildUpon();
        if (str2 != null) {
            buildUpon.appendQueryParameter("sn", encodePrinterSerial(str2));
        }
        return buildUpon.build();
    }

    @NonNull
    static String encodePrinterSerial(@NonNull String str) {
        return Base64.encodeToString(str.getBytes(Charset.forName(CharEncoding.US_ASCII)), 2);
    }

    public boolean isInSupportedArea() {
        return isSupportedCountry(Locale.getDefault().getCountry());
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public boolean isSupportedCountry(String str) {
        return Arrays.asList(new String[]{Locale.US.getCountry(), Locale.CANADA.getCountry(), Locale.UK.getCountry(), Locale.FRANCE.getCountry(), Locale.ITALY.getCountry(), Locale.GERMANY.getCountry(), "ES", "PT", "NL", "CZ", "DK", "FI", "NO", "PL", "SE", "AT", "BE", "IE", "CH"}).contains(str);
    }

    static boolean isInkReplenishSystemEnabled(@NonNull Context context) {
        return context.getResources().getBoolean(R.bool.ink_replenish_system_enabled);
    }

    static boolean useGoEpsonTls(@NonNull Context context) {
        return context.getResources().getBoolean(R.bool.ink_replen_go_epson_use_tls);
    }
}
