package ink.wgink.module.file.components;

import ink.wgink.exceptions.FileException;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.module.file.enums.UploadTypeEnum;
import ink.wgink.properties.FileProperties;
import ink.wgink.util.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @ClassName: FileUtil
 * @Description: 文件工具
 * @Author: wanggeng
 * @Date: 2022/7/25 17:00
 * @Version: 1.0
 */
@Component
public class FileComponent {

    protected static final Logger LOG = LoggerFactory.getLogger(FileComponent.class);
    private String[] imageTypes = null;
    private String[] videoTypes = null;
    private String[] audioTypes = null;
    private String[] fileTypes = null;
    @Autowired
    private FileProperties fileProperties;


    /**
     * 获取上传绝对文件
     *
     * @param uploadTypeEnum
     * @param fileType
     * @return
     * @throws FileException
     */
    public String getUploadPath(UploadTypeEnum uploadTypeEnum, String fileType) throws FileException {
        String baseUploadPath = fileProperties.getUploadPath();
        if (StringUtils.isBlank(baseUploadPath)) {
            throw new SystemException("上传路径未配置");
        }
        StringBuilder filePath = new StringBuilder();
        if (!baseUploadPath.endsWith(File.separator)) {
            filePath.append(baseUploadPath).append(File.separator);
        } else {
            filePath.append(baseUploadPath);
        }
        boolean hasFileType = !StringUtils.isBlank(fileType);
        if (uploadTypeEnum.getValue() == UploadTypeEnum.IMAGE.getValue()) {
            if (hasFileType && !isTypeCorrect(getImageTypes(), fileType)) {
                throw new FileException("图片格式不支持上传");
            }
            filePath.append("images");
        } else if (uploadTypeEnum.getValue() == UploadTypeEnum.VIDEO.getValue()) {
            if (hasFileType && !isTypeCorrect(getVideoTypes(), fileType)) {
                throw new FileException("视频格式不支持上传");
            }
            filePath.append("videos");
        } else if (uploadTypeEnum.getValue() == UploadTypeEnum.AUDIO.getValue()) {
            if (hasFileType && !isTypeCorrect(getAudioTypes(), fileType)) {
                throw new FileException("音频格式不支持上传");
            }
            filePath.append("audios");
        } else if (uploadTypeEnum.getValue() == UploadTypeEnum.ERROR_EXCEL.getValue()) {
            filePath.append("errorexcels");
        } else {
            if (hasFileType && !isTypeCorrect(getFileTypes(), fileType)) {
                throw new FileException("文件格式不支持上传");
            }
            filePath.append("files");
        }
        filePath.append(File.separator).append(DateUtil.getDays());
        return filePath.toString();
    }

    /**
     * 获取文件url
     *
     * @param uploadPath
     * @return
     */
    public String getFileUrl(String uploadPath) {
        String fileUrl = uploadPath.replace(fileProperties.getUploadPath(), "");
        if ("\\".equals(File.separator)) {
            fileUrl = fileUrl.replace("\\", "/");
        }
        if (fileUrl.startsWith("/")) {
            fileUrl = fileUrl.substring(1, fileUrl.length() - 1);
        }
        return "files/" + fileUrl;
    }

    /**
     * 获取文件类型
     *
     * @param fileName
     * @return
     */
    public String getFileType(String fileName) {
        String[] names = fileName.split("\\.");
        if (names != null) {
            return names[names.length - 1].toLowerCase();
        }
        return "";
    }

    /**
     * 校验类型
     *
     * @param types
     * @param fileType
     * @return
     */
    public boolean isTypeCorrect(String[] types, String fileType) {
        for (String type : types) {
            if (StringUtils.equalsIgnoreCase(fileType, type)) {
                return true;
            }
        }
        return false;
    }

    public String[] getImageTypes() {
        return imageTypes == null ? fileProperties.getImageTypes().split(",") : imageTypes;
    }

    public String[] getVideoTypes() {
        return videoTypes == null ? fileProperties.getVideoTypes().split(",") : videoTypes;
    }

    public String[] getAudioTypes() {
        return audioTypes == null ? fileProperties.getAudioTypes().split(",") : audioTypes;
    }

    public String[] getFileTypes() {
        return fileTypes == null ? fileProperties.getFileTypes().split(",") : fileTypes;
    }

}
