package epson.print;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.content.Context;
import android.os.AsyncTask;



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import epson.common.ExternalFileUtils;
import epson.common.ImageUtil;
import epson.print.phlayout.PhotoPreviewImageList;

public class PhotoImageConvertViewModel extends AndroidViewModel {
    private final Application mApplication;
    private final FilenameListLiveData mImageFilenameLibData = new FilenameListLiveData();

    public PhotoImageConvertViewModel(@NonNull Application application) {
        super(application);
        mApplication = application;
    }

    /* access modifiers changed from: private */
    @NonNull
    public static String initTempDirectory(@NonNull Context context) {
        return ExternalFileUtils.getInstance(context).initTempCacheDirectory(ExternalFileUtils.TempCacheDirectory.PHOTO_FILE_CONVERT).getPath();
    }

    public static void clearTempDirectory(@NonNull Context context) {
        initTempDirectory(context);
    }

    public FilenameListLiveData getData() {
        return mImageFilenameLibData;
    }

    public void setOriginalImageList(ArrayList<String> arrayList) {
        mImageFilenameLibData.setOriginalList(arrayList, mApplication);
    }

    public static class Result {
        @Nullable
        public ArrayList<PhotoPreviewImageList.EpImageWrapper> epImageWrappers;

        public Result(@Nullable ArrayList<PhotoPreviewImageList.EpImageWrapper> arrayList) {
            epImageWrappers = arrayList;
        }
    }

    public static class FilenameListLiveData extends LiveData<Result> {
        public void setOriginalList(final ArrayList<String> arrayList, @NonNull final Application application) {
            new AsyncTask<Void, Void, Result>() {
                protected Result doInBackground(Void... voidArr) {
                    ArrayList access$000 = FilenameListLiveData.this.convertHeifImageList(arrayList, application);
                    if (!FilenameListLiveData.this.checkEpImage(access$000)) {
                        return new Result((ArrayList<PhotoPreviewImageList.EpImageWrapper>) null);
                    }
                    return new Result(access$000);
                }

                protected void onPostExecute(Result result) {
                    FilenameListLiveData.this.setValue(result);
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }

        private boolean checkFilePath(ArrayList<String> arrayList, String str) {
            if (str == null) {
                return true;
            }
            try {
                String lowerCase = new File(str).getCanonicalPath().toLowerCase();
                int i = 0;
                while (i < arrayList.size()) {
                    String str2 = arrayList.get(i);
                    if (str2 == null) {
                        return false;
                    }
                    try {
                        if (new File(str2).getCanonicalPath().toLowerCase().startsWith(lowerCase)) {
                            return false;
                        }
                        i++;
                    } catch (IOException unused) {
                        return false;
                    }
                }
                return true;
            } catch (IOException unused2) {
                return false;
            }
        }

        /* access modifiers changed from: private */
        /* JADX WARNING: Removed duplicated region for block: B:6:0x000e  */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public boolean checkEpImage(@android.support.annotation.Nullable ArrayList<epson.print.phlayout.PhotoPreviewImageList.EpImageWrapper> r4) {
            /*
                r3 = this;
                r0 = 0
                if (r4 != 0) goto L_0x0004
                return r0
            L_0x0004:
                java.util.Iterator r4 = r4.iterator()
            L_0x0008:
                boolean r1 = r4.hasNext()
                if (r1 == 0) goto L_0x0026
                java.lang.Object r1 = r4.next()
                epson.print.phlayout.PhotoPreviewImageList$EpImageWrapper r1 = (epson.print.phlayout.PhotoPreviewImageList.EpImageWrapper) r1
                epson.print.EPImage r2 = r1.epImage
                boolean r2 = epson.print.ActivityViewImageSelect.checkEPImage(r2)
                if (r2 != 0) goto L_0x001d
                return r0
            L_0x001d:
                epson.print.EPImage r1 = r1.epImage
                boolean r1 = epson.print.ActivityViewImageSelect.checkBmpNot24Bit(r1)
                if (r1 == 0) goto L_0x0008
                return r0
            L_0x0026:
                r4 = 1
                return r4
            */
            throw new UnsupportedOperationException("Method not decompiled: epson.print.PhotoImageConvertViewModel.FilenameListLiveData.checkEpImage(java.util.ArrayList):boolean");
        }

        /* access modifiers changed from: private */
        @Nullable
        public ArrayList<PhotoPreviewImageList.EpImageWrapper> convertHeifImageList(@Nullable ArrayList<String> arrayList, @NonNull Application application) {
            ArrayList<PhotoPreviewImageList.EpImageWrapper> arrayList2 = new ArrayList<>();
            if (arrayList == null) {
                return arrayList2;
            }
            String access$300 = PhotoImageConvertViewModel.initTempDirectory(application);
            for (int i = 0; i < arrayList.size(); i++) {
                String str = arrayList.get(i);
                if (str != null) {
                    if (ImageUtil.isHEIFFormat(str)) {
                        String notDuplicateFilename = ExternalFileUtils.getNotDuplicateFilename(str, access$300, 30);
                        if (notDuplicateFilename == null || !ImageUtil.convertHEIFtoJPEG(str, notDuplicateFilename)) {
                            return null;
                        }
                        arrayList2.add(new PhotoPreviewImageList.EpImageWrapper(notDuplicateFilename, str, i));
                    } else {
                        arrayList2.add(new PhotoPreviewImageList.EpImageWrapper(str, (String) null, i));
                    }
                }
            }
            return arrayList2;
        }
    }
}
