package com.datarecovery.master.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.format.Formatter;


import com.atmob.common.runtime.ContextUtil;
import com.datarecovery.master.R;

import java.util.Locale;
import java.util.UUID;

public class FileUtil {

    private FileUtil() {

    }

    public static String formatShortBytes(long bytes) {
        Context context = ContextUtil.getContext();
        return Formatter.formatShortFileSize(context, bytes);
    }

    public static String getImageFileType(String fileName) {
        if (fileName == null) {
            return "IMG";
        }
        String[] split = fileName.split("\\.");
        if (split.length <= 1) {
            return "IMG";
        }
        return split[split.length - 1].toUpperCase(Locale.getDefault());
    }

    public static String isImageFile(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return null;
        }
        String[] imageExtensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".tiff", ".tif", ".ico", ".svg"};
        for (String extension : imageExtensions) {
            if (fileName.toLowerCase().endsWith(extension)) {
                return extension;
            }
        }
        return null;
    }

    public static String getExportImageFileName(String fileName) {
        String uuid = UUID.randomUUID().toString();
        String fileType = isImageFile(fileName);
        if (!TextUtils.isEmpty(fileType)) {
            return uuid + fileType;
        } else {
            return uuid + ".png";
        }
    }


    public static String getCreateFileName(String fileName, String defaultSuffix) {
        String uuid = UUID.randomUUID().toString();
        if (fileName == null) {
            return uuid + defaultSuffix;
        }
        String[] split = fileName.split("\\.");
        if (split.length <= 1) {
            return uuid + defaultSuffix;
        }
        return uuid + "." + split[split.length - 1];
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public static Drawable getFileRecoverIcon(int fileTypeId) {
        Context context = ContextUtil.getContext();
        if (fileTypeId == R.string.word) {
            return context.getDrawable(R.drawable.icon_file_word);
        } else if (fileTypeId == R.string.excel) {
            return context.getDrawable(R.drawable.icon_file_excel);
        } else if (fileTypeId == R.string.ppt) {
            return context.getDrawable(R.drawable.icon_file_ppt);
        } else if (fileTypeId == R.string.pdf) {
            return context.getDrawable(R.drawable.icon_file_pdf);
        }
        return null;
    }


}
