package com.esafirm.imagepicker.features;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.esafirm.imagepicker.R;
import com.esafirm.imagepicker.features.camera.CameraModule;
import com.esafirm.imagepicker.features.camera.DefaultCameraModule;
import com.esafirm.imagepicker.features.common.BasePresenter;
import com.esafirm.imagepicker.features.common.ImageLoaderListener;
import com.esafirm.imagepicker.model.Folder;
import com.esafirm.imagepicker.model.Image;

import java.io.File;
import java.util.List;

class ImagePickerPresenter extends BasePresenter<ImagePickerView> {

    private ImageFileLoader imageLoader;
    private CameraModule cameraModule = new DefaultCameraModule();
    private Handler handler = new Handler(Looper.getMainLooper());

    ImagePickerPresenter(ImageFileLoader imageLoader) {
        this.imageLoader = imageLoader;
    }

    void abortLoad() {
        imageLoader.abortLoadImages();
    }

    void loadImages(boolean isFolderMode) {
        if (!isViewAttached()) return;

        getView().showLoading(true);
        imageLoader.loadDeviceImages(isFolderMode, new ImageLoaderListener() {
            @Override
            public void onImageLoaded(final List<Image> images, final List<Folder> folders) {
                handler.post(() -> {
                    if (isViewAttached()) {
                        getView().showFetchCompleted(images, folders);

                        final boolean isEmpty = folders != null
                                ? folders.isEmpty()
                                : images.isEmpty();

                        if (isEmpty) {
                            getView().showEmpty();
                        } else {
                            getView().showLoading(false);
                        }
                    }
                });
            }

            @Override
            public void onFailed(final Throwable throwable) {
                handler.post(() -> {
                    if (isViewAttached()) {
                        getView().showError(throwable);
                    }
                });
            }
        });
    }

    void onDoneSelectImages(List<Image> selectedImages) {
        if (selectedImages != null && selectedImages.size() > 0) {

            /* Scan selected images which not existed */
            for (int i = 0; i < selectedImages.size(); i++) {
                Image image = selectedImages.get(i);
                File file = new File(image.getPath());
                if (!file.exists()) {
                    selectedImages.remove(i);
                    i--;
                }
            }
            getView().finishPickImages(selectedImages);
        }
    }

    void captureImage(Activity activity, ImagePickerConfig config, int requestCode) {
        Context context = activity.getApplicationContext();
        Intent intent = cameraModule.getCameraIntent(activity, config);
        if (intent == null) {
            Toast.makeText(context, context.getString(R.string.ef_error_create_image_file), Toast.LENGTH_LONG).show();
            return;
        }
        activity.startActivityForResult(intent, requestCode);
    }

    void finishCaptureImage(Context context, Intent data, final ImagePickerConfig config) {
        cameraModule.getImage(context, data, images -> {
            if (config.isReturnAfterFirst()) {
                getView().finishPickImages(images);
            } else {
                getView().showCapturedImage();
            }
        });
    }
}

@@ -836,23 +836,20 @@
 Handler 
-handler
+main
  = new H
@@ -1140,24 +1140,40 @@
 rn;%0A%0A       
+ main.post(() -%3E
  getView().s
@@ -1188,17 +1188,19 @@
 ng(true)
-;
+);%0A
 %0A       
@@ -1395,39 +1395,36 @@
                 
-handler
+main
 .post(() -%3E %7B%0A  
@@ -1437,32 +1437,33 @@
             if (
+!
 isViewAttached()
@@ -1455,39 +1455,66 @@
 sViewAttached())
- %7B
 %0A    
+                    return;%0A%0A
                 
@@ -1585,20 +1585,16 @@
         
-    
 final bo
@@ -1625,20 +1625,16 @@
 != null%0A
-    
         
@@ -1701,20 +1701,16 @@
         
-    
 : images
@@ -1738,28 +1738,24 @@
             
-    
 if (isEmpty)
@@ -1753,28 +1753,24 @@
 (isEmpty) %7B%0A
-    
             
@@ -1816,36 +1816,32 @@
                 
-    
 %7D else %7B%0A       
@@ -1825,36 +1825,32 @@
        %7D else %7B%0A
-    
                 
@@ -1891,34 +1891,8 @@
 e);%0A
-                        %7D%0A
     
@@ -2048,15 +2048,12 @@
     
-handler
+main
 .pos

Fix crash in Oreo after capturing image