package de.beyondsoft.ownchat.filepicker.cursors.loadercallbacks;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import de.beyondsoft.ownchat.filepicker.PickerManager;
import de.beyondsoft.ownchat.filepicker.cursors.ImageDirectoryLoader;
import de.beyondsoft.ownchat.filepicker.models.ImageDirectory;
import de.beyondsoft.ownchat.utils.Constants;

import static android.provider.BaseColumns._ID;
import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE;
import static android.provider.MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME;
import static android.provider.MediaStore.Images.ImageColumns.BUCKET_ID;
import static android.provider.MediaStore.MediaColumns.DATA;
import static android.provider.MediaStore.MediaColumns.DATE_ADDED;
import static android.provider.MediaStore.MediaColumns.TITLE;

public class ImageDirLoaderCallback implements LoaderManager.LoaderCallbacks<Cursor> {
    public final static int INDEX_ALL_PHOTOS = 0;
    private WeakReference<Context> context;
    private FileResultCallback<ImageDirectory> resultCallback;

    public ImageDirLoaderCallback(Context context, FileResultCallback<ImageDirectory> resultCallback) {
        this.context = new WeakReference<>(context);
        this.resultCallback = resultCallback;
    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new ImageDirectoryLoader(context.get(), args);
    }

    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
        if(data == null) {
            return;
        }

        List<ImageDirectory> directories = new ArrayList<>();

        while(data.moveToNext()) {
            int imageId = data.getInt(data.getColumnIndexOrThrow(_ID));
            String bucketId = data.getString(data.getColumnIndexOrThrow(BUCKET_ID));
            String name = data.getString(data.getColumnIndexOrThrow(BUCKET_DISPLAY_NAME));
            String path = data.getString(data.getColumnIndexOrThrow(DATA));
            String fileName = data.getString(data.getColumnIndexOrThrow(TITLE));
            int mediaType = data.getInt(data.getColumnIndexOrThrow(MEDIA_TYPE));

            ImageDirectory photoDirectory = new ImageDirectory();
            photoDirectory.setBucketId(bucketId);
            photoDirectory.setName(name);

            if(!directories.contains(photoDirectory)) {
                if(path.toLowerCase().endsWith("gif")) {
                    if(PickerManager.getInstance().isShowGif()) {
                        photoDirectory.addPhoto(imageId, fileName, path, mediaType);
                    }
                } else {
                    photoDirectory.addPhoto(imageId, fileName, path, mediaType);
                }

                photoDirectory.setDateAdded(data.getLong(data.getColumnIndexOrThrow(DATE_ADDED)));
                directories.add(photoDirectory);
            } else {
                directories.get(directories.indexOf(photoDirectory)).addPhoto(imageId, fileName, path, mediaType);
            }

        }

        if(resultCallback != null) {
            resultCallback.onResultCallback(directories);
        }
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {
        Log.d(Constants.LOG_TAG_FILE_PICKER, "Resetting loader. This method is not implemented!");
    }
}
