package epson.print;

import android.content.Context;

import com.epson.iprint.storage.StorageSecureStore;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import epson.common.Constants;
import epson.common.ExternalFileUtils;
import epson.common.Utils;
import epson.print.Util.EPLog;
import epson.print.ecclient.EpsonConnectAccess;
import epson.scan.lib.ScanSettingHelper;

/**
 * 爱普生打印管理器
 */
public class EPPrinterManager {
    private static final String TAG = "EPPrinterManager";
    private static ArrayList<EPPrinterInfo> deletedRemotePrinterList;
    private static ArrayList<EPPrinterInfo> ipPrinterList;
    private static ArrayList<EPPrinterInfo> remotePrinterList;
    Context context;

    public EPPrinterManager(Context context2) {
        StorageSecureStore.initSharedSecureStore(context2);
        context = context2;
    }

    public ArrayList<EPPrinterInfo> getRemotePrinterList() {
        ArrayList<EPPrinterInfo> arrayList = remotePrinterList;
        if (arrayList != null) {
            return arrayList;
        }
        remotePrinterList = loadPrinterList(ExternalFileUtils.getInstance(context).getRemotePrintersInfo(), 2);
        return remotePrinterList;
    }

    public ArrayList<EPPrinterInfo> getIpPrinterList() {
        ArrayList<EPPrinterInfo> arrayList = ipPrinterList;
        if (arrayList != null) {
            return arrayList;
        }
        ipPrinterList = loadPrinterList(ExternalFileUtils.getInstance(context).getIpPrintersInfo(), 3);
        return ipPrinterList;
    }

    public String getIpPrinterUserName(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        Iterator<EPPrinterInfo> it = getIpPrinterList().iterator();
        while (it.hasNext()) {
            EPPrinterInfo next = it.next();
            if (str.equals(next.scannerID)) {
                return next.userDefName;
            }
        }
        return null;
    }

    public boolean doseContainScannerIdInIpPrinterList(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        Iterator<EPPrinterInfo> it = getIpPrinterList().iterator();
        while (it.hasNext()) {
            if (str.equals(it.next().scannerID)) {
                return true;
            }
        }
        return false;
    }

    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v4, resolved type: epson.print.EPPrinterInfo} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r4v6, resolved type: java.io.ObjectInputStream} */
    /* JADX WARNING: type inference failed for: r4v2 */
    /* JADX WARNING: type inference failed for: r4v3, types: [java.io.ObjectInputStream] */
    /* JADX WARNING: type inference failed for: r4v5 */
    /* JADX WARNING: type inference failed for: r4v7 */
    /* JADX WARNING: type inference failed for: r4v8 */
    /* JADX WARNING: type inference failed for: r4v9 */
    /* JADX WARNING: type inference failed for: r4v10 */
    /* JADX WARNING: Code restructure failed: missing block: B:54:0x00c1, code lost:
        r7 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:55:0x00c2, code lost:
        r4 = r2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:57:0x00c5, code lost:
        r4.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:59:0x00ca, code lost:
        r3.close();
     */
    /* JADX WARNING: Failed to insert additional move for type inference */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Multi-variable type inference failed */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x006b A[SYNTHETIC, Splitter:B:41:0x006b] */
    /* JADX WARNING: Removed duplicated region for block: B:44:0x0070 A[Catch:{ Exception -> 0x00ce }] */
    /* JADX WARNING: Removed duplicated region for block: B:46:0x0073 A[Catch:{ Exception -> 0x00ce }] */
    /* JADX WARNING: Removed duplicated region for block: B:57:0x00c5 A[Catch:{ Exception -> 0x00ce }] */
    /* JADX WARNING: Removed duplicated region for block: B:59:0x00ca A[Catch:{ Exception -> 0x00ce }] */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x00bd A[SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private ArrayList<EPPrinterInfo> loadPrinterList(String r7, int r8) {
        /*
            r6 = this;
            java.util.ArrayList r0 = new java.util.ArrayList
            r0.<init>()
            java.io.File r1 = new java.io.File     // Catch:{ Exception -> 0x00ce }
            r1.<init>(r7)     // Catch:{ Exception -> 0x00ce }
            java.io.File[] r7 = r1.listFiles()     // Catch:{ Exception -> 0x00ce }
            if (r7 != 0) goto L_0x0011
            return r0
        L_0x0011:
            r0.clear()     // Catch:{ Exception -> 0x00ce }
            r1 = 0
        L_0x0015:
            int r2 = r7.length     // Catch:{ Exception -> 0x00ce }
            if (r1 >= r2) goto L_0x00d5
            epson.print.EPPrinterInfo r2 = new epson.print.EPPrinterInfo     // Catch:{ Exception -> 0x00ce }
            r2.<init>()     // Catch:{ Exception -> 0x00ce }
            java.io.FileInputStream r3 = new java.io.FileInputStream     // Catch:{ Exception -> 0x00ce }
            r4 = r7[r1]     // Catch:{ Exception -> 0x00ce }
            java.lang.String r4 = r4.getAbsolutePath()     // Catch:{ Exception -> 0x00ce }
            r3.<init>(r4)     // Catch:{ Exception -> 0x00ce }
            r4 = 0
            r2.readJSON(r3)     // Catch:{ JSONException -> 0x003c, Exception -> 0x0034 }
            r3.close()     // Catch:{ Exception -> 0x00ce }
            r4 = r2
            goto L_0x0071
        L_0x0031:
            r7 = move-exception
            goto L_0x00c3
        L_0x0034:
            r2 = move-exception
            r2.printStackTrace()     // Catch:{ all -> 0x0031 }
        L_0x0038:
            r3.close()     // Catch:{ Exception -> 0x00ce }
            goto L_0x0071
        L_0x003c:
            r2 = move-exception
            r2.printStackTrace()     // Catch:{ all -> 0x0031 }
            r3.close()     // Catch:{ all -> 0x0031 }
            java.io.FileInputStream r3 = new java.io.FileInputStream     // Catch:{ Exception -> 0x0063, all -> 0x0060 }
            r2 = r7[r1]     // Catch:{ Exception -> 0x0063, all -> 0x0060 }
            java.lang.String r2 = r2.getAbsolutePath()     // Catch:{ Exception -> 0x0063, all -> 0x0060 }
            r3.<init>(r2)     // Catch:{ Exception -> 0x0063, all -> 0x0060 }
            java.io.ObjectInputStream r2 = new java.io.ObjectInputStream     // Catch:{ Exception -> 0x005d }
            r2.<init>(r3)     // Catch:{ Exception -> 0x005d }
            java.lang.Object r5 = r2.readObject()     // Catch:{ Exception -> 0x005b }
            epson.print.EPPrinterInfo r5 = (epson.print.EPPrinterInfo) r5     // Catch:{ Exception -> 0x005b }
            r4 = r5
            goto L_0x0069
        L_0x005b:
            r5 = move-exception
            goto L_0x0066
        L_0x005d:
            r5 = move-exception
            r2 = r4
            goto L_0x0066
        L_0x0060:
            r7 = move-exception
            r3 = r4
            goto L_0x00c3
        L_0x0063:
            r5 = move-exception
            r2 = r4
            r3 = r2
        L_0x0066:
            r5.printStackTrace()     // Catch:{ all -> 0x00c1 }
        L_0x0069:
            if (r2 == 0) goto L_0x006e
            r2.close()     // Catch:{ Exception -> 0x00ce }
        L_0x006e:
            if (r3 == 0) goto L_0x0071
            goto L_0x0038
        L_0x0071:
            if (r4 == 0) goto L_0x00bd
            r2 = 2
            if (r8 != r2) goto L_0x00ba
            com.epson.iprint.storage.StorageSecureStore r2 = com.epson.iprint.storage.StorageSecureStore.getSharedSecureStore()     // Catch:{ Exception -> 0x00ce }
            java.lang.String r3 = r4.printerEmailAddress     // Catch:{ Exception -> 0x00ce }
            java.lang.String r3 = r3.toLowerCase()     // Catch:{ Exception -> 0x00ce }
            java.lang.String r2 = r2.fetch(r3)     // Catch:{ Exception -> 0x00ce }
            r4.printerAccessKey = r2     // Catch:{ Exception -> 0x00ce }
            java.lang.String r2 = r4.printerAccessKey     // Catch:{ Exception -> 0x00ce }
            if (r2 == 0) goto L_0x00b6
            java.lang.String r2 = "EPPrinterManager"
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x00ce }
            r3.<init>()     // Catch:{ Exception -> 0x00ce }
            java.lang.String r5 = "Success: fetch printerAccessKey. ("
            r3.append(r5)     // Catch:{ Exception -> 0x00ce }
            java.lang.String r5 = r4.printerEmailAddress     // Catch:{ Exception -> 0x00ce }
            java.lang.String r5 = r5.toLowerCase()     // Catch:{ Exception -> 0x00ce }
            r3.append(r5)     // Catch:{ Exception -> 0x00ce }
            java.lang.String r5 = " = "
            r3.append(r5)     // Catch:{ Exception -> 0x00ce }
            java.lang.String r5 = r4.printerAccessKey     // Catch:{ Exception -> 0x00ce }
            r3.append(r5)     // Catch:{ Exception -> 0x00ce }
            java.lang.String r5 = ")"
            r3.append(r5)     // Catch:{ Exception -> 0x00ce }
            java.lang.String r3 = r3.toString()     // Catch:{ Exception -> 0x00ce }
            epson.print.Util.EPLog.i(r2, r3)     // Catch:{ Exception -> 0x00ce }
            goto L_0x00ba
        L_0x00b6:
            java.lang.String r2 = ""
            r4.printerAccessKey = r2     // Catch:{ Exception -> 0x00ce }
        L_0x00ba:
            r0.add(r4)     // Catch:{ Exception -> 0x00ce }
        L_0x00bd:
            int r1 = r1 + 1
            goto L_0x0015
        L_0x00c1:
            r7 = move-exception
            r4 = r2
        L_0x00c3:
            if (r4 == 0) goto L_0x00c8
            r4.close()     // Catch:{ Exception -> 0x00ce }
        L_0x00c8:
            if (r3 == 0) goto L_0x00cd
            r3.close()     // Catch:{ Exception -> 0x00ce }
        L_0x00cd:
            throw r7     // Catch:{ Exception -> 0x00ce }
        L_0x00ce:
            java.lang.String r7 = "EPPrinterManager"
            java.lang.String r8 = "loadPrinterList(): error."
            epson.print.Util.EPLog.e((java.lang.String) r7, (java.lang.String) r8)
        L_0x00d5:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.EPPrinterManager.loadPrinterList(java.lang.String, int):java.util.ArrayList");
    }

    public EPPrinterInfo loadRemotePrinterInfo(String str) {
        Iterator<EPPrinterInfo> it = getRemotePrinterList().iterator();
        while (it.hasNext()) {
            EPPrinterInfo next = it.next();
            if (next.printerEmailAddress.toLowerCase().equals(str.toLowerCase())) {
                return next;
            }
        }
        return null;
    }

    public EPPrinterInfo loadIpPrinterInfo(String str) {
        Iterator<EPPrinterInfo> it = getIpPrinterList().iterator();
        while (it.hasNext()) {
            EPPrinterInfo next = it.next();
            if (next.printerID.equals(str)) {
                return next;
            }
        }
        return null;
    }

    public void saveRemotePrinterInfo(EPPrinterInfo ePPrinterInfo) {
        ePPrinterInfo.printerLocation = 2;
        ArrayList<EPPrinterInfo> remotePrinterList2 = getRemotePrinterList();
        for (int i = 0; i < remotePrinterList2.size(); i++) {
            if (remotePrinterList2.get(i).printerEmailAddress.toLowerCase().equals(ePPrinterInfo.printerEmailAddress.toLowerCase())) {
                remotePrinterList2.set(i, ePPrinterInfo);
                return;
            }
        }
        remotePrinterList2.add(ePPrinterInfo);
    }

    public void saveIPPrinterInfo(String str, EPPrinterInfo ePPrinterInfo) {
        ePPrinterInfo.printerLocation = 3;
        ArrayList<EPPrinterInfo> ipPrinterList2 = getIpPrinterList();
        for (int i = 0; i < ipPrinterList2.size(); i++) {
            if (ipPrinterList2.get(i).printerID.equals(str)) {
                ipPrinterList2.set(i, ePPrinterInfo);
                return;
            }
        }
        ipPrinterList2.add(ePPrinterInfo);
    }

    public void commitRemotePrinterInfo() {
        if (remotePrinterList != null) {
            EPLog.i(TAG, "commitRemotePrinterInfo() called");
            if (deletedRemotePrinterList != null) {
                for (int i = 0; i < deletedRemotePrinterList.size(); i++) {
                    String str = deletedRemotePrinterList.get(i).printerEmailAddress;
                    if (StorageSecureStore.getSharedSecureStore().revoke(str.toLowerCase())) {
                        EPLog.i(TAG, "Success: remove printerAccessKey. (" + str.toLowerCase() + ")");
                    }
                }
            }
            ExternalFileUtils.getInstance(context).clearRemotePrintersInfo();
            ArrayList<EPPrinterInfo> remotePrinterList2 = getRemotePrinterList();
            for (int i2 = 0; i2 < remotePrinterList2.size(); i2++) {
                serializePrinterInfo(ExternalFileUtils.getInstance(context).getRemotePrintersInfo(), remotePrinterList2.get(i2).printerEmailAddress.toLowerCase(), remotePrinterList2.get(i2));
            }
            rollbackRemotePrinterInfo();
        }
    }

    public void rollbackRemotePrinterInfo() {
        EPLog.i(TAG, "rollbackRemotePrinterInfo() called");
        remotePrinterList = null;
        deletedRemotePrinterList = null;
    }

    public void commitIPPrinterInfo() {
        if (ipPrinterList != null) {
            EPLog.i(TAG, "commitIPPrinterInfo() called");
            ExternalFileUtils.getInstance(context).clearIpPrintersInfo();
            ArrayList<EPPrinterInfo> ipPrinterList2 = getIpPrinterList();
            for (int i = 0; i < ipPrinterList2.size(); i++) {
                serializePrinterInfo(ExternalFileUtils.getInstance(context).getIpPrintersInfo(), ipPrinterList2.get(i).printerID, ipPrinterList2.get(i));
            }
            checkModifiedIP();
            rollbackIPPrinterInfo();
        }
    }

    private void checkModifiedIP() {
        boolean z;
        MyPrinter curPrinter = MyPrinter.getCurPrinter(context);
        boolean z2 = true;
        if (curPrinter.getLocation() == 3) {
            Iterator<EPPrinterInfo> it = getIpPrinterList().iterator();
            while (true) {
                if (!it.hasNext()) {
                    z = false;
                    break;
                }
                EPPrinterInfo next = it.next();
                if (curPrinter.getPrinterId().equals(next.printerID)) {
                    if (!curPrinter.getIp().equals(next.printerIP)) {
                        curPrinter.setIp(next.printerIP);
                        curPrinter.setCurPrinter(context);
                        EPLog.w(TAG, "IP printer's IP changed");
                    }
                    z = true;
                }
            }
            if (!z) {
                MyPrinter.clearCurPrinter(context);
                EPLog.w(TAG, "IP printer deleted");
            }
        }
        if (Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SCANNER_LOCATION) == 3) {
            String prefString = Utils.getPrefString(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SCANNER_ID);
            String prefString2 = Utils.getPrefString(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SCANNER_IP);
            Iterator<EPPrinterInfo> it2 = getIpPrinterList().iterator();
            while (true) {
                if (!it2.hasNext()) {
                    z2 = false;
                    break;
                }
                EPPrinterInfo next2 = it2.next();
                if (prefString.equals(next2.scannerID)) {
                    if (!prefString2.equals(next2.printerIP)) {
                        Utils.savePref(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SCANNER_IP, next2.printerIP);
                        EPLog.w(TAG, "IP scanner's IP changed");
                    }
                }
            }
            if (!z2) {
                ScanSettingHelper.saveErroredBlankSettings(context);
                EPLog.w(TAG, "IP scanner deleted");
            }
        }
    }

    public void rollbackIPPrinterInfo() {
        EPLog.i(TAG, "rollbackIPPrinterInfo() called");
        ipPrinterList = null;
    }

    private void serializePrinterInfo(String str, String str2, EPPrinterInfo ePPrinterInfo) {
        FileOutputStream fileOutputStream = null;
        try {
            String path = new File(str, Integer.toString(str2.hashCode())).getPath();
            if (ePPrinterInfo.printerLocation == 2) {
                StorageSecureStore sharedSecureStore = StorageSecureStore.getSharedSecureStore();
                sharedSecureStore.revoke(str2);
                if (sharedSecureStore.put(str2, ePPrinterInfo.printerAccessKey)) {
                    EPLog.i(TAG, "Success: put printerAccessKey. (" + str2 + " = " + ePPrinterInfo.printerAccessKey + ")");
                }
                ePPrinterInfo.printerAccessKey = "";
            }
            File file = new File(str);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream fileOutputStream2 = new FileOutputStream(path);
            try {
                ePPrinterInfo.writeJSON(fileOutputStream2);
            } catch (Exception unused) {
                fileOutputStream = fileOutputStream2;
            }
        } catch (Exception unused2) {
            EPLog.e(TAG, "savePrinterInfo(): writeObject error.");
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException unused3) {
                }
            }
        }
    }

    public boolean deleteRemotePrinterInfo(String str) {
        ArrayList<EPPrinterInfo> remotePrinterList2 = getRemotePrinterList();
        for (int i = 0; i < remotePrinterList2.size(); i++) {
            if (remotePrinterList2.get(i).printerEmailAddress.toLowerCase().equals(str.toLowerCase())) {
                if (deletedRemotePrinterList == null) {
                    deletedRemotePrinterList = new ArrayList<>();
                }
                deletedRemotePrinterList.add(remotePrinterList2.get(i));
                remotePrinterList2.remove(i);
                return true;
            }
        }
        EPLog.e(TAG, "deleteRemotePrinterInfo(): Couldn't delete - " + str.toLowerCase() + CommonDefine.DOT);
        return false;
    }

    public boolean deleteIpPrinterInfo(String str) {
        ArrayList<EPPrinterInfo> ipPrinterList2 = getIpPrinterList();
        for (int i = 0; i < ipPrinterList2.size(); i++) {
            if (ipPrinterList2.get(i).printerID.equals(str)) {
                ipPrinterList2.remove(i);
                return true;
            }
        }
        EPLog.e(TAG, "deleteIpPrinterInfo(): Couldn't delete - " + str + CommonDefine.DOT);
        return false;
    }

    public static void convertV3RemotePrinter(Context context2) {
        EPPrinterManager ePPrinterManager = new EPPrinterManager(context2);
        ArrayList<EPPrinterInfo> remotePrinterList2 = ePPrinterManager.getRemotePrinterList();
        for (int i = 0; i < remotePrinterList2.size(); i++) {
            EPPrinterInfo ePPrinterInfo = remotePrinterList2.get(i);
            if (ePPrinterInfo.userDefName == null || ePPrinterInfo.userDefName.length() <= 0) {
                EpsonConnectAccess.RemotePrinterInfo printerInfo = EpsonConnectAccess.getPrinterInfo(context2, ePPrinterInfo.printerEmailAddress, ePPrinterInfo.printerAccessKey);
                if (printerInfo != null) {
                    ePPrinterInfo.userDefName = ePPrinterInfo.printerName;
                    ePPrinterInfo.printerName = printerInfo.mDeviceId;
                    ePPrinterInfo.printerSerialNo = printerInfo.mSerialNumber;
                }
                remotePrinterList2.set(i, ePPrinterInfo);
            }
        }
        ePPrinterManager.commitRemotePrinterInfo();
    }


    public static boolean checkV3RemotePrinterData(Context r2) {

        throw new UnsupportedOperationException("Method not decompiled: epson.print.EPPrinterManager.checkV3RemotePrinterData(android.content.Context):boolean");
    }
}
