package com.esafirm.imagepicker.features;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;

import com.esafirm.imagepicker.R;
import com.esafirm.imagepicker.helper.Constants;
import com.esafirm.imagepicker.model.Image;

import java.util.ArrayList;

public abstract class ImagePicker {

    private int mode;
    private int limit;
    private boolean showCamera;
    private String folderTitle;
    private String imageTitle;
    private ArrayList<Image> selectedImages;
    private boolean folderMode;
    private String imageDirectory;

    public abstract void start(int requestCode);

    public static class ImagePickerWithActivity extends ImagePicker {

        private Activity activity;

        public ImagePickerWithActivity(Activity activity) {
            this.activity = activity;
            init(activity);
        }

        @Override
        public void start(int requestCode) {
            Intent intent = getIntent(activity);
            activity.startActivityForResult(intent, requestCode);
        }
    }

    public static class ImagePickerWithFragment extends ImagePicker {

        private Fragment fragment;

        public ImagePickerWithFragment(Fragment fragment) {
            this.fragment = fragment;
            init(fragment.getActivity());
        }

        @Override
        public void start(int requestCode) {
            Intent intent = getIntent(fragment.getActivity());
            fragment.startActivityForResult(intent, requestCode);
        }
    }


    public void init(Activity activity) {
        this.mode = ImagePickerActivity.MODE_MULTIPLE;
        this.limit = Constants.MAX_LIMIT;
        this.showCamera = true;
        this.folderTitle = activity.getString(R.string.title_folder);
        this.imageTitle = activity.getString(R.string.title_select_image);
        this.selectedImages = new ArrayList<>();
        this.folderMode = false;
        this.imageDirectory = activity.getString(R.string.image_directory);
    }


    public static ImagePickerWithActivity create(Activity activity) {
        return new ImagePickerWithActivity(activity);
    }

    public static ImagePickerWithFragment create(Fragment fragment) {
        return new ImagePickerWithFragment(fragment);
    }

    public ImagePicker single() {
        mode = ImagePickerActivity.MODE_SINGLE;
        return this;
    }

    public ImagePicker multi() {
        mode = ImagePickerActivity.MODE_MULTIPLE;
        return this;
    }


    public ImagePicker limit(int count) {
        limit = count;
        return this;
    }

    public ImagePicker showCamera(boolean show) {
        showCamera = show;
        return this;
    }

    public ImagePicker folderTitle(String title) {
        this.folderTitle = title;
        return this;
    }

    public ImagePicker imageTitle(String title) {
        this.imageTitle = title;
        return this;
    }

    public ImagePicker origin(ArrayList<Image> images) {
        selectedImages = images;
        return this;
    }

    public ImagePicker folderMode(boolean folderMode) {
        this.folderMode = folderMode;
        return this;
    }

    public ImagePicker imageDirectory(String directory) {
        this.imageDirectory = directory;
        return this;
    }

    public Intent getIntent(Activity activity) {
        Intent intent = new Intent(activity, ImagePickerActivity.class);
        intent.putExtra(ImagePickerActivity.EXTRA_MODE, mode);
        intent.putExtra(ImagePickerActivity.EXTRA_LIMIT, limit);
        intent.putExtra(ImagePickerActivity.EXTRA_SHOW_CAMERA, showCamera);
        intent.putExtra(ImagePickerActivity.EXTRA_FOLDER_TITLE, folderTitle);
        intent.putExtra(ImagePickerActivity.EXTRA_IMAGE_TITLE, imageTitle);
        intent.putExtra(ImagePickerActivity.EXTRA_SELECTED_IMAGES, selectedImages);
        intent.putExtra(ImagePickerActivity.EXTRA_FOLDER_MODE, folderMode);
        intent.putExtra(ImagePickerActivity.EXTRA_IMAGE_DIRECTORY, imageDirectory);
        return intent;
    }


}

@@ -65,16 +65,48 @@
 tivity;%0A
+import android.content.Context;%0A
 import a
@@ -128,16 +128,16 @@
 Intent;%0A
-
 import a
@@ -324,16 +324,39 @@
 rayList;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -1617,33 +1617,31 @@
 id init(
-Activity activity
+Context context
 ) %7B%0A    
@@ -1788,32 +1788,31 @@
 lderTitle = 
-activity
+context
 .getString(R
@@ -1860,24 +1860,23 @@
 Title = 
-activity
+context
 .getStri
@@ -2020,24 +2020,23 @@
 ctory = 
-activity
+context
 .getStri
@@ -3374,33 +3374,31 @@
 tIntent(
-Activity activity
+Context context
 ) %7B%0A    
@@ -3428,24 +3428,23 @@
  Intent(
-activity
+context
 , ImageP
@@ -4097,11 +4097,232 @@
 %0A    %7D%0A%0A
+    public static List%3CImage%3E getImages(Intent intent) %7B%0A        if (intent == null) %7B%0A            return null;%0A        %7D%0A        return intent.getParcelableArrayListExtra(ImagePickerActivity.EXTRA_SELECTED_IMAGES);%0A    %7D
 %0A%7D%0A

Add helper method ImagePicker.getImage(Intent)