package com.epson.iprint.storage.gdrivev3;



import com.epson.iprint.storage.gdrivev3.DownloadTask;
import com.epson.iprint.storage.gdrivev3.DriveListTask;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;

public class DownloaderWrapper {
    private DownloadTask mDownloadTask;
    private GoogleDownloader mGoogleDownloader;

    public boolean createDownloader(@NonNull ActivityWrapper activityWrapper, @NonNull GoogleSignInAccount googleSignInAccount) {
        this.mGoogleDownloader = GoogleDownloader.createGoogleDownloader(activityWrapper.getActivity(), googleSignInAccount);
        return this.mGoogleDownloader != null;
    }

    public void invalidateDownloader() {
        this.mGoogleDownloader = null;
    }

    public boolean isDownloadValid() {
        return this.mGoogleDownloader != null;
    }

    public void startDownload(@NonNull ActivityWrapper activityWrapper, @NonNull OnlineFile onlineFile, @NonNull DownloadTask.DownloadCompleteListener downloadCompleteListener) {
        GoogleDownloader googleDownloader = this.mGoogleDownloader;
        if (googleDownloader != null) {
            this.mDownloadTask = googleDownloader.startDownload(activityWrapper, onlineFile, downloadCompleteListener);
            return;
        }
        throw new IllegalStateException();
    }

    public void startListTask(@Nullable OnlineFile onlineFile, @NonNull DriveListTask.ListTaskCompleteListener listTaskCompleteListener) {
        GoogleDownloader googleDownloader = this.mGoogleDownloader;
        if (googleDownloader != null) {
            googleDownloader.startListTask(onlineFile, listTaskCompleteListener);
            return;
        }
        throw new IllegalStateException();
    }

    public boolean isRootFolder() {
        return this.mGoogleDownloader.isRootFolder();
    }

    public void cancelDownload() {
        DownloadTask downloadTask = this.mDownloadTask;
        if (downloadTask != null) {
            downloadTask.cancelTask();
        }
    }

    public void onDownloadPreviewEnd() {
        DownloadTask downloadTask = this.mDownloadTask;
        if (downloadTask != null) {
            downloadTask.deleteDownloadFile();
        }
    }
}
