package de.beyondsoft.ownchat.filepicker.cursors;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;

import de.beyondsoft.ownchat.filepicker.FilePickerConst;

/**
 * Created by Lyndon on 26-Oct-2017.
 */

public class ImageDirectoryLoader extends CursorLoader {
    final String[] IMAGE_PROJECTION = {
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media.TITLE
    };

    public ImageDirectoryLoader(Context context, Bundle args) {
        super(context);
        String bucketId = args.getString(FilePickerConst.EXTRA_BUCKET_ID, null);
        int mediaType = args.getInt(FilePickerConst.EXTRA_FILE_TYPE, FilePickerConst.MEDIA_TYPE_IMAGE);

        setProjection(null);
        setUri(MediaStore.Files.getContentUri("external"));
        setSortOrder(MediaStore.Images.Media._ID + " DESC");

        String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;

        if(mediaType== FilePickerConst.MEDIA_TYPE_VIDEO) {
            selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
        }

        if(bucketId!=null) {
            selection += " AND " + MediaStore.Images.Media.BUCKET_ID + "='" + bucketId + "'";
        }

        setSelection(selection);
    }

    private ImageDirectoryLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        super(context, uri, projection, selection, selectionArgs, sortOrder);
    }
}
