package epson.common;

import android.content.Context;


import android.support.annotation.VisibleForTesting;
import epson.print.Util.EPLog;
import java.io.File;
import java.util.Locale;

public class ExternalFileUtils {
    private static String AREA_INFO = "area_info.dat";
    private static String EPSONIPRINT_DOWNLOAD_FOLDER = "download/";
    private static String INFO_FOLDER_IPPRINTER = "ipprinters/";
    private static String INFO_FOLDER_REMOTEPRINTER = "printers/";
    private static String PREVIEW_FOLDER = "tempPDF";
    private static String PRE_AREA_INFO = "pre_area_info.dat";
    private static String PRE_SUPPORTED_MEDIA = "pre_supported_media.dat";
    private static String PRINT_FOLDER = "print";
    private static String PRINT_TEMP_FOLDER = "temp";
    private static String SAVE_AREA_INFO = "save_area_info.dat";
    private static String SAVE_SUPPORTED_MEDIA = "save_supported_media.dat";
    private static String SCAN_SCANNED_IMAGE_PATH = "tempScan/";
    private static final String SHARED = "shared";
    private static String SUPPORTED_MEDIA = "supported_media.dat";
    private static final String TAG = "ExternalFileUtils";
    private static String TEMP_CR_FOLDER = "tempCR";
    private static String TEMP_VIEW_FOLDER = "tempView";
    private static ExternalFileUtils externalFileUtils;
    private Context mContext;

    public enum TempCacheDirectory {
        MEMORY_CARD_ACCESS("tempMemcard"),
        PHOTO_FILE_CONVERT("tempPhotoFConv"),
        GOOGLE_DRIVE_CONVERT("tempGConvert");
        
        String relativeDirectory;

        private TempCacheDirectory(String str) {
            relativeDirectory = str;
        }

        public String getRelativeDirectory() {
            return relativeDirectory;
        }
    }

    private ExternalFileUtils(Context context) {
        mContext = context;
    }

    public static ExternalFileUtils getInstance(Context context) {
        ExternalFileUtils externalFileUtils2 = externalFileUtils;
        if (externalFileUtils2 != null) {
            return externalFileUtils2;
        }
        return new ExternalFileUtils(context);
    }

    public String getFilesDir() {
        File externalFilesDir = mContext.getExternalFilesDir((String) null);
        if (externalFilesDir != null) {
            return externalFilesDir.getPath();
        }
        return mContext.getFilesDir().getPath();
    }

    public String getCacheDir() {
        File externalCacheDir = mContext.getExternalCacheDir();
        if (externalCacheDir != null) {
            return externalCacheDir.getPath();
        }
        return mContext.getCacheDir().getPath();
    }

    public boolean createTempFolder(String str) {
        File file = new File(str);
        if (file.exists()) {
            return false;
        }
        EPLog.m313i(TAG, "createTempFolder = " + str);
        return file.mkdirs();
    }

    public void clearTempFoler(String str) {
        File file = new File(str);
        File[] listFiles = file.listFiles();
        if (listFiles != null) {
            for (int i = 0; i < listFiles.length; i++) {
                if (listFiles[i].isFile()) {
                    EPLog.m313i(TAG, "clearTempFoler delete() = " + listFiles[i].getPath());
                    listFiles[i].delete();
                } else if (listFiles[i].isDirectory()) {
                    clearTempFoler(listFiles[i].toString());
                }
            }
        }
        if (file.exists() && file.isFile()) {
            EPLog.m313i(TAG, "clearTempFoler delete() = " + file.getPath());
            file.delete();
        }
    }

    public String getSupportedMediaDir() {
        return getFilesDir();
    }

    public File getSupportedMedia() {
        return new File(getSupportedMediaDir(), SUPPORTED_MEDIA);
    }

    public File getSavedSupportedMedia() {
        return new File(getSupportedMediaDir(), SAVE_SUPPORTED_MEDIA);
    }

    public File getPreSupportedMedia() {
        return new File(getSupportedMediaDir(), PRE_SUPPORTED_MEDIA);
    }

    public File getAreaInfo() {
        return new File(getSupportedMediaDir(), AREA_INFO);
    }

    public File getSavedAreaInfo() {
        return new File(getSupportedMediaDir(), SAVE_AREA_INFO);
    }

    public File getPreAreaInfo() {
        return new File(getSupportedMediaDir(), PRE_AREA_INFO);
    }

    public void removeAreaInfo() {
        File areaInfo = getAreaInfo();
        if (areaInfo.exists()) {
            areaInfo.delete();
        }
        File savedAreaInfo = getSavedAreaInfo();
        if (savedAreaInfo.exists()) {
            savedAreaInfo.delete();
        }
    }

    public String getWorkingDir() {
        return new File(getFilesDir()).getParent();
    }

    public String getRemotePrintersInfo() {
        return new File(getWorkingDir(), INFO_FOLDER_REMOTEPRINTER).getPath();
    }

    public String getIpPrintersInfo() {
        return new File(getWorkingDir(), INFO_FOLDER_IPPRINTER).getPath();
    }

    public void clearRemotePrintersInfo() {
        clearTempFoler(getRemotePrintersInfo());
    }

    public void clearIpPrintersInfo() {
        clearTempFoler(getIpPrintersInfo());
    }

    public String getDownloadDir() {
        return new File(getCacheDir(), EPSONIPRINT_DOWNLOAD_FOLDER).getPath();
    }

    public void initDownloadDir() {
        createTempFolder(getDownloadDir());
        clearTempFoler(getDownloadDir());
    }

    public String getPrintDir() {
        return new File(getCacheDir(), PRINT_FOLDER).getPath();
    }

    public String getPrintTmpDir() {
        return new File(getPrintDir(), PRINT_TEMP_FOLDER).getPath();
    }

    public void initPrintDir() {
        createTempFolder(getPrintDir());
        createTempFolder(getPrintTmpDir());
        clearTempFoler(getPrintDir());
    }

    public String getTempViewDir() {
        return new File(getCacheDir(), TEMP_VIEW_FOLDER).getPath();
    }

    public void initTempViewDir() {
        createTempFolder(getTempViewDir());
        clearTempFoler(getTempViewDir());
    }

    public String getTempCRDir() {
        return new File(getCacheDir(), TEMP_CR_FOLDER).getPath();
    }

    public void initTempCRDir() {
        createTempFolder(getTempCRDir());
        clearTempFoler(getTempCRDir());
    }

    public String getPdfDir() {
        return new File(getCacheDir(), PREVIEW_FOLDER).getPath();
    }

    public void initPdfDir() {
        createTempFolder(getPdfDir());
        clearTempFoler(getPdfDir());
    }

    public String getScannedImageDir() {
        return new File(getCacheDir(), SCAN_SCANNED_IMAGE_PATH).getPath();
    }

    public void initScannedImageDir() {
        createTempFolder(getScannedImageDir());
        clearTempFoler(getScannedImageDir());
    }

    public String getTempSharedDir() {
        return new File(getCacheDir(), SHARED).getPath();
    }

    public void initTempSharedDir() {
        createTempFolder(getTempSharedDir());
        clearTempFoler(getTempSharedDir());
    }

    @NonNull
    public File getTempCacheDir(@NonNull TempCacheDirectory tempCacheDirectory) {
        return new File(getCacheDir(), tempCacheDirectory.getRelativeDirectory());
    }

    @NonNull
    public File initTempCacheDirectory(@NonNull TempCacheDirectory tempCacheDirectory) {
        File tempCacheDir = getTempCacheDir(tempCacheDirectory);
        String path = tempCacheDir.getPath();
        createTempFolder(path);
        clearTempFoler(path);
        return tempCacheDir;
    }

    @Nullable
    public static String getNotDuplicateFilename(@NonNull String str, String str2, int i) {
        if (str2 == null) {
            return null;
        }
        int i2 = 0;
        do {
            String convertFilename = getConvertFilename(str, i2, str2);
            if (!new File(convertFilename).exists()) {
                return convertFilename;
            }
            i2++;
        } while (i2 < i);
        return null;
    }

    @VisibleForTesting
    public static String getConvertFilename(@NonNull String str, int i, @NonNull String str2) {
        return new File(str2, String.format(Locale.US, "%s_%02x.jpg", new Object[]{new File(str).getName(), Integer.valueOf(i)})).getPath();
    }
}
