package epson.print.storage.gdrivev3;


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) {
        mGoogleDownloader = GoogleDownloader.createGoogleDownloader(activityWrapper.getActivity(), googleSignInAccount);
        return mGoogleDownloader != null;
    }

    public void invalidateDownloader() {
        mGoogleDownloader = null;
    }

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

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

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

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

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

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