package com.epson.memcardacc;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.view.View;
import android.widget.TextView;

import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Deque;
import java.util.Iterator;
import java.util.Locale;

import epson.common.DialogProgress;
import epson.common.DialogProgressViewModel;
import epson.print.ActivityIACommon;
import epson.print.CommonDefine;
import epson.print.Util.EPLog;

public class ConfirmReadMemcard extends ActivityIACommon implements DialogProgress.DialogButtonClick {
    private static final int ACTIVITY_DIRECTORY_SELECT = 2;
    private static final int ACTIVITY_TYPE_PROGRESS = 1;
    private static final String DIALOG_COMMUNICATION_ERROR = "dialog_communication_error";
    private static final String DIALOG_COPY_RESULT_CANCEL = "dialog_copy_result";
    private static final String DIALOG_COPY_RESULT_COMPLETE = "dialog_copy_result_complete";
    private static final String DIALOG_COPY_RESULT_ERROR = "dialog_copy_result_error";
    private static final String DIALOG_DISK_SPACE_SHORTAGE = "dialog_disk_space_shortage";
    private static final String DIALOG_PRINTER_BUSY = "dialog_printer_busy";
    private static final String DIALOG_PROGRESS = "dialog_progress";
    private static final String DIALOG_WRITE_LOCAL_DIR_CANNOT_GET = "dialog_write_local_dir_cannot_get";
    static final int ERROR_COMMUNICATION_ERROR = 11;
    static final int ERROR_MEMCARD_BUSY = 10;
    public static final String KEY_IMAGE_LIST = "imagelist";
    private static final String LOG_TAG = "ConfirmReadMemcard";
    public static final String MEMCARD_STORAGE_TYPE = "memcard_storage_type";
    public static final int RESULT_BACK_TO_MEMCARD_TOP = 1;
    boolean doTotalFileSizeCalc = false;
    boolean isKeepSimleApConnect = false;
    boolean isTryConnectSimpleAp = false;
    CheckAndNextActivityTask mCheckAndNextActivityTask;
    private String mCopyTargetFolderName;
    private TextView mCopyTargetPathText;
    private TextView mFileNumText;
    private TextView mFileSizeText;
    private ArrayList<String> mImageList;
    private int mMemcardStorageType;
    private DialogProgressViewModel mModelDialog;
    private long mTotalFileSize;
    private TotalFileSizeCalcTask mTotalFileSizeCalcTask;
    private String mWriteDirectory = MemcardConfig.DEFAULT_READER_LOCAL_DIRECTORY;

    public void onCancelDialog(String str) {
    }

    public void onNeutralClick(String str) {
    }

    public void onPositiveClick(String str) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_confirm_read_memcard);
        mModelDialog = (DialogProgressViewModel) ViewModelProviders.m2of((FragmentActivity) this).get(DialogProgressViewModel.class);
        mModelDialog.getDialogJob().observe(this, new Observer() {
            public final void onChanged(Object obj) {
                ConfirmReadMemcard.lambda$onCreate$0(ConfirmReadMemcard.this, (Deque) obj);
            }
        });
        setActionBar(R.string.memcard_title_copy_confirm, true);
        mFileNumText = (TextView) findViewById(R.id.textFileNumber);
        mFileSizeText = (TextView) findViewById(R.id.textFileSize);
        mFileSizeText.setText(CommonDefine.QUESTION_MARK);
        mCopyTargetPathText = (TextView) findViewById(R.id.textWriteFolder);
        Intent intent = getIntent();
        mImageList = intent.getStringArrayListExtra(KEY_IMAGE_LIST);
        if (mImageList == null) {
            mImageList = new ArrayList<>();
        }
        mMemcardStorageType = intent.getIntExtra("memcard_storage_type", 1);
        int size = mImageList.size();
        mFileNumText.setText(String.format(getResources().getString(R.string.memcard_file_num_format), new Object[]{Integer.valueOf(size)}));
        setTargetDirectory(MemcardConfig.DEFAULT_READER_LOCAL_DIRECTORY);
        doTotalFileSizeCalc = true;
    }

    public static /* synthetic */ void lambda$onCreate$0(ConfirmReadMemcard confirmReadMemcard, Deque deque) {
        String[] checkQueue = confirmReadMemcard.mModelDialog.checkQueue();
        if (checkQueue != null) {
            String str = checkQueue[0];
            String str2 = checkQueue[1];
            if (str2.equals("do_show")) {
                confirmReadMemcard.showDialog(str);
            }
            if (str2.equals("do_dismiss")) {
                confirmReadMemcard.dismissDialog(str);
            }
        }
    }

    protected void onResume() {
        super.onResume();
        if (!WiFiDirectManager.isNeedConnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER)) {
            isTryConnectSimpleAp = false;
        } else if (!this.isTryConnectSimpleAp) {
            isTryConnectSimpleAp = true;
            if (WiFiDirectManager.reconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, -1)) {
                doTotalFileSizeCalc = true;
                return;
            }
        }
        isKeepSimleApConnect = false;
        if (doTotalFileSizeCalc) {
            mTotalFileSizeCalcTask = new TotalFileSizeCalcTask();
            mTotalFileSizeCalcTask.execute(new Void[0]);
            doTotalFileSizeCalc = false;
        }
    }

    protected void onPause() {
        EPLog.d(LOG_TAG, "onPause()");
        super.onPause();
    }

    protected void onStop() {
        EPLog.d(LOG_TAG, "onStop()");
        super.onStop();
        if (!this.isKeepSimleApConnect) {
            WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
        }
    }

    protected void onDestroy() {
        mTotalFileSizeCalcTask.cancel(true);
        super.onDestroy();
    }

    public void onBackPressed() {
        super.onBackPressed();
        isKeepSimleApConnect = true;
    }

    private void localShowDialog(String str) {
        mModelDialog.doShow(str);
    }

    public void setTargetDirectory(String str) {
        mWriteDirectory = str;
        mCopyTargetFolderName = getTargetFolderName();
        String str2 = mCopyTargetFolderName;
        if (str2 == null) {
            localShowDialog(DIALOG_WRITE_LOCAL_DIR_CANNOT_GET);
            str2 = "";
        }
        EPLog.i(LOG_TAG, "outdir: " + mCopyTargetFolderName);
        mCopyTargetPathText.setText(String.format(getResources().getString(R.string.memcard_folder_save_to_format), new Object[]{str2}));
    }

    private String getTargetFolderName() {
        Calendar instance = Calendar.getInstance();
        for (int i = 0; i < 100; i++) {
            String format = String.format(Locale.US, "%s/%s/%04d%02d%02d%02d%02d%02d", new Object[]{MemcardConfig.BASE_DIRECTORY, mWriteDirectory, Integer.valueOf(instance.get(1)), Integer.valueOf(instance.get(2) + 1), Integer.valueOf(instance.get(5)), Integer.valueOf(instance.get(11)), Integer.valueOf(instance.get(12)), Integer.valueOf(i)});
            File file = new File(format);
            if (!file.exists()) {
                boolean mkdirs = file.mkdirs();
                file.delete();
                if (mkdirs) {
                    return format;
                }
            }
        }
        return null;
    }

    static long getTotalFileSize(ArrayList<String> arrayList) {
        long j = 0;
        if (arrayList == null) {
            return 0;
        }
        Iterator<String> it = arrayList.iterator();
        while (it.hasNext()) {
            j += new File(it.next()).length();
        }
        return j;
    }

    private boolean checkFileSize() {
        StatFs statFs = new StatFs(new File(MemcardConfig.BASE_DIRECTORY, mWriteDirectory).toString());
        return (mTotalFileSize / statFs.getBlockSizeLong()) + ((long) mImageList.size()) <= statFs.getAvailableBlocksLong();
    }

    public void updateFileSize(long j) {
        mTotalFileSize = j;
        mFileSizeText.setText(String.format(getResources().getString(R.string.memcard_total_file_size_format), new Object[]{Double.valueOf((mTotalFileSize) / 1000000.0d)}));
    }

    private void startCopyActivity() {
        Intent intent = new Intent(this, MemcardReadProgress.class);
        intent.putStringArrayListExtra("imageList", mImageList);
        intent.putExtra("target_folder_name", mCopyTargetFolderName);
        intent.putExtra("memcard_storage_type", mMemcardStorageType);
        startActivityForResult(intent, 1);
        isKeepSimleApConnect = true;
    }

    public void copy_dest_button_clicked(View view) {
        Intent intent = new Intent(this, DirectorySelecterActivity.class);
        intent.putExtra(DirectorySelecterActivity.PARAM_BASE_DIRECTORY, MemcardConfig.BASE_DIRECTORY);
        intent.putExtra(DirectorySelecterActivity.PARAM_DEFAULT_DIR, MemcardConfig.DEFAULT_READER_LOCAL_DIRECTORY);
        startActivityForResult(intent, 2);
    }

    public void copy_button_clicked(View view) {
        mCheckAndNextActivityTask = new CheckAndNextActivityTask();
        mCheckAndNextActivityTask.execute(new Void[0]);
    }

    /* JADX WARNING: Can't fix incorrect switch cases order */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void showDialog(java.lang.String r12) {
        /*
            r11 = this;
            int r0 = r12.hashCode()
            r1 = 0
            switch(r0) {
                case -2116590872: goto L_0x004f;
                case -2003975289: goto L_0x0045;
                case -508401744: goto L_0x003b;
                case -63760199: goto L_0x0031;
                case -21731516: goto L_0x0027;
                case 1079349783: goto L_0x001d;
                case 1194959080: goto L_0x0013;
                case 1429177493: goto L_0x0009;
                default: goto L_0x0008;
            }
        L_0x0008:
            goto L_0x0059
        L_0x0009:
            java.lang.String r0 = "dialog_printer_busy"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 6
            goto L_0x005a
        L_0x0013:
            java.lang.String r0 = "dialog_copy_result_complete"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 3
            goto L_0x005a
        L_0x001d:
            java.lang.String r0 = "dialog_write_local_dir_cannot_get"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 2
            goto L_0x005a
        L_0x0027:
            java.lang.String r0 = "dialog_progress"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 0
            goto L_0x005a
        L_0x0031:
            java.lang.String r0 = "dialog_copy_result_error"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 5
            goto L_0x005a
        L_0x003b:
            java.lang.String r0 = "dialog_copy_result"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 4
            goto L_0x005a
        L_0x0045:
            java.lang.String r0 = "dialog_disk_space_shortage"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 1
            goto L_0x005a
        L_0x004f:
            java.lang.String r0 = "dialog_communication_error"
            boolean r0 = r12.equals(r0)
            if (r0 == 0) goto L_0x0059
            r0 = 7
            goto L_0x005a
        L_0x0059:
            r0 = -1
        L_0x005a:
            r2 = 2131624893(0x7f0e03bd, float:1.8876979E38)
            r3 = 2131625202(0x7f0e04f2, float:1.8877605E38)
            switch(r0) {
                case 0: goto L_0x0107;
                case 1: goto L_0x00eb;
                case 2: goto L_0x00eb;
                case 3: goto L_0x00d5;
                case 4: goto L_0x00bd;
                case 5: goto L_0x009f;
                case 6: goto L_0x0082;
                case 7: goto L_0x0066;
                default: goto L_0x0063;
            }
        L_0x0063:
            r0 = 0
            goto L_0x0115
        L_0x0066:
            r5 = 2
            r0 = 2131624886(0x7f0e03b6, float:1.8876964E38)
            java.lang.String r6 = r11.getString(r0)
            r0 = 2131624887(0x7f0e03b7, float:1.8876966E38)
            java.lang.String r7 = r11.getString(r0)
            java.lang.String r8 = r11.getString(r3)
            r9 = 0
            r10 = 0
            r4 = r12
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r4, r5, r6, r7, r8, r9, r10)
            goto L_0x0115
        L_0x0082:
            r2 = 2
            r0 = 2131624471(0x7f0e0217, float:1.8876123E38)
            java.lang.String r0 = r11.getString(r0)
            r1 = 2131624472(0x7f0e0218, float:1.8876125E38)
            java.lang.String r4 = r11.getString(r1)
            java.lang.String r5 = r11.getString(r3)
            r6 = 0
            r7 = 0
            r1 = r12
            r3 = r0
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x0115
        L_0x009f:
            r0 = 2
            java.lang.String r4 = r11.getString(r2)
            r1 = 2131624894(0x7f0e03be, float:1.887698E38)
            java.lang.String r5 = r11.getString(r1)
            java.lang.String r6 = r11.getString(r3)
            r7 = 0
            r8 = 0
            r1 = r12
            r2 = r0
            r3 = r4
            r4 = r5
            r5 = r6
            r6 = r7
            r7 = r8
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x0115
        L_0x00bd:
            r0 = 2
            java.lang.String r4 = r11.getString(r2)
            r5 = 0
            r6 = 0
            r7 = 0
            java.lang.String r8 = r11.getString(r3)
            r1 = r12
            r2 = r0
            r3 = r4
            r4 = r5
            r5 = r6
            r6 = r7
            r7 = r8
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x0115
        L_0x00d5:
            r2 = 2
            r0 = 2131624890(0x7f0e03ba, float:1.8876972E38)
            java.lang.String r0 = r11.getString(r0)
            r4 = 0
            r5 = 0
            r6 = 0
            java.lang.String r7 = r11.getString(r3)
            r1 = r12
            r3 = r0
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x0115
        L_0x00eb:
            r2 = 2
            r0 = 2131624883(0x7f0e03b3, float:1.8876958E38)
            java.lang.String r0 = r11.getString(r0)
            r1 = 2131624884(0x7f0e03b4, float:1.887696E38)
            java.lang.String r4 = r11.getString(r1)
            java.lang.String r5 = r11.getString(r3)
            r6 = 0
            r7 = 0
            r1 = r12
            r3 = r0
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r1, r2, r3, r4, r5, r6, r7)
            goto L_0x0115
        L_0x0107:
            r0 = 2131624885(0x7f0e03b5, float:1.8876962E38)
            java.lang.String r0 = r11.getString(r0)
            epson.common.DialogProgress r0 = epson.common.DialogProgress.newInstance(r12, r1, r0)
            r0.setCancelable(r1)
        L_0x0115:
            if (r0 == 0) goto L_0x011e
            android.support.v4.app.FragmentManager r1 = r11.getSupportFragmentManager()
            r0.show(r1, r12)
        L_0x011e:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.ConfirmReadMemcard.showDialog(java.lang.String):void");
    }

    private void dismissDialog(String str) {
        DialogFragment dialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(str));
        if (dialogFragment != null) {
            dialogFragment.dismiss();
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        switch (i) {
            case 1:
                isKeepSimleApConnect = false;
                if (i2 == -2) {
                    setResult(1);
                    finish();
                    return;
                } else if (i2 != 0) {
                    localShowDialog(DIALOG_COPY_RESULT_ERROR);
                    return;
                } else {
                    return;
                }
            case 2:
                if (i2 == -1) {
                    setTargetDirectory(intent.getStringExtra(DirectorySelecterActivity.RESULT_DIRECTORY));
                    return;
                }
                return;
            default:
                return;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:12:0x0029  */
    /* JADX WARNING: Removed duplicated region for block: B:13:0x002f  */
    /* JADX WARNING: Removed duplicated region for block: B:16:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void onNegativeClick(java.lang.String r4) {
        /*
            r3 = this;
            int r0 = r4.hashCode()
            r1 = -508401744(0xffffffffe1b267b0, float:-4.11374E20)
            r2 = 1
            if (r0 == r1) goto L_0x001a
            r1 = 1194959080(0x4739a0e8, float:47520.906)
            if (r0 == r1) goto L_0x0010
            goto L_0x0024
        L_0x0010:
            java.lang.String r0 = "dialog_copy_result_complete"
            boolean r4 = r4.equals(r0)
            if (r4 == 0) goto L_0x0024
            r4 = 0
            goto L_0x0025
        L_0x001a:
            java.lang.String r0 = "dialog_copy_result"
            boolean r4 = r4.equals(r0)
            if (r4 == 0) goto L_0x0024
            r4 = 1
            goto L_0x0025
        L_0x0024:
            r4 = -1
        L_0x0025:
            switch(r4) {
                case 0: goto L_0x002f;
                case 1: goto L_0x0029;
                default: goto L_0x0028;
            }
        L_0x0028:
            goto L_0x0035
        L_0x0029:
            java.lang.String r4 = r3.mWriteDirectory
            r3.setTargetDirectory(r4)
            goto L_0x0035
        L_0x002f:
            r3.setResult(r2)
            r3.finish()
        L_0x0035:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.ConfirmReadMemcard.onNegativeClick(java.lang.String):void");
    }

    class TotalFileSizeCalcTask extends AsyncTask<Void, Void, Long> {
        CifsAccess mCifsAccess = CifsAccess.getInstance();

        TotalFileSizeCalcTask() {
        }

        protected synchronized Long doInBackground(Void... voidArr) {
            if (mCifsAccess.initDefault(ConfirmReadMemcard.this, 1) == 0) {
                return -1L;
            }
            if (mCifsAccess.connectDefaultStorageWidthDefaultAthInfo(ConfirmReadMemcard.mMemcardStorageType) == 0) {
                mCifsAccess.free();
                return -1L;
            }
            try {
                Iterator it = ConfirmReadMemcard.mImageList.iterator();
                long j = 0;
                while (it.hasNext()) {
                    if (isCancelled()) {
                        mCifsAccess.disconnectStorage();
                        mCifsAccess.free();
                        return 0L;
                    }
                    long fileSize = mCifsAccess.getFileSize((String) it.next());
                    if (fileSize <= 0) {
                        return -1L;
                    }
                    j += fileSize;
                }
                mCifsAccess.disconnectStorage();
                mCifsAccess.free();
                return Long.valueOf(j);
            } finally {
                mCifsAccess.disconnectStorage();
                mCifsAccess.free();
            }
        }

        protected void onPostExecute(Long l) {
            if (l != null) {
                if (l.longValue() == 0) {
                    ConfirmReadMemcard.this.localShowDialog(ConfirmReadMemcard.DIALOG_COPY_RESULT_CANCEL);
                } else if (l.longValue() < 0) {
                    ConfirmReadMemcard.this.localShowDialog(ConfirmReadMemcard.DIALOG_COMMUNICATION_ERROR);
                } else {
                    ConfirmReadMemcard.this.updateFileSize(l.longValue());
                }
            }
            ConfirmReadMemcard.mModelDialog.doDismiss(ConfirmReadMemcard.DIALOG_PROGRESS);
        }

        protected void onPreExecute() {
            ConfirmReadMemcard.mModelDialog.doShow(ConfirmReadMemcard.DIALOG_PROGRESS);
        }
    }

    class CheckAndNextActivityTask extends AsyncTask<Void, Void, Integer> {
        CheckAndNextActivityTask() {
        }

        protected Integer doInBackground(Void... voidArr) {
            if (ConfirmReadMemcard.mCopyTargetFolderName == null) {
                return 100;
            }
            if (isCancelled()) {
                return -1;
            }
            if (!ConfirmReadMemcard.this.checkFileSize()) {
                return 1;
            }
            if (isCancelled()) {
                return -1;
            }
            switch (EscprLibPrinter.checkIdleOrError(ConfirmReadMemcard.this, 60)) {
                case 1:
                    return 11;
                case 2:
                    return 10;
                default:
                    new File(ConfirmReadMemcard.mCopyTargetFolderName).mkdir();
                    return 0;
            }
        }

        protected void onCancelled() {
            ConfirmReadMemcard.mModelDialog.doDismiss(ConfirmReadMemcard.DIALOG_PROGRESS);
        }

        protected void onPostExecute(Integer num) {
            ConfirmReadMemcard.mModelDialog.doDismiss(ConfirmReadMemcard.DIALOG_PROGRESS);
            switch (num.intValue()) {
                case 0:
                    ConfirmReadMemcard.this.startCopyActivity();
                    return;
                case 1:
                    ConfirmReadMemcard.this.localShowDialog(ConfirmReadMemcard.DIALOG_DISK_SPACE_SHORTAGE);
                    return;
                case 10:
                    ConfirmReadMemcard.this.localShowDialog(ConfirmReadMemcard.DIALOG_PRINTER_BUSY);
                    return;
                case 11:
                    ConfirmReadMemcard.this.localShowDialog(ConfirmReadMemcard.DIALOG_COMMUNICATION_ERROR);
                    return;
                default:
                    return;
            }
        }

        protected void onPreExecute() {
            ConfirmReadMemcard.mModelDialog.doShow(ConfirmReadMemcard.DIALOG_PROGRESS);
            if (ConfirmReadMemcard.mCopyTargetFolderName == null) {
                ConfirmReadMemcard.this.setTargetDirectory(MemcardConfig.DEFAULT_READER_LOCAL_DIRECTORY);
            }
        }
    }
}
