package epson.print.imgsel;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.os.AsyncTask;

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

import epson.print.ImageItem;

class ImageFindTask extends AsyncTask<ImageFinder, List<ImageItem>, Void> implements ImageFinder.Canceller {
    private static int IMAGE_QUERY_LIMIT = 32;
    String mFolderPath;
    WeakReference<ImageGridFragment> mFragmentReference;

    public ImageFindTask(ImageGridFragment imageGridFragment, String str) {
        mFragmentReference = new WeakReference<>(imageGridFragment);
        mFolderPath = str;
    }

    protected Void doInBackground(ImageFinder... imageFinderArr) {
        ImageGridFragment imageGridFragment;
        List<ImageFileInfo> findImageInDirectory;
        if (imageFinderArr == null || imageFinderArr.length <= 0) {
            return null;
        }
        ImageFinder imageFinder = imageFinderArr[0];
        if (!(isCancelled() || imageFinder == null || (imageGridFragment = (ImageGridFragment) mFragmentReference.get()) == null || imageGridFragment.getActivity() == null || isCancelled() || (findImageInDirectory = imageFinder.findImageInDirectory(this.mFolderPath, getResolver(), this)) == null)) {
            ArrayList arrayList = new ArrayList();
            for (ImageFileInfo next : findImageInDirectory) {
                if (isCancelled()) {
                    return null;
                }
                boolean isImageSelected = isImageSelected(next.mCanonicalPath);
                arrayList.add(new ImageItem((Bitmap) null, isImageSelected ? 1 : 0, next.mCanonicalPath, next.mMediaInfoId));
                if (arrayList.size() % IMAGE_QUERY_LIMIT == 0) {
                    publishProgress(new List[]{arrayList});
                    arrayList = new ArrayList();
                }
            }
            if (arrayList.size() > 0) {
                publishProgress(new List[]{arrayList});
            }
        }
        return null;
    }

    private ContentResolver getResolver() {
        FragmentActivity activity;
        ImageGridFragment imageGridFragment = (ImageGridFragment) mFragmentReference.get();
        if (imageGridFragment == null || (activity = imageGridFragment.getActivity()) == null) {
            return null;
        }
        return activity.getContentResolver();
    }

    private boolean isImageSelected(String str) {
        ImageGridFragment imageGridFragment = (ImageGridFragment) mFragmentReference.get();
        if (imageGridFragment == null) {
            return false;
        }
        return imageGridFragment.isImageSelected(str);
    }

    public boolean checkCanceled() {
        return isCancelled();
    }

    protected void onPreExecute() {
        ImageGridFragment imageGridFragment = (ImageGridFragment) mFragmentReference.get();
        if (imageGridFragment != null) {
            imageGridFragment.clearItem();
        }
    }

    protected void onProgressUpdate(List<ImageItem>... listArr) {
        ImageGridFragment imageGridFragment = (ImageGridFragment) mFragmentReference.get();
        if (imageGridFragment != null && listArr != null && listArr.length > 0) {
            imageGridFragment.addItem(listArr[0]);
        }
    }

    protected void onPostExecute(Void voidR) {
        ImageGridFragment imageGridFragment = (ImageGridFragment) mFragmentReference.get();
        if (imageGridFragment != null) {
            imageGridFragment.updateData();
        }
    }
}
