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 androidx.annotation.NonNull;

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

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

public class FileUtil {

    private FileUtil() {

    }

    private static final String IMAGE_PREFIX = "recover_img_";
    private static final String FILE_PREFIX = "recover_file_";
    private static final String AUDIO_PREFIX = "recover_audio_";
    private static final String VIDEO_PREFIX = "recover_video_";

    public static String getFilePath(String[] pathSegments) {
        if (pathSegments == null) {
            return null;
        }
        return String.join("%2F", pathSegments);
    }

    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 getCreateFileName(@MediaStoreHelper.MediaType int mediaType, String fileName) {
        if (mediaType == MediaStoreHelper.TYPE_IMAGE) {
            return getExportFileName(IMAGE_PREFIX, ".png", "");
        } else if (mediaType == MediaStoreHelper.TYPE_VIDEO) {
            return getExportFileName(VIDEO_PREFIX, ".mp4", fileName);
        } else if (mediaType == MediaStoreHelper.TYPE_AUDIO) {
            return getExportFileName(AUDIO_PREFIX, ".mp3", fileName);
        } else {
            return getExportFileName(FILE_PREFIX, "", fileName);
        }
    }

    public static String getExportFileName(@NonNull String prefix, String defaultSuffix, String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            return prefix + getNowTime() + defaultSuffix;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        sb.append(getNowTime());
        int lastDot = fileName.lastIndexOf(".");
        if (lastDot == -1) {
            sb.append(defaultSuffix);
        } else {
            sb.append(fileName.substring(lastDot));
        }
        return sb.toString();
    }

    private static String getNowTime() {
        return DateUtil.formatNormalDate("yyyyMMddHHmmssSSS", System.currentTimeMillis());
    }


    @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;
    }


}
