package com.epson.iprint.storage.gdrivev3;

import android.content.Context;


import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.http.FileContent;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import epson.print.gdconv.ConvertStatus;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class DriveWriter {
    private static final String ROOT_ID = "root";
    private static final String TAG = "DriveConverter ";
    private Drive mDrive;

    public interface ConvertStatusNotifier {
        void changeStatus(ConvertStatus convertStatus);
    }

    @NonNull
    public static String getWriteScope() {
        return "https://www.googleapis.com/auth/drive";
    }

    public DriveWriter(@NonNull Drive drive) {
        this.mDrive = drive;
    }

    public void uploadFileToRoot(@NonNull String str, @NonNull String str2, @NonNull String str3) throws IOException {
        uploadFile(str, str2, str3, ROOT_ID);
    }

    public String uploadFile(@NonNull String str, @NonNull String str2, @NonNull String str3, @NonNull String str4) throws IOException {
        return ((File) this.mDrive.files().create(new File().setParents(Collections.singletonList(str4)).setMimeType(str3).setName(str2), new FileContent(str3, new java.io.File(str))).setFields("id, parents, mimeType").execute()).getId();
    }

    @NonNull
    public List<String> findFolder(@Nullable String str, String str2) throws IOException {
        if (str == null) {
            str = ROOT_ID;
        }
        if (str2 == null) {
            return Arrays.asList(new String[]{ROOT_ID});
        }
        List<File> files = ((FileList) this.mDrive.files().list().setQ(String.format(Locale.US, "name = '%s' and '%s' in parents and trashed = false and mimeType contains '%s'", new Object[]{str2, str, GoogleDriveMimeType.MIME_TYPE_GOOGLE_FOLDER})).execute()).getFiles();
        ArrayList arrayList = new ArrayList();
        for (File id : files) {
            arrayList.add(id.getId());
        }
        return arrayList;
    }

    public String createFolderInRoot(@NonNull String str) throws IOException {
        return ((File) this.mDrive.files().create(new File().setName(str).setMimeType(GoogleDriveMimeType.MIME_TYPE_GOOGLE_FOLDER)).setFields("id").execute()).getId();
    }

    public void convertFileToPdf(@NonNull String str, @NonNull String str2, @Nullable String str3, @NonNull java.io.File file, ConvertStatusNotifier convertStatusNotifier) throws IOException {
        if (str3 == null) {
            str3 = ROOT_ID;
        }
        String googleDocsConvertMimeType = getGoogleDocsConvertMimeType(str2);
        if (googleDocsConvertMimeType != null) {
            File file2 = (File) this.mDrive.files().create(new File().setParents(Collections.singletonList(str3)).setMimeType(googleDocsConvertMimeType).setName("iprint_convert_tmp.tmp"), new FileContent(str2, new java.io.File(str))).setFields("id, parents").execute();
            if (convertStatusNotifier != null) {
                convertStatusNotifier.changeStatus(ConvertStatus.DOWNLOAD);
            }
            String id = file2.getId();
            try {
                this.mDrive.files().export(id, "application/pdf").executeMediaAndDownloadTo(new FileOutputStream(file));
            } finally {
                this.mDrive.files().delete(id).execute();
            }
        } else {
            throw new ConvertMimeTypeException("mime type <" + str2 + "> not supported");
        }
    }

    /* JADX WARNING: Can't fix incorrect switch cases order */
    @android.support.annotation.Nullable
    @android.support.annotation.VisibleForTesting
    /* Code decompiled incorrectly, please refer to instructions dump. */
    static java.lang.String getGoogleDocsConvertMimeType(@android.support.annotation.NonNull java.lang.String r1) {
        /*
            int r0 = r1.hashCode()
            switch(r0) {
                case -1248332507: goto L_0x0071;
                case -1082243251: goto L_0x0067;
                case -1073633483: goto L_0x005c;
                case -1071817359: goto L_0x0051;
                case -1050893613: goto L_0x0047;
                case -1004747228: goto L_0x003c;
                case -366307023: goto L_0x0032;
                case 717553764: goto L_0x0028;
                case 817335912: goto L_0x001e;
                case 904647503: goto L_0x0014;
                case 1993842850: goto L_0x0009;
                default: goto L_0x0007;
            }
        L_0x0007:
            goto L_0x007b
        L_0x0009:
            java.lang.String r0 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 6
            goto L_0x007c
        L_0x0014:
            java.lang.String r0 = "application/msword"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 1
            goto L_0x007c
        L_0x001e:
            java.lang.String r0 = "text/plain"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 2
            goto L_0x007c
        L_0x0028:
            java.lang.String r0 = "application/vnd.google-apps.document"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 5
            goto L_0x007c
        L_0x0032:
            java.lang.String r0 = "application/vnd.ms-excel"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 7
            goto L_0x007c
        L_0x003c:
            java.lang.String r0 = "text/csv"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 8
            goto L_0x007c
        L_0x0047:
            java.lang.String r0 = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 0
            goto L_0x007c
        L_0x0051:
            java.lang.String r0 = "application/vnd.ms-powerpoint"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 9
            goto L_0x007c
        L_0x005c:
            java.lang.String r0 = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 10
            goto L_0x007c
        L_0x0067:
            java.lang.String r0 = "text/html"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 3
            goto L_0x007c
        L_0x0071:
            java.lang.String r0 = "application/rtf"
            boolean r1 = r1.equals(r0)
            if (r1 == 0) goto L_0x007b
            r1 = 4
            goto L_0x007c
        L_0x007b:
            r1 = -1
        L_0x007c:
            switch(r1) {
                case 0: goto L_0x0087;
                case 1: goto L_0x0087;
                case 2: goto L_0x0087;
                case 3: goto L_0x0087;
                case 4: goto L_0x0087;
                case 5: goto L_0x0087;
                case 6: goto L_0x0084;
                case 7: goto L_0x0084;
                case 8: goto L_0x0084;
                case 9: goto L_0x0081;
                case 10: goto L_0x0081;
                default: goto L_0x007f;
            }
        L_0x007f:
            r1 = 0
            return r1
        L_0x0081:
            java.lang.String r1 = "application/vnd.google-apps.presentation"
            return r1
        L_0x0084:
            java.lang.String r1 = "application/vnd.google-apps.spreadsheet"
            return r1
        L_0x0087:
            java.lang.String r1 = "application/vnd.google-apps.document"
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.gdrivev3.DriveWriter.getGoogleDocsConvertMimeType(java.lang.String):java.lang.String");
    }

    public String findOrCreateFolder(@NonNull String str) throws IOException {
        List<String> findFolder = findFolder((String) null, str);
        if (!findFolder.isEmpty()) {
            return findFolder.get(0);
        }
        return createFolderInRoot(str);
    }

    @NonNull
    public static DriveWriter createDriveWriter(@NonNull Context context) {
        GoogleSignInAccount signInAccount = new IprintGoogleSignIn().getSignInAccount(context);
        if (signInAccount != null) {
            GoogleAccountCredential usingOAuth2 = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(getWriteScope()));
            usingOAuth2.setSelectedAccount(signInAccount.getAccount());
            return new DriveWriter(new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), usingOAuth2).setApplicationName(GoogleDownloader.getDriveApplicationName(context)).build());
        }
        throw new IllegalStateException();
    }

    public static class ConvertMimeTypeException extends IOException {
        public ConvertMimeTypeException(String str) {
            super(str);
        }
    }
}
