package com.epson.iprint.storage.gdrivev3;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;

import com.epson.iprint.storage.StorageItem;
import com.epson.iprint.storage.StorageServiceClient;
import com.epson.iprint.storage.gdrivev3.IprintGoogleSignIn;
import epson.print.R;

public class GoogleV3UploadClient extends StorageServiceClient {
    public static final String UPLOAD_FOLDER_NAME = "Epson iPrint";
    private static boolean sCheckSignIn;
    private IprintGoogleSignIn mIprintGoogleSignIn = new IprintGoogleSignIn();

    public boolean isSupportedUploadType(StorageServiceClient.UploadFileType uploadFileType) {
        return true;
    }

    public static boolean isCheckSignIn() {
        return sCheckSignIn;
    }

    public static void setSingInValue(boolean z) {
        sCheckSignIn = z;
    }

    public GoogleV3UploadClient() {
        sCheckSignIn = false;
    }

    public StorageServiceClient.Uploader getUploader(Context context, StorageServiceClient.UploadFileType uploadFileType, String str, String str2) {
        return new GoogleV3Uploader(context, new UploadInfo(uploadFileType, str, str2));
    }

    public StorageServiceClient.Downloader getDownloader(Context context, StorageItem storageItem, String str) {
        throw new UnsupportedOperationException();
    }

    @NonNull
    public StorageServiceClient.Enumerator getEnumerator(Context context) {
        throw new UnsupportedOperationException();
    }

    public boolean isSignedIn(Context context) {
        if (!isCheckSignIn()) {
            return false;
        }
        return this.mIprintGoogleSignIn.isSignInValid(context, IprintGoogleSignIn.OperationType.UPLOAD);
    }

    public boolean revokeSignedInData(Activity activity) {
        activity.startActivity(GoogleV3UploadSignInActivity.getDisconnectIntent(activity));
        return false;
    }

    public String getStorageServiceName(Context context) {
        return context.getString(R.string.google_drive);
    }

    class GoogleV3Uploader extends StorageServiceClient.Uploader {
        private final Context mContext;
        private UploadInfo mUploadInfo;

        public boolean isCancelable() {
            return false;
        }

        public GoogleV3Uploader(Context context, @NonNull UploadInfo uploadInfo) {
            super();
            this.mContext = context.getApplicationContext();
            this.mUploadInfo = uploadInfo;
        }

        public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
            new UploadTask(this.mContext, uploadCompletion).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new UploadInfo[]{this.mUploadInfo});
        }
    }

    private static class UploadTask extends AsyncTask<UploadInfo, Void, Void> {
        private final Context mContext;
        private final StorageServiceClient.UploadCompletion mUploadNotifier;

        UploadTask(Context context, StorageServiceClient.UploadCompletion uploadCompletion) {
            this.mContext = context;
            this.mUploadNotifier = uploadCompletion;
        }

        protected Void doInBackground(UploadInfo... uploadInfoArr) {
            if (uploadInfoArr == null || uploadInfoArr[0] == null) {
                this.mUploadNotifier.onUploadComplete((String) null, (String) null, StorageServiceClient.ProcessError.ERROR);
                return null;
            }
            UploadInfo uploadInfo = uploadInfoArr[0];
            DriveWriter createDriveWriter = DriveWriter.createDriveWriter(this.mContext);
            try {
                createDriveWriter.uploadFile(uploadInfo.originalLocalPath, uploadInfo.uploadFilename, uploadInfo.fileType == StorageServiceClient.UploadFileType.PDF ? "application/pdf" : "image/jpeg", createDriveWriter.findOrCreateFolder("Epson iPrint"));
                notifyUploadEnd(uploadInfo, StorageServiceClient.ProcessError.NONE);
                return null;
            } catch (Exception unused) {
                notifyUploadEnd(uploadInfo, StorageServiceClient.ProcessError.ERROR);
                return null;
            }
        }

        private void notifyUploadEnd(UploadInfo uploadInfo, StorageServiceClient.ProcessError processError) {
            StorageServiceClient.UploadCompletion uploadCompletion = this.mUploadNotifier;
            if (uploadCompletion != null) {
                uploadCompletion.onUploadComplete(uploadInfo.originalLocalPath, uploadInfo.uploadFilename, processError);
            }
        }
    }

    public static class UploadInfo {
        StorageServiceClient.UploadFileType fileType;
        String originalLocalPath;
        String uploadFilename;

        public UploadInfo(@NonNull StorageServiceClient.UploadFileType uploadFileType, @NonNull String str, @NonNull String str2) {
            this.fileType = uploadFileType;
            this.originalLocalPath = str;
            this.uploadFilename = str2;
        }
    }
}
