package epson.print.storage.evernote;

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 org.apache.commons.lang.CharEncoding;

import java.util.ArrayList;
import java.util.List;

import epson.print.CommonDefine;
import epson.print.Util.AsyncTaskExecutor;

public class EvernoteClient extends StorageServiceClient {
    public String getStorageServiceName(Context context) {
        return "Evernote";
    }

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

    public boolean isValidUploadName(String str) {
        if (str.startsWith(CommonDefine.DOT)) {
            return false;
        }
        try {
            if (str.getBytes(CharEncoding.UTF_8).length > 256) {
                return false;
            }
            for (char indexOf : str.toCharArray()) {
                if ("\\/:,;*?<>|#、\"".indexOf(indexOf) > -1) {
                    return false;
                }
            }
            return super.isValidUploadName(str);
        } catch (Exception unused) {
            return false;
        }
    }

    public StorageServiceClient.Uploader getUploader(final Context context, StorageServiceClient.UploadFileType uploadFileType, final String str, final String str2) {
        return new StorageServiceClient.Uploader() {
            EvernoteRequest request = new EvernoteRequest(context);
            StorageServiceClient.UploadCompletion uploaded;
            EvernoteRequest.UploadHandler uploader = request.getUploadHandler();

            public boolean isCancelable() {
                return false;
            }

            /* renamed from: com.epson.iprint.storage.evernote.EvernoteClient$1$UploadTask */
            class UploadTask extends AsyncTaskExecutor<Void, Void, Void> {
                UploadTask() {
                }

                protected Void doInBackground(Void... voidArr) {
                    StorageServiceClient.ProcessError processError;
                    StorageServiceClient.ProcessError processError2 = StorageServiceClient.ProcessError.NONE;
                    do {
                        try {
                            StorageItem uploadFolder = C09441.this.request.getUploadFolder();
                            if (uploadFolder == null) {
                                uploadFolder = C09441.this.request.newUploadFolder();
                            }
                            processError = C09441.this.uploader.upload(uploadFolder, str, str2);
                        } catch (Exception e) {
                            processError = C09441.this.request.handleException(e);
                        }
                    } while (StorageServiceClient.ProcessError.RETRY.equals(processError));
                    C09441.this.uploaded.onUploadComplete(str, str2, processError);
                    return null;
                }
            }

            public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
                uploaded = uploadCompletion;
                new UploadTask().executeOnExecutor(new Void[0]);
            }

            public void cancel() {
                uploader.cancel();
            }
        };
    }

    public StorageServiceClient.Downloader getDownloader(final Context context, final StorageItem storageItem, final String str) {
        return new StorageServiceClient.Downloader() {
            StorageServiceClient.DownloadCompletion downloaded;
            EvernoteRequest.DownloadHandler downloader = request.getDownloadHandler();
            EvernoteRequest request = new EvernoteRequest(context);

            public boolean isCancelable() {
                return true;
            }

            /* renamed from: com.epson.iprint.storage.evernote.EvernoteClient$2$DownloadTask */
            class DownloadTask extends AsyncTask<Void, Void, Void> {
                DownloadTask() {
                }

                protected Void doInBackground(Void... voidArr) {
                    StorageServiceClient.ProcessError processError;
                    StorageServiceClient.ProcessError processError2 = StorageServiceClient.ProcessError.NONE;
                    do {
                        try {
                            processError = C09452.this.downloader.download(storageItem.path, str);
                        } catch (Exception e) {
                            processError = C09452.this.request.handleException(e);
                        }
                    } while (StorageServiceClient.ProcessError.RETRY.equals(processError));
                    C09452.this.downloaded.onDownloadComplete(storageItem, str, processError);
                    return null;
                }
            }

            public void start(StorageServiceClient.DownloadCompletion downloadCompletion) {
                downloaded = downloadCompletion;
                new DownloadTask().execute(new Void[0]);
            }

            public void cancel() {
                downloader.cancel();
            }
        };
    }

    public StorageServiceClient.Enumerator getEnumerator(final Context context) {
        return new StorageServiceClient.Enumerator() {
            StorageServiceClient.EnumerateCompletion enumerated;
            EvernoteRequest request = new EvernoteRequest(context);

            private List<StorageItem> getPrintableItems(List<StorageItem> list) {
                ArrayList arrayList = new ArrayList();
                for (StorageItem next : list) {
                    if (next.type != StorageItem.ItemType.FILE || EvernoteClient.this.isPrintableFileTypes(next)) {
                        arrayList.add(next);
                    }
                }
                return arrayList;
            }

            /* renamed from: com.epson.iprint.storage.evernote.EvernoteClient$3$EnumerateTask */
            class EnumerateTask extends AsyncTask<StorageItem, Void, Void> {
                EnumerateTask() {
                }

                protected Void doInBackground(StorageItem... storageItemArr) {
                    StorageServiceClient.ProcessError processError;
                    StorageServiceClient.ProcessError processError2 = StorageServiceClient.ProcessError.NONE;
                    StorageItem storageItem = storageItemArr[0];
                    List<StorageItem> list = null;
                    do {
                        try {
                            processError = StorageServiceClient.ProcessError.NONE;
                            list = C09463.this.request.getItemList(storageItem);
                        } catch (Exception e) {
                            processError = C09463.this.request.handleException(e);
                        }
                    } while (StorageServiceClient.ProcessError.RETRY.equals(processError));
                    if (list != null) {
                        list = C09463.this.getPrintableItems(list);
                    } else {
                        processError = StorageServiceClient.ProcessError.ERROR;
                    }
                    C09463.this.enumerated.onEnumerateComplete(list, processError);
                    return null;
                }
            }

            public StorageItem getRootItem() {
                return request.getFeedItem();
            }

            public void enumerate(StorageItem storageItem, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
                enumerated = enumerateCompletion;
                new EnumerateTask().execute(new StorageItem[]{storageItem});
            }
        };
    }
}
