package com.cpm.lorealbaautomation.utils;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class AppUtils {
    public static File getInternalDirectory(Context context, String folderName) {
        File dir = context.getExternalFilesDir("");
        File file = new File(dir, folderName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public static File getFile(Context context, String fileName, String folderName) {
        File dir = getInternalDirectory(context, folderName);
        if (fileName == null || fileName.isEmpty()) {
            return dir;
        } else {
            return new File(dir, fileName);
        }
    }

    public static void saveByteArrayToFile(byte[] byteArray, String fileName) throws IOException {
        try (FileOutputStream fileOutputStream = new FileOutputStream(fileName)) {
            fileOutputStream.write(byteArray);
        }
    }
    public static void clearMyFiles(Context activity, String folderName) {
        File[] files = getInternalDirectory(activity, folderName).listFiles();
        if (files != null) {
            for (File file : files) {
                file.delete();
            }
        }
    }

    public static File getExterDir(Context context) {
        return context.getExternalFilesDir("");
    }

}
