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.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 DefaultCameraModule cameraModule;
    private Handler main = new Handler(Looper.getMainLooper());

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

    DefaultCameraModule getCameraModule() {
        return cameraModule;
    }

    /* Camera Module is late initiated */
    void setCameraModule(DefaultCameraModule cameraModule) {
        this.cameraModule = cameraModule;
    }

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

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

        runOnUiIfAvailable(() -> getView().showLoading(true));

        imageLoader.loadDeviceImages(isFolderMode, new ImageLoaderListener() {
            @Override
            public void onImageLoaded(final List<Image> images, final List<Folder> folders) {
                runOnUiIfAvailable(() -> {
                    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) {
                runOnUiIfAvailable(() -> 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) {
        if (cameraModule == null) {
            throw new IllegalStateException("Camera Module must be set first");
        }

        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) {
        if (cameraModule == null) {
            throw new IllegalStateException("Camera Module must be set first");
        }

        cameraModule.getImage(context, data, images -> {
            if (config.isReturnAfterFirst()) {
                getView().finishPickImages(images);
            } else {
                getView().showCapturedImage();
            }
        });
    }

    private void runOnUiIfAvailable(Runnable runnable) {
        main.post(() -> {
            if (isViewAttached()) {
                runnable.run();
            }
        });
    }
}

@@ -942,24 +942,124 @@
 aModule() %7B%0A
+        if (cameraModule == null) %7B%0A            cameraModule = new DefaultCameraModule();%0A        %7D%0A
         retu
@@ -1093,39 +1093,50 @@
  /* 
-C
+Set the c
 amera 
-M
+m
 odule i
-s late initiated
+n onRestoreInstance
  */%0A
@@ -2995,135 +2995,8 @@
 ) %7B%0A
-        if (cameraModule == null) %7B%0A            throw new IllegalStateException(%22Camera Module must be set first%22);%0A        %7D%0A%0A
     
@@ -3075,17 +3075,20 @@
 ntent = 
-c
+getC
 ameraMod
@@ -3086,24 +3086,26 @@
 CameraModule
+()
 .getCameraIn
@@ -3480,147 +3480,25 @@
     
-if (cameraModule == null) %7B%0A            throw new IllegalStateException(%22
+get
 Camera
- 
 Module
- must be set first%22);%0A        %7D%0A%0A        cameraModule
+()
 .get

Fix the missing CameraModule initialization