package epson.print.storage.gdrivev3;

import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.Iterator;

public class DriveListTask extends AsyncTask<OnlineFile, Void, ArrayList<OnlineFile>> {
    private Exception mException;
    private final ListTaskCompleteListener mListTaskCompleteListener;
    private final GoogleDownloader mMyDrive;

    public interface ListTaskCompleteListener {
        void listComplete(ArrayList<OnlineFile> arrayList);
    }

    DriveListTask(@NonNull GoogleDownloader googleDownloader, @NonNull ListTaskCompleteListener listTaskCompleteListener) {
        mMyDrive = googleDownloader;
        mListTaskCompleteListener = listTaskCompleteListener;
    }

    protected ArrayList<OnlineFile> doInBackground(OnlineFile... onlineFileArr) {
        try {
            return filterFiles(mMyDrive.listFile(onlineFileArr != null ? onlineFileArr[0] : null));
        } catch (Exception e) {
            e.printStackTrace();
            mException = e;
            return null;
        }
    }

    protected void onPostExecute(ArrayList<OnlineFile> arrayList) {
        mListTaskCompleteListener.listComplete(arrayList);
    }

    @NonNull
    private ArrayList<OnlineFile> filterFiles(@Nullable ArrayList<OnlineFile> arrayList) {
        if (arrayList == null) {
            return new ArrayList<>();
        }
        ArrayList<OnlineFile> arrayList2 = new ArrayList<>(arrayList.size());
        Iterator<OnlineFile> it = arrayList.iterator();
        while (it.hasNext()) {
            OnlineFile next = it.next();
            if (next.isDisplayFile()) {
                arrayList2.add(next);
            }
        }
        return arrayList2;
    }
}
