package com.epson.memcardacc;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;


import android.support.p000v4.app.FragmentActivity;
import com.epson.memcardacc.AltAlertDialogFragment;
import com.epson.memcardacc.FileConvertTask;
import com.epson.memcardacc.FileConvertViewModel;

import epson.print.imgsel.ImageFinder;
import epson.print.imgsel.ImageSelectActivity;
import epson.print.imgsel.JpegHeifImageFinder;
import epson.print.imgsel.JpegImageFinder;
import java.util.ArrayList;

public class PhotoCopyImageSelectActivity extends ImageSelectActivity implements AltAlertDialogFragment.DialogCallback {
    public static final String DIALOG_FRAGMENT_TAG_PROGRESS = "progress";
    private static final int REQUEST_CODE_EXEC_MEMCARD_WRITE = 106;
    private static final String SAVED_INSTANCE_STATE_KEY_WAITING_FOR_TRANSFER_END = "WaitingForTransferEnd";
    private DialogType mDialogType;
    private final Observer<FileConvertViewModel.FileConvertStatus> mFileConvertStateObserver = new Observer<FileConvertViewModel.FileConvertStatus>() {
        public void onChanged(@Nullable FileConvertViewModel.FileConvertStatus fileConvertStatus) {
            PhotoCopyImageSelectActivity.this.onStateChanged(fileConvertStatus);
        }
    };
    private FileConvertViewModel mFileConvertViewModel;
    private boolean mWaitingForTransferEnd;

    public int getMessageType() {
        return 1;
    }

    public void onPositiveCallback() {
    }

    public boolean singleImageMode() {
        return true;
    }

    private void onStateChanged(@Nullable FileConvertViewModel.FileConvertStatus fileConvertStatus) {
        if (fileConvertStatus == null) {
            hideProgress();
            return;
        }
        switch (fileConvertStatus.getConvertStatus()) {
            case TASK_RUNNING:
                showProgress();
                return;
            case TASK_END:
                hideProgress();
                onConvertEnd(fileConvertStatus.getErrorType(), fileConvertStatus.getConvertFileList());
                return;
            default:
                hideProgress();
                return;
        }
    }

    public ImageFinder getImageFinder() {
        if (Build.VERSION.SDK_INT >= 28) {
            return new JpegHeifImageFinder();
        }
        return new JpegImageFinder();
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mWaitingForTransferEnd = false;
        if (bundle != null) {
            mWaitingForTransferEnd = bundle.getBoolean(SAVED_INSTANCE_STATE_KEY_WAITING_FOR_TRANSFER_END);
        }
        mFileConvertViewModel = (FileConvertViewModel) ViewModelProviders.m2of((FragmentActivity) this).get(FileConvertViewModel.class);
        mFileConvertViewModel.getData().observe(this, mFileConvertStateObserver);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 106) {
            mWaitingForTransferEnd = false;
            FileConvertTask.initTempDirectory();
            if (i2 == -2) {
                finish();
            } else if (i2 != 0) {
                mDialogType = new DialogType(112);
                if (intent != null) {
                    mDialogType.mErrorCode = intent.getIntExtra(MemcardWriteProgress.RESULT_KEY_CIFS_ERROR_CODE, 0);
                } else {
                    mDialogType.mErrorCode = 0;
                }
                clearSelectFile();
            } else {
                clearSelectFile();
            }
        }
    }

    protected void onPostResume() {
        super.onPostResume();
        DialogType dialogType = mDialogType;
        if (dialogType != null) {
            localShowDialog(dialogType);
            mDialogType = null;
        }
    }

    protected void localShowDialog(DialogType dialogType) {
        dialogType.getDialogFragment(this).show(getSupportFragmentManager(), "dialog");
    }

    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putBoolean(SAVED_INSTANCE_STATE_KEY_WAITING_FOR_TRANSFER_END, mWaitingForTransferEnd);
    }

    protected void clearSelectFile() {
        refreshSelector(new ArrayList());
    }

    protected void goNext() {
        ArrayList<String> fileArrayList = getImageSelector().getFileArrayList();
        if (fileArrayList.size() > 0) {
            mFileConvertViewModel.startConvert(fileArrayList);
        }
    }

    private void startWriteProgressActivity(@NonNull ArrayList<String> arrayList) {
        Intent intent = new Intent(this, PhotoCopyWriteProgress.class);
        intent.putStringArrayListExtra("imageList", arrayList);
        intent.putExtra("target_folder_name", "");
        intent.putExtra(MemcardWriteProgress.KEY_CIFS_STORAGE_TYPE, 2);
        intent.putExtra(MemcardWriteProgress.KEY_CHECK_WIFI_DIRECT, true);
        ArrayList arrayList2 = new ArrayList();
        arrayList2.add(MemcardConfig.PHOTO_COPY_WRITE_FILE_NAME);
        intent.putStringArrayListExtra(MemcardWriteProgress.KEY_WRITE_FILE_LIST, arrayList2);
        startActivityForResult(intent, 106);
    }

    private static class DialogType {
        public static final int DIALOG_COPY_RESULT_CANCELED = 111;
        public static final int DIALOG_FILE_SIZE_OVER = 113;
        public static final int DIALOG_GENERIC_ERROR = 112;
        public static final int DIALOG_NO_DIALOG = 0;
        private int mDialogCode;
        public int mErrorCode;

        public DialogType(int i) {
            mDialogCode = i;
        }

        public AltAlertDialogFragment getDialogFragment(Context context) {
            String str = "";
            String str2 = null;
            switch (mDialogCode) {
                case 111:
                    str = context.getString(R.string.memcard_file_read_error_message);
                    break;
                case 112:
                    int i = mErrorCode;
                    if (i == 5) {
                        str2 = context.getString(R.string.memcard_capacity_shortage_error_title);
                        str = context.getString(R.string.mcphotocopy_capacity_shortage_error_message);
                        break;
                    } else {
                        switch (i) {
                            case 1:
                                str2 = context.getString(R.string.memcard_comm_error_title);
                                str = context.getString(R.string.memcard_comm_error_message);
                                break;
                            case 2:
                                str2 = context.getString(R.string.EPS_PRNST_BUSY_TITLE);
                                str = context.getString(R.string.EPS_PRNST_BUSY_MSG);
                                break;
                            case 3:
                                str2 = context.getString(R.string.mcphotocopy_media_not_found_title);
                                str = context.getString(R.string.mcphotocopy_media_not_found_message);
                                break;
                            default:
                                str2 = context.getString(R.string.memcard_file_write_error_title);
                                str = context.getString(R.string.mcphotocopy_file_write_error_message);
                                break;
                        }
                    }
                case 113:
                    str = context.getString(R.string.mcphotocopy_file_size_over_message);
                    break;
            }
            return AltAlertDialogFragment.newInstance(str2, str);
        }
    }

    @Nullable
    private LocalProgressDialogFragment getProgressDialog() {
        return (LocalProgressDialogFragment) getSupportFragmentManager().findFragmentByTag("progress");
    }

    private void showProgress() {
        LocalProgressDialogFragment progressDialog = getProgressDialog();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        LocalProgressDialogFragment.newInstance(R.string.convert_google_docs_convert).show(getSupportFragmentManager(), "progress");
    }

    private void hideProgress() {
        LocalProgressDialogFragment progressDialog = getProgressDialog();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    private void onConvertEnd(@NonNull FileConvertTask.ErrorType errorType, @Nullable ArrayList<String> arrayList) {
        if (errorType != FileConvertTask.ErrorType.NO_ERROR) {
            localShowDialog(new DialogType(getErrorDialogType(errorType)));
            clearSelectFile();
        } else if (arrayList != null) {
            mWaitingForTransferEnd = true;
            startWriteProgressActivity(arrayList);
        }
    }

    /* renamed from: com.epson.memcardacc.PhotoCopyImageSelectActivity$2 */
    static /* synthetic */ class C10012 {
        static final /* synthetic */ int[] $SwitchMap$com$epson$memcardacc$FileConvertTask$ErrorType = new int[FileConvertTask.ErrorType.values().length];

        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x0027 */
        static {
            /*
                com.epson.memcardacc.FileConvertTask$ErrorType[] r0 = com.epson.memcardacc.FileConvertTask.ErrorType.values()
                int r0 = r0.length
                int[] r0 = new int[r0]
                $SwitchMap$com$epson$memcardacc$FileConvertTask$ErrorType = r0
                r0 = 1
                int[] r1 = $SwitchMap$com$epson$memcardacc$FileConvertTask$ErrorType     // Catch:{ NoSuchFieldError -> 0x0014 }
                com.epson.memcardacc.FileConvertTask$ErrorType r2 = com.epson.memcardacc.FileConvertTask.ErrorType.FILE_SIZE_ERROR     // Catch:{ NoSuchFieldError -> 0x0014 }
                int r2 = r2.ordinal()     // Catch:{ NoSuchFieldError -> 0x0014 }
                r1[r2] = r0     // Catch:{ NoSuchFieldError -> 0x0014 }
            L_0x0014:
                com.epson.memcardacc.FileConvertViewModel$ConvertStatus[] r1 = com.epson.memcardacc.FileConvertViewModel.ConvertStatus.values()
                int r1 = r1.length
                int[] r1 = new int[r1]
                f142x5e194216 = r1
                int[] r1 = f142x5e194216     // Catch:{ NoSuchFieldError -> 0x0027 }
                com.epson.memcardacc.FileConvertViewModel$ConvertStatus r2 = com.epson.memcardacc.FileConvertViewModel.ConvertStatus.TASK_RUNNING     // Catch:{ NoSuchFieldError -> 0x0027 }
                int r2 = r2.ordinal()     // Catch:{ NoSuchFieldError -> 0x0027 }
                r1[r2] = r0     // Catch:{ NoSuchFieldError -> 0x0027 }
            L_0x0027:
                int[] r0 = f142x5e194216     // Catch:{ NoSuchFieldError -> 0x0032 }
                com.epson.memcardacc.FileConvertViewModel$ConvertStatus r1 = com.epson.memcardacc.FileConvertViewModel.ConvertStatus.TASK_END     // Catch:{ NoSuchFieldError -> 0x0032 }
                int r1 = r1.ordinal()     // Catch:{ NoSuchFieldError -> 0x0032 }
                r2 = 2
                r0[r1] = r2     // Catch:{ NoSuchFieldError -> 0x0032 }
            L_0x0032:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.PhotoCopyImageSelectActivity.C10012.<clinit>():void");
        }
    }

    private int getErrorDialogType(FileConvertTask.ErrorType errorType) {
        return C10012.$SwitchMap$com$epson$memcardacc$FileConvertTask$ErrorType[errorType.ordinal()] != 1 ? 112 : 113;
    }
}
