package com.bef.effectsdk;

import X.4N2;
import X.8Kc;
import X.8r0;
import X.8r1;
import android.content.Context;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EffectSDKUtils {
    public static List<String> assetFiles = ModelsList.list;
    public static Set<File> localFiles = new HashSet();
    public static Set<File> needRemoveFiles = new HashSet();

    public static native String nativeGetSdkVersion();

    public static String getSdkVersion() {
        MethodCollector.i(12812);
        String nativeGetSdkVersion = nativeGetSdkVersion();
        MethodCollector.o(12812);
        return nativeGetSdkVersion;
    }

    static {
        Covode.recordClassIndex(2779);
    }

    public static void deleteNoUseModel() {
        for (File next : localFiles) {
            if (needRemoveFiles.contains(next) && next.exists()) {
                INVOKEVIRTUAL_com_bef_effectsdk_EffectSDKUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(next);
            }
        }
    }

    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception unused) {
            }
        }
    }

    public static String getAssetRelativePath(String str) {
        int indexOf = str.indexOf("model/");
        if (indexOf >= 0) {
            return str.substring(indexOf + 6, str.length());
        }
        return str;
    }

    public static String getFileName(String str) {
        int lastIndexOf = str.lastIndexOf("/");
        if (lastIndexOf != -1) {
            return str.substring(lastIndexOf + 1, str.length());
        }
        return "";
    }

    public static boolean INVOKEVIRTUAL_com_bef_effectsdk_EffectSDKUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        8r1 r3;
        MethodCollector.i(9015);
        try {
            if (8Kc.LIZIZ()) {
                r3 = (8r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", 8r1.class, 4N2.LIZ);
            } else {
                r3 = 4N2.LIZ;
            }
            if (8r0.LIZ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_delete_log", 8r0.LIZ(r3));
            }
            if (8r0.LIZJ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_handle", 8r0.LIZ(r3));
                MethodCollector.o(9015);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(9015);
        return delete;
    }

    public static void flushAlgorithmModelFiles(Context context, String str) {
        if (!localFiles.isEmpty()) {
            localFiles.clear();
        }
        scanRecursive(str, localFiles);
        copyAssets(context, str, (String[]) null, false);
        deleteNoUseModel();
        localFiles.clear();
    }

    public static File takeFirstMatchingOrNull(Set<File> set, FileFilter fileFilter) {
        for (File next : set) {
            if (fileFilter.accept(next)) {
                return next;
            }
        }
        return null;
    }

    public static void scanRecursive(String str, Set<File> set) {
        File[] listFiles;
        File file = new File(str);
        if (file.exists() && (listFiles = file.listFiles()) != null) {
            for (File file2 : listFiles) {
                if (file2.isDirectory()) {
                    scanRecursive(file2.getAbsolutePath(), set);
                } else {
                    set.add(file2);
                }
            }
        }
    }

    public static boolean needUpdate(final Context context, String str) {
        if (!localFiles.isEmpty()) {
            localFiles.clear();
        }
        scanRecursive(str, localFiles);
        try {
            if (assetFiles.size() > localFiles.size()) {
                return true;
            }
            for (final String next : assetFiles) {
                if (takeFirstMatchingOrNull(localFiles, new FileFilter() {
                    static {
                        Covode.recordClassIndex(2780);
                    }

                    public final boolean accept(File file) {
                        MethodCollector.i(14524);
                        boolean z = false;
                        if (next.contains(file.getName())) {
                            InputStream inputStream = null;
                            try {
                                inputStream = context.getAssets().open(next);
                                if (file.length() == ((long) inputStream.available())) {
                                    z = true;
                                }
                                return z;
                            } catch (IOException unused) {
                                return false;
                            } finally {
                                EffectSDKUtils.closeQuietly(inputStream);
                                MethodCollector.o(14524);
                            }
                        } else {
                            MethodCollector.o(14524);
                            return false;
                        }
                    }
                }) == null) {
                    return true;
                }
            }
            return false;
        } catch (Throwable unused) {
            return true;
        }
    }

    public static void copyFile(Context context, String str, String str2) {
        FileOutputStream fileOutputStream;
        MethodCollector.i(8847);
        InputStream inputStream = null;
        try {
            InputStream open = context.getAssets().open(str);
            try {
                String str3 = str2 + str.substring(str.indexOf("model") + 6, str.lastIndexOf("/"));
                File file = new File(str3);
                if (file.exists() || file.mkdirs()) {
                    fileOutputStream = new FileOutputStream(new File(str3 + "/" + getFileName(str)));
                    try {
                        byte[] bArr = new byte[1024];
                        while (true) {
                            int read = open.read(bArr);
                            if (read > 0) {
                                fileOutputStream.write(bArr, 0, read);
                            } else {
                                try {
                                    closeQuietly(open);
                                    return;
                                } finally {
                                    closeQuietly(fileOutputStream);
                                    MethodCollector.o(8847);
                                }
                            }
                        }
                    } catch (Throwable th) {
                        th = th;
                        inputStream = open;
                        try {
                            closeQuietly(inputStream);
                            throw th;
                        } finally {
                            closeQuietly(fileOutputStream);
                            MethodCollector.o(8847);
                        }
                    }
                } else {
                    IOException iOException = new IOException("Can not mkdirs " + file.getPath());
                    MethodCollector.o(8847);
                    throw iOException;
                }
            } catch (Throwable th2) {
                th = th2;
                fileOutputStream = null;
                inputStream = open;
                closeQuietly(inputStream);
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            fileOutputStream = null;
            closeQuietly(inputStream);
            throw th;
        }
    }

    public static void flushAlgorithmModelFiles(Context context, String str, String[] strArr, boolean z) {
        if (!localFiles.isEmpty()) {
            localFiles.clear();
        }
        scanRecursive(str, localFiles);
        copyAssets(context, str, strArr, z);
        deleteNoUseModel();
        localFiles.clear();
    }

    public static void copyAssets(Context context, String str, String[] strArr, boolean z) {
        if (!needRemoveFiles.isEmpty()) {
            needRemoveFiles.clear();
        }
        needRemoveFiles.addAll(localFiles);
        if (!str.endsWith("/")) {
            str = str + "/";
        }
        for (String next : assetFiles) {
            final String fileName = getFileName(next);
            File takeFirstMatchingOrNull = takeFirstMatchingOrNull(needRemoveFiles, new FileFilter() {
                static {
                    Covode.recordClassIndex(2781);
                }

                public final boolean accept(File file) {
                    return file.getName().contains(fileName);
                }
            });
            if (takeFirstMatchingOrNull == null || !new File(str, getAssetRelativePath(next)).exists()) {
                if (strArr != null && !TextUtils.isEmpty(fileName)) {
                    int length = strArr.length;
                    int i = 0;
                    while (true) {
                        if (i >= length) {
                            break;
                        } else if (!fileName.equals(strArr[i])) {
                            i++;
                        } else if (z) {
                            copyFile(context, next, str);
                        }
                    }
                }
                if (!z) {
                    copyFile(context, next, str);
                }
            } else {
                needRemoveFiles.remove(takeFirstMatchingOrNull);
            }
        }
    }
}
