package com.epson.cameracopy.ui;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;


import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Bundle;


import android.support.p000v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;

import com.epson.cameracopy.alt.UiCommon;
import com.epson.cameracopy.device.CameraFile;
import com.epson.cameracopy.device.CameraPreviewControl;
import com.epson.mobilephone.common.wifidirect.NfcTagUtils;

import epson.common.ImageUtil;
import epson.common.ScalableImageView;
import epson.print.ActivityIACommon;
import epson.print.ActivityRequestPermissions;

import epson.print.IprintApplication;
import epson.print.Util.CommonMessage;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import org.opencv.videoio.Videoio;

/* renamed from: com.epson.cameracopy.ui.ImagePreviewActivity */
public class ImagePreviewActivity extends ActivityIACommon {
    public static final String PARAM_CAMERA_FILE = "camera-file";
    public static final String PARAM_PICTURE_FILENAME = "file_name";
    public static final String PARAM_SHOW_SIZE_SET_MESSAGE_ON_PRINT_PREVIEW = "pram-sizeset-message-printpreview";
    private static final int REQEST_CODE_IMAGE_CROP = 1;
    private static final int REQEST_RUNTIMEPERMMISSION = 2;
    private static final int REQUEST_WRITE_PERMISSION_FOR_IMAGE_FORMAT_CONVERT = 3;
    private PlaceholderFragment mFragment;

    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_picture_view);
        setActionBar(R.string.image_preview_title, true);
        if (bundle == null) {
            Intent intent = getIntent();
            if (intent == null) {
                finish();
                return;
            }
            String stringExtra = intent.getStringExtra(PARAM_PICTURE_FILENAME);
            CameraFile cameraFile = (CameraFile) intent.getSerializableExtra(PARAM_CAMERA_FILE);
            if (stringExtra == null && cameraFile == null) {
                finish();
                return;
            }
            Bundle extras = intent.getExtras();
            if (extras.containsKey(PARAM_PICTURE_FILENAME) && extras.containsKey(PARAM_CAMERA_FILE)) {
                finish();
            } else if (cameraFile == null || new File(cameraFile.getValidFileName()).exists()) {
                mFragment = PlaceholderFragment.getInstance(stringExtra, cameraFile);
                getSupportFragmentManager().beginTransaction().add(R.id.container, (Fragment) mFragment).commit();
            } else {
                finish();
            }
        }
    }

    protected void onPause() {
        super.onPause();
        NfcTagUtils.disableForegroundDispatch(this);
    }

    protected void onResume() {
        super.onResume();
        NfcTagUtils.enableForegroundDispatch(this, (IntentFilter[]) null, (String[][]) null);
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        CommonMessage.showInvalidPrintMessageIfEpsonNfcPrinter(this, intent);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        menuItem.getItemId();
        return super.onOptionsItemSelected(menuItem);
    }

    public void onBackPressed() {
        PlaceholderFragment placeholderFragment = mFragment;
        if (placeholderFragment == null || !placeholderFragment.checkProgress()) {
            backCameraPreviewActivity();
        }
    }

    public void backCameraPreviewActivity() {
        Intent intent = new Intent(this, CameraPreviewActivity.class);
        intent.addFlags(67108864);
        intent.addFlags(Videoio.CAP_INTELPERC_DEPTH_GENERATOR);
        startActivity(intent);
    }

    /* renamed from: com.epson.cameracopy.ui.ImagePreviewActivity$PlaceholderFragment */
    public static class PlaceholderFragment extends Fragment {
        static final /* synthetic */ boolean $assertionsDisabled = false;
        private static final String KEY_PREVIEW_FILE = "preview-file";
        private static final String PARAM_CAMERA_FILE = "camera-file";
        private static final String PARAM_FILE_NAME = "file_name";
        ImagePreviewFile mImagePreviewFile;
        private ScalableImageView mImageView;
        private ProgressBar mProgressBar;
        private Button mSaveButton;
        private boolean mShowSetDisplaySizeMessageOnPrintPreview;

        static {
            Class<ImagePreviewActivity> cls = ImagePreviewActivity.class;
        }

        public static PlaceholderFragment getInstance(String str, CameraFile cameraFile) {
            PlaceholderFragment placeholderFragment = new PlaceholderFragment();
            Bundle bundle = new Bundle();
            bundle.putCharSequence("file_name", str);
            bundle.putSerializable("camera-file", cameraFile);
            placeholderFragment.setArguments(bundle);
            return placeholderFragment;
        }

        private void setPreview(ScalableImageView scalableImageView, String str) {
            scalableImageView.SetImageBitmap(ImageUtil.loadBitmap(str, 1200, 1200, true, true), getResources());
            try {
                int attributeInt = new ExifInterface(str).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
                if (attributeInt == 3) {
                    scalableImageView.SetRotate(180.0f);
                } else if (attributeInt == 6) {
                    scalableImageView.SetRotate(90.0f);
                } else if (attributeInt != 8) {
                    scalableImageView.SetRotate(0.0f);
                } else {
                    scalableImageView.SetRotate(270.0f);
                }
            } catch (IOException unused) {
            }
        }

        public void setPreview() {
            String printFileName = mImagePreviewFile.getPrintFileName();
            if (printFileName != null) {
                setPreview(mImageView, printFileName);
            }
        }

        public void restorePreview() {
            ScalableImageView scalableImageView = mImageView;
            if (scalableImageView != null && mImagePreviewFile != null && scalableImageView.getBitmap() == null) {
                setPreview();
            }
        }

        public void releasePreviewBitmap() {
            ScalableImageView scalableImageView = mImageView;
            if (scalableImageView != null) {
                scalableImageView.SetImageBitmap((Bitmap) null, (Resources) null);
            }
        }

        public void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            if (bundle != null) {
                mImagePreviewFile = (ImagePreviewFile) bundle.getSerializable(KEY_PREVIEW_FILE);
                return;
            }
            Bundle arguments = getArguments();
            if (arguments != null) {
                CameraFile cameraFile = (CameraFile) arguments.getSerializable("camera-file");
                if (cameraFile != null) {
                    mShowSetDisplaySizeMessageOnPrintPreview = false;
                    mImagePreviewFile = new CameraFileAdapter(cameraFile);
                    return;
                }
                mShowSetDisplaySizeMessageOnPrintPreview = true;
            }
        }

        public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
            View inflate = layoutInflater.inflate(R.layout.fragment_image_preview_view, viewGroup, false);
            mImageView = (ScalableImageView) inflate.findViewById(R.id.preview_view);
            mProgressBar = (ProgressBar) inflate.findViewById(R.id.progressBar);
            mSaveButton = (Button) inflate.findViewById(R.id.save_button);
            mSaveButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    if (!PlaceholderFragment.this.checkProgress()) {
                        if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
                            String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
                            String[] strArr2 = {PlaceholderFragment.this.getString(R.string.permission_dialog_title), PlaceholderFragment.this.getString(R.string.permission_dialog_title)};
                            PlaceholderFragment placeholderFragment = PlaceholderFragment.this;
                            PlaceholderFragment placeholderFragment2 = PlaceholderFragment.this;
                            ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], strArr2, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Fragment) placeholderFragment, placeholderFragment.getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Fragment) placeholderFragment2, placeholderFragment2.getString(R.string.permission_dialog_message_storage), PlaceholderFragment.this.getString(R.string.permission_function_storage))});
                            if (!ActivityRequestPermissions.checkPermission(PlaceholderFragment.this.getActivity(), strArr)) {
                                ActivityRequestPermissions.requestPermission((Fragment) PlaceholderFragment.this, permission, 2);
                                return;
                            }
                        }
                        PlaceholderFragment.this.saveImage();
                    }
                }
            });
            ((Button) inflate.findViewById(R.id.print_button)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    if (!PlaceholderFragment.this.checkProgress()) {
                        PlaceholderFragment.this.startPrintPreviewActivity();
                    }
                }
            });
            ((Button) inflate.findViewById(R.id.retouch_button)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    if (!PlaceholderFragment.this.checkProgress()) {
                        PlaceholderFragment.this.startImageCollectActivity();
                    }
                }
            });
            if (mImagePreviewFile != null) {
                mProgressBar.setVisibility(View.GONE);
                setPreview();
                UiCommon.setButtonEnabled(mSaveButton, mImagePreviewFile.needSaveButton());
            } else {
                mProgressBar.setVisibility(View.VISIBLE);
                observeImageConvertFromArgument();
            }
            return inflate;
        }

        private void observeImageConvertFromArgument() {
            String str;
            Bundle arguments = getArguments();
            if (arguments != null && (str = (String) arguments.getCharSequence("file_name")) != null) {
                observeImageFolderFile(str);
            }
        }

        private void observeImageFolderFile(@Nullable String str) {
            if (str != null) {
                ImageFolderFile imageFolderFile = new ImageFolderFile(str);
                if (!imageFolderFile.needsConvert(IprintApplication.getInstance()) || !requestWritePermissionIfNeeded()) {
                    ImageFolderFileViewModel imageFolderFileViewModel = (ImageFolderFileViewModel) ViewModelProviders.m0of((Fragment) this).get(ImageFolderFileViewModel.class);
                    imageFolderFileViewModel.setOriginalData(imageFolderFile);
                    imageFolderFileViewModel.getData().observe(this, new Observer<ImageFolderFile>() {
                        public void onChanged(@Nullable ImageFolderFile imageFolderFile) {
                            PlaceholderFragment.this.localFolderFileViewModelPrepared(imageFolderFile);
                        }
                    });
                }
            }
        }

        private boolean requestWritePermissionIfNeeded() {
            if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
                String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
                ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], new String[]{getString(R.string.permission_dialog_title), getString(R.string.permission_dialog_title)}, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Fragment) this, getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Fragment) this, getString(R.string.permission_dialog_message_storage), getString(R.string.permission_function_storage))});
                if (!ActivityRequestPermissions.checkPermission(getActivity(), strArr)) {
                    ActivityRequestPermissions.requestPermission((Fragment) this, permission, 3);
                    return true;
                }
            }
            return false;
        }

        private boolean checkProgress() {
            return mProgressBar.getVisibility() == 0;
        }

        private void localFolderFileViewModelPrepared(@Nullable ImageFolderFile imageFolderFile) {
            mProgressBar.setVisibility(View.GONE);
            mImagePreviewFile = imageFolderFile;
            UiCommon.setButtonEnabled(mSaveButton, mImagePreviewFile.needSaveButton());
            restorePreview();
        }

        public void onStart() {
            super.onStart();
            restorePreview();
        }

        public void onPause() {
            if (getActivity().isFinishing()) {
                releasePreviewBitmap();
                mImagePreviewFile.deleteTemporaryFile();
            }
            super.onPause();
        }

        public void onStop() {
            releasePreviewBitmap();
            super.onStop();
        }

        public void onSaveInstanceState(Bundle bundle) {
            super.onSaveInstanceState(bundle);
            bundle.putSerializable(KEY_PREVIEW_FILE, (Serializable) mImagePreviewFile);
        }

        public void saveImage() {
            UiCommon.setButtonEnabled(mSaveButton, false);
            if (!mImagePreviewFile.doSave(CameraPreviewControl.getInstance(getActivity()).getSaveDirecotry())) {
                UiCommon.setButtonEnabled(mSaveButton, true);
                SimpleMessageDialogFragment.newInstance(R.string.file_save_error_message).show(getFragmentManager(), "error-dialog");
                return;
            }
            File file = new File(mImagePreviewFile.getPrintFileName());
            ImageUtil.galleryAddPic(getActivity(), file);
            FileSaveDialogFragment.newInstance(1, file.getParentFile().getName()).show(getFragmentManager(), "ok-dialog");
        }

        public void startPrintPreviewActivity() {
            String printFileName = mImagePreviewFile.getPrintFileName();
            if (printFileName != null) {
                Intent intent = new Intent(getActivity(), PrintPreviewActivity.class);
                ArrayList arrayList = new ArrayList(1);
                arrayList.add(printFileName);
                intent.putExtra("imageList", arrayList);
                if (mShowSetDisplaySizeMessageOnPrintPreview) {
                    intent.putExtra(PrintPreviewActivity.PARAM_DISPLAY_SIZE_SET_MESSAGE, true);
                }
                startActivity(intent);
            }
        }

        public void startImageCollectActivity() {
            String imageProcFileName = mImagePreviewFile.getImageProcFileName(getActivity());
            if (imageProcFileName != null) {
                Intent intent = new Intent(getActivity(), CropImageActivity.class);
                intent.putExtra(CropImageActivity.IMAGE_FILE_NAME, imageProcFileName);
                startActivityForResult(intent, 1);
            }
        }

        public void onActivityResult(int i, int i2, Intent intent) {
            String stringExtra;
            switch (i) {
                case 1:
                    if (i2 == -1 && (stringExtra = intent.getStringExtra(CropImageActivity.RESULT_COLLECTED_FILE_NAME)) != null) {
                        mImagePreviewFile.deleteTemporaryFile();
                        mImagePreviewFile = new CameraFileAdapter(new CameraFile(new Date(), stringExtra));
                        setPreview();
                        UiCommon.setButtonEnabled(mSaveButton, mImagePreviewFile.needSaveButton());
                        mShowSetDisplaySizeMessageOnPrintPreview = false;
                        startPrintPreviewActivity();
                        return;
                    }
                    return;
                case 2:
                    if (i2 == -1) {
                        saveImage();
                        return;
                    }
                    return;
                case 3:
                    if (i2 != -1) {
                        FragmentActivity activity = getActivity();
                        if (activity != null) {
                            activity.finish();
                            return;
                        }
                        return;
                    }
                    observeImageConvertFromArgument();
                    return;
                default:
                    return;
            }
        }
    }

    @SuppressLint({"ValidFragment"})
    /* renamed from: com.epson.cameracopy.ui.ImagePreviewActivity$FileSaveDialogFragment */
    public static class FileSaveDialogFragment extends DialogFragment {
        private static final String PARAM_FILENAME = "filename";
        private static final String PARAM_TYPE = "type";
        private String mFileName;

        static FileSaveDialogFragment newInstance(int i, String str) {
            FileSaveDialogFragment fileSaveDialogFragment = new FileSaveDialogFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type", i);
            bundle.putString(PARAM_FILENAME, str);
            fileSaveDialogFragment.setArguments(bundle);
            return fileSaveDialogFragment;
        }

        public void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            Bundle arguments = getArguments();
            arguments.getInt("type");
            mFileName = arguments.getString(PARAM_FILENAME);
        }

        public Dialog onCreateDialog(Bundle bundle) {
            super.onCreateDialog(bundle);
            return new AlertDialog.Builder(getActivity()).setMessage(getResources().getString(R.string.image_save_message, new Object[]{mFileName})).setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).create();
        }
    }
}
