package com.epson.cameracopy.printlayout;

import android.content.Context;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import org.json.JSONException;
import org.json.JSONObject;

public class RegistedDocumentSizeList {
    private static final int CLASS_SAVE_VERSION = 1;
    private static int DEFAULT_PAPER_SIZE_COUNT = 17;
    private static final int[] DOCSIZE_NAME_ID = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
    private static final double[] PAPER_SIZE_HEIGHT = {297.0d, 420.0d, 297.0d, 210.0d, 148.0d, 364.0d, 257.0d, 182.0d, 11.0d, 14.0d, 148.0d, 152.0d, 127.0d, 178.0d, 86.0d, 91.0d, 182.0d};
    private static final int[] PAPER_SIZE_ID = {-1, 62, 0, 3, 4, 63, 5, 46, 1, 2, 16, 10, 15, 28, 35, 36, 46};
    private static final int[] PAPER_SIZE_NAME = {R.string.papersize_auto, R.string.papersize_a3, R.string.papersize_a4, R.string.papersize_a5, R.string.papersize_a6, R.string.papersize_b4, R.string.papersize_b5, R.string.papersize_b6, R.string.papersize_letter, R.string.papersize_legal, R.string.papersize_postcard, R.string.papersize_10x15, R.string.papersize_l, R.string.papersize_2l, R.string.papersize_card, R.string.papersize_buzcard, R.string.papersize_passport};
    private static final int[] PAPER_SIZE_SCALE = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1};
    private static final double[] PAPER_SIZE_WIDTH = {210.0d, 297.0d, 210.0d, 148.0d, 105.0d, 257.0d, 182.0d, 128.0d, 8.5d, 8.5d, 100.0d, 102.0d, 89.0d, 127.0d, 54.0d, 55.0d, 128.0d};
    private static final String REGISTED_DOCUMENTSIZE_LIST_FILENAME = "registed_documentsize.bin";
    static RegistedDocumentSizeList sRegistedDocumentSizeList;
    Context mContext;
    DocumentSizeInfo mCurrentDocumentSize;
    private LinkedList<DocumentSizeInfo> mDocumentSizeList = new LinkedList<>();

    RegistedDocumentSizeList(Context context) {
        if (context != null) {
            mContext = context;
            initData();
            return;
        }
        throw new IllegalArgumentException("context is null");
    }

    public static DocumentSizeInfo getCurrentDocumentSize(Context context) {
        RegistedDocumentSizeList registedDocumentSizeList = sRegistedDocumentSizeList;
        if (registedDocumentSizeList != null) {
            return registedDocumentSizeList.getCurrentDocumentSize();
        }
        return getInstance(context).getCurrentDocumentSize();
    }

    public static RegistedDocumentSizeList getInstance(Context context) {
        if (context != null) {
            if (sRegistedDocumentSizeList == null) {
                sRegistedDocumentSizeList = new RegistedDocumentSizeList(context);
                sRegistedDocumentSizeList.restoreData(context);
            }
            return sRegistedDocumentSizeList;
        }
        throw new RuntimeException("context == null");
    }

    public Collection<DocumentSizeInfo> getRegistedList() {
        return mDocumentSizeList;
    }

    public DocumentSizeInfo getCurrentDocumentSize() {
        return mCurrentDocumentSize;
    }

    public void add(DocumentSizeInfo documentSizeInfo) {
        if (documentSizeInfo != null) {
            mDocumentSizeList.add(documentSizeInfo);
            mCurrentDocumentSize = documentSizeInfo;
            saveData(mContext);
        }
    }

    public void update(DocumentSizeInfo documentSizeInfo, int i) {
        DocumentSizeInfo documentSizeInfo2 = mDocumentSizeList.get(i);
        if (documentSizeInfo2 != null) {
            documentSizeInfo2.update(documentSizeInfo);
            mCurrentDocumentSize = mDocumentSizeList.get(i);
            saveData(mContext);
        }
    }

    public void delete(DocumentSizeInfo documentSizeInfo, int i) {
        mDocumentSizeList.remove(documentSizeInfo);
        DocumentSizeInfo documentSizeInfo2 = mCurrentDocumentSize;
        if (documentSizeInfo2 != null && documentSizeInfo2.equals(documentSizeInfo)) {
            if (i > 0) {
                mCurrentDocumentSize = mDocumentSizeList.get(i - 1);
            } else if (mDocumentSizeList.size() > 0) {
                mCurrentDocumentSize = mDocumentSizeList.get(0);
            } else {
                mCurrentDocumentSize = null;
            }
        }
    }

    public void deleteAll() {
        LinkedList<DocumentSizeInfo> linkedList = mDocumentSizeList;
        linkedList.removeAll(linkedList);
        mCurrentDocumentSize = null;
    }

    public void reset() {
        initData();
        for (int i = 0; i < DEFAULT_PAPER_SIZE_COUNT; i++) {
            DocumentSizeInfo documentSizeInfo = new DocumentSizeInfo();
            documentSizeInfo.setDocSizeName(mContext.getString(PAPER_SIZE_NAME[i]));
            documentSizeInfo.setScaleId(PAPER_SIZE_SCALE[i]);
            documentSizeInfo.setWidth(PAPER_SIZE_WIDTH[i]);
            documentSizeInfo.setHeight(PAPER_SIZE_HEIGHT[i]);
            documentSizeInfo.setPaperId(PAPER_SIZE_ID[i]);
            documentSizeInfo.setDocSizeNameId(DOCSIZE_NAME_ID[i]);
            add(documentSizeInfo);
        }
        select(0);
    }

    public int size() {
        return mDocumentSizeList.size();
    }

    public DocumentSizeInfo getItem(int i) {
        return mDocumentSizeList.get(i);
    }

    public DocumentSizeInfo getItem(String str) {
        DocumentSizeInfo documentSizeInfo;
        Iterator it = mDocumentSizeList.iterator();
        do {
            documentSizeInfo = null;
            if (!it.hasNext()) {
                break;
            }
            documentSizeInfo = (DocumentSizeInfo) it.next();
        } while (!documentSizeInfo.getDocSizeName().equals(str));
        return documentSizeInfo;
    }

    public boolean isExistDocumentSizeName(String str, int i) {
        DocumentSizeInfo item = getItem(str);
        return (item == null || mDocumentSizeList.indexOf(item) == i) ? false : true;
    }

    public void storeData() {
        saveData(mContext);
    }

    public boolean isSelected(DocumentSizeInfo documentSizeInfo) {
        DocumentSizeInfo documentSizeInfo2 = mCurrentDocumentSize;
        if (documentSizeInfo2 == null) {
            return false;
        }
        return documentSizeInfo2.equals(documentSizeInfo);
    }

    public void select(int i) {
        if (i < 0) {
            mCurrentDocumentSize = null;
        } else if (i < mDocumentSizeList.size()) {
            mCurrentDocumentSize = mDocumentSizeList.get(i);
            saveData(mContext);
        } else {
            throw new IndexOutOfBoundsException(String.format(Locale.US, "position <%d> learger than list size <%s>", new Object[]{Integer.valueOf(i), Integer.valueOf(mDocumentSizeList.size())}));
        }
    }

    public void select(DocumentSizeInfo documentSizeInfo) {
        if (documentSizeInfo != null) {
            int indexOf = mDocumentSizeList.indexOf(documentSizeInfo);
            if (indexOf >= 0) {
                mCurrentDocumentSize = mDocumentSizeList.get(indexOf);
            } else {
                mDocumentSizeList.add(documentSizeInfo);
                mCurrentDocumentSize = documentSizeInfo;
            }
            saveData(mContext);
            return;
        }
        throw new IllegalArgumentException("DocumentSizeInfo is null");
    }

    public int getCurrentDocumentSizePosition() {
        Iterator it = mDocumentSizeList.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (((DocumentSizeInfo) it.next()) == mCurrentDocumentSize) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public Iterator<DocumentSizeInfo> getIterator() {
        return mDocumentSizeList.iterator();
    }

    public int indexOf(DocumentSizeInfo documentSizeInfo) {
        LinkedList<DocumentSizeInfo> linkedList = mDocumentSizeList;
        if (linkedList == null) {
            return -1;
        }
        return linkedList.indexOf(documentSizeInfo);
    }

    private void initData() {
        mDocumentSizeList.clear();
        mCurrentDocumentSize = null;
    }

    private void saveData(Context context) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = context.openFileOutput(REGISTED_DOCUMENTSIZE_LIST_FILENAME, 0);
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("version", 1);
            jSONObject.put("DocumentSizeInfoList", DocumentSizeInfo.makeDocumentSizeInfoList(mDocumentSizeList));
            jSONObject.put("currentDocumentSize", mCurrentDocumentSize);
            fileOutputStream.write(jSONObject.toString().getBytes());
            if (fileOutputStream == null) {
                return;
            }
        } catch (IOException unused) {
            if (fileOutputStream == null) {
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            if (fileOutputStream == null) {
                return;
            }
        } catch (Throwable th) {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException unused2) {
                }
            }
            throw th;
        }
        try {
            fileOutputStream.close();
        } catch (IOException unused3) {
        }
    }

    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r2v6, resolved type: java.lang.Object} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r3v6, resolved type: java.lang.Integer} */
    /* JADX WARNING: Code restructure failed: missing block: B:59:?, code lost:
        r0.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:68:?, code lost:
        r0.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:85:?, code lost:
        return;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:86:?, code lost:
        return;
     */
    /* JADX WARNING: Exception block dominator not found, dom blocks: [B:55:0x00c9, B:64:0x00d7] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:55:0x00c9 */
    /* JADX WARNING: Missing exception handler attribute for start block: B:64:0x00d7 */
    /* JADX WARNING: Multi-variable type inference failed */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x00b0 A[SYNTHETIC, Splitter:B:39:0x00b0] */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x00ce A[SYNTHETIC, Splitter:B:58:0x00ce] */
    /* JADX WARNING: Removed duplicated region for block: B:67:0x00dc A[SYNTHETIC, Splitter:B:67:0x00dc] */
    /* JADX WARNING: Removed duplicated region for block: B:75:0x00eb A[SYNTHETIC, Splitter:B:75:0x00eb] */
    /* JADX WARNING: Removed duplicated region for block: B:79:0x00f2 A[SYNTHETIC, Splitter:B:79:0x00f2] */
    /* JADX WARNING: Removed duplicated region for block: B:84:? A[RETURN, SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:85:? A[RETURN, SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:86:? A[RETURN, SYNTHETIC] */
    /* JADX WARNING: Unknown top exception splitter block from list: {B:64:0x00d7=Splitter:B:64:0x00d7, B:55:0x00c9=Splitter:B:55:0x00c9} */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void restoreData(android.content.Context r8) {
        /*
            r7 = this;
            r0 = 0
            java.lang.String r1 = "registed_documentsize.bin"
            java.io.FileInputStream r1 = r8.openFileInput(r1)     // Catch:{ ClassNotFoundException -> 0x00d6, IOException -> 0x00c8, all -> 0x00c5 }
            r2 = 1
            int r3 = r1.available()     // Catch:{ JSONException -> 0x004b }
            byte[] r3 = new byte[r3]     // Catch:{ JSONException -> 0x004b }
            r1.read(r3)     // Catch:{ JSONException -> 0x004b }
            org.json.JSONObject r4 = new org.json.JSONObject     // Catch:{ JSONException -> 0x004b }
            java.lang.String r5 = new java.lang.String     // Catch:{ JSONException -> 0x004b }
            r5.<init>(r3)     // Catch:{ JSONException -> 0x004b }
            r4.<init>(r5)     // Catch:{ JSONException -> 0x004b }
            java.lang.String r3 = "version"
            int r3 = r4.getInt(r3)     // Catch:{ JSONException -> 0x004b }
            java.lang.Integer r3 = java.lang.Integer.valueOf(r3)     // Catch:{ JSONException -> 0x004b }
            int r3 = r3.intValue()     // Catch:{ JSONException -> 0x004b }
            if (r3 != r2) goto L_0x003c
            java.util.LinkedList r3 = com.epson.cameracopy.printlayout.DocumentSizeInfo.getDocumentSizeInfoList(r4)     // Catch:{ JSONException -> 0x004b }
            r7.mDocumentSizeList = r3     // Catch:{ JSONException -> 0x004b }
            java.lang.String r3 = "currentDocumentSize"
            int r3 = r4.optInt(r3)     // Catch:{ JSONException -> 0x004b }
            java.lang.Integer r3 = java.lang.Integer.valueOf(r3)     // Catch:{ JSONException -> 0x004b }
            goto L_0x0051
        L_0x003c:
            java.io.IOException r3 = new java.io.IOException     // Catch:{ JSONException -> 0x004b }
            r3.<init>()     // Catch:{ JSONException -> 0x004b }
            throw r3     // Catch:{ JSONException -> 0x004b }
        L_0x0042:
            r8 = move-exception
            goto L_0x00e9
        L_0x0045:
            r8 = r1
            goto L_0x00c9
        L_0x0048:
            r8 = r1
            goto L_0x00d7
        L_0x004b:
            r3 = move-exception
            r3.printStackTrace()     // Catch:{ ClassNotFoundException -> 0x0048, IOException -> 0x0045, all -> 0x0042 }
            r3 = r0
            r4 = r3
        L_0x0051:
            if (r4 != 0) goto L_0x0083
            r1.close()     // Catch:{ ClassNotFoundException -> 0x0048, IOException -> 0x0045, all -> 0x0042 }
            java.lang.String r3 = "registed_documentsize.bin"
            java.io.FileInputStream r8 = r8.openFileInput(r3)     // Catch:{ ClassNotFoundException -> 0x0048, IOException -> 0x0045, all -> 0x0042 }
            java.io.ObjectInputStream r1 = new java.io.ObjectInputStream     // Catch:{ ClassNotFoundException -> 0x00d7, IOException -> 0x00c9 }
            r1.<init>(r8)     // Catch:{ ClassNotFoundException -> 0x00d7, IOException -> 0x00c9 }
            java.lang.Object r3 = r1.readObject()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            java.lang.Integer r3 = (java.lang.Integer) r3     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            int r3 = r3.intValue()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            if (r3 != r2) goto L_0x007d
            java.lang.Object r2 = r1.readObject()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            java.util.LinkedList r2 = (java.util.LinkedList) r2     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            r7.mDocumentSizeList = r2     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            java.lang.Object r2 = r1.readObject()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            r3 = r2
            java.lang.Integer r3 = (java.lang.Integer) r3     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            goto L_0x0085
        L_0x007d:
            java.io.IOException r0 = new java.io.IOException     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            r0.<init>()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            throw r0     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
        L_0x0083:
            r8 = r1
            r1 = r0
        L_0x0085:
            if (r3 == 0) goto L_0x00ac
            int r2 = r3.intValue()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            java.util.LinkedList<com.epson.cameracopy.printlayout.DocumentSizeInfo> r4 = r7.mDocumentSizeList     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            int r4 = r4.size()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            if (r2 < r4) goto L_0x0094
            goto L_0x00ac
        L_0x0094:
            int r2 = r3.intValue()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            if (r2 >= 0) goto L_0x009d
            r7.mCurrentDocumentSize = r0     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            goto L_0x00ae
        L_0x009d:
            java.util.LinkedList<com.epson.cameracopy.printlayout.DocumentSizeInfo> r0 = r7.mDocumentSizeList     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            int r2 = r3.intValue()     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            java.lang.Object r0 = r0.get(r2)     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            com.epson.cameracopy.printlayout.DocumentSizeInfo r0 = (com.epson.cameracopy.printlayout.DocumentSizeInfo) r0     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            r7.mCurrentDocumentSize = r0     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
            goto L_0x00ae
        L_0x00ac:
            r7.mCurrentDocumentSize = r0     // Catch:{ ClassNotFoundException -> 0x00c3, IOException -> 0x00c1, all -> 0x00bb }
        L_0x00ae:
            if (r1 == 0) goto L_0x00b5
            r1.close()     // Catch:{ IOException -> 0x00b4 }
            goto L_0x00b5
        L_0x00b4:
        L_0x00b5:
            if (r8 == 0) goto L_0x00e4
        L_0x00b7:
            r8.close()     // Catch:{ IOException -> 0x00e4 }
            goto L_0x00e4
        L_0x00bb:
            r0 = move-exception
            r6 = r1
            r1 = r8
            r8 = r0
            r0 = r6
            goto L_0x00e9
        L_0x00c1:
            r0 = r1
            goto L_0x00c9
        L_0x00c3:
            r0 = r1
            goto L_0x00d7
        L_0x00c5:
            r8 = move-exception
            r1 = r0
            goto L_0x00e9
        L_0x00c8:
            r8 = r0
        L_0x00c9:
            r7.reset()     // Catch:{ all -> 0x00e5 }
            if (r0 == 0) goto L_0x00d3
            r0.close()     // Catch:{ IOException -> 0x00d2 }
            goto L_0x00d3
        L_0x00d2:
        L_0x00d3:
            if (r8 == 0) goto L_0x00e4
            goto L_0x00b7
        L_0x00d6:
            r8 = r0
        L_0x00d7:
            r7.reset()     // Catch:{ all -> 0x00e5 }
            if (r0 == 0) goto L_0x00e1
            r0.close()     // Catch:{ IOException -> 0x00e0 }
            goto L_0x00e1
        L_0x00e0:
        L_0x00e1:
            if (r8 == 0) goto L_0x00e4
            goto L_0x00b7
        L_0x00e4:
            return
        L_0x00e5:
            r1 = move-exception
            r6 = r1
            r1 = r8
            r8 = r6
        L_0x00e9:
            if (r0 == 0) goto L_0x00f0
            r0.close()     // Catch:{ IOException -> 0x00ef }
            goto L_0x00f0
        L_0x00ef:
        L_0x00f0:
            if (r1 == 0) goto L_0x00f5
            r1.close()     // Catch:{ IOException -> 0x00f5 }
        L_0x00f5:
            throw r8
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.cameracopy.printlayout.RegistedDocumentSizeList.restoreData(android.content.Context):void");
    }
}
