package epson.print.imgsel;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import epson.print.Util.EPLog;

public abstract class MediaStoreImageFinder implements ImageFinder {
    private static final String TAG = "MediaStoreImageFinder";

    protected abstract boolean myCheckFile(File file);

    public Collection<ImageFolderInfo> getFolderPhotoList(ImageFinder.Canceller canceller, ContentResolver contentResolver) {
        try {
            Cursor myQuery = myQuery(contentResolver);
            if (myQuery == null) {
                return null;
            }
            try {
                return getImageDirInfo(myQuery, canceller);
            } catch (Exception unused) {
                return null;
            } finally {
                myQuery.close();
            }
        } catch (Exception unused2) {
            return null;
        }
    }

    private List<ImageFolderInfo> getImageDirInfo(Cursor cursor, ImageFinder.Canceller canceller) {
        EPLog.d(TAG, "Enter getImageDirInfo");
        long currentTimeMillis = System.currentTimeMillis();
        int columnIndexOrThrow = cursor.getColumnIndexOrThrow("_data");
        int columnIndexOrThrow2 = cursor.getColumnIndexOrThrow("_id");
        if (!cursor.moveToFirst()) {
            return null;
        }
        HashMap hashMap = new HashMap();
        int i = 0;
        while (!canceller.checkCanceled()) {
            String string = cursor.getString(columnIndexOrThrow);
            if (string != null) {
                long j = cursor.getLong(columnIndexOrThrow2);
                try {
                    File absoluteFile = new File(string).getAbsoluteFile();
                    String parent = absoluteFile.getParent();
                    if (parent != null) {
                        if (myCheckFile(absoluteFile)) {
                            ImageFolderInfo imageFolderInfo = (ImageFolderInfo) hashMap.get(parent);
                            if (imageFolderInfo == null) {
                                imageFolderInfo = new ImageFolderInfo(parent);
                                hashMap.put(parent, imageFolderInfo);
                            }
                            imageFolderInfo.addImageLastIfPossible(j, absoluteFile.toString(), i);
                        }
                        i++;
                    } else {
                        throw new IOException();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!cursor.moveToNext()) {
                long currentTimeMillis2 = System.currentTimeMillis();
                EPLog.d(TAG, "Leave getImageDirInfo time = " + (currentTimeMillis2 - currentTimeMillis) + " msec");
                return convertTempMapToSortedList(hashMap);
            }
        }
        return null;
    }

    private List<ImageFolderInfo> convertTempMapToSortedList(Map<String, ImageFolderInfo> map) {
        if (map == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList(map.values());
        Collections.sort(arrayList, new ImageFolderInfo.DatabaseOrderComparator());
        return arrayList;
    }

    public List<ImageFileInfo> findImageInDirectory(String str, ContentResolver contentResolver, ImageFinder.Canceller canceller) {
        try {
            Cursor myQuery = myQuery(contentResolver);
            if (myQuery == null) {
                return null;
            }
            try {
                return getImageFileInfoList(str, myQuery, canceller);
            } catch (Exception unused) {
                return null;
            } finally {
                myQuery.close();
            }
        } catch (Exception unused2) {
            return null;
        }
    }

    private List<ImageFileInfo> getImageFileInfoList(String str, Cursor cursor, ImageFinder.Canceller canceller) {
        if (str == null || cursor == null || canceller == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        int columnIndexOrThrow = cursor.getColumnIndexOrThrow("_data");
        int columnIndexOrThrow2 = cursor.getColumnIndexOrThrow("_id");
        if (!cursor.moveToFirst()) {
            return null;
        }
        while (!canceller.checkCanceled()) {
            String string = cursor.getString(columnIndexOrThrow);
            if (string != null) {
                File file = new File(string);
                if (str.equals(file.getParent()) && myCheckFile(file)) {
                    arrayList.add(new ImageFileInfo(file.toString(), cursor.getLong(columnIndexOrThrow2)));
                }
            }
            if (!cursor.moveToNext()) {
                return arrayList;
            }
        }
        return null;
    }

    private Cursor myQuery(ContentResolver contentResolver) {
        if (contentResolver == null) {
            return null;
        }
        return contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data"}, (String) null, (String[]) null, "date_modified DESC");
    }
}
