package cn.stylefeng.team.project.modular.filing.factory;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.file.api.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.api.enums.FileLocationEnum;
import cn.stylefeng.roses.kernel.file.api.enums.FileStatusEnum;
import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.file.modular.entity.SysFileInfo;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.math.BigDecimal;

import static cn.stylefeng.roses.kernel.file.api.constants.FileConstants.FILE_POSTFIX_SEPARATOR;

public class ByteFileInfoFactory {

    public static SysFileInfo createPngFileInfo(byte[] bytes, SysFileInfo excelFileInfo) {

        SysFileInfo sysFileInfo = new SysFileInfo();

        fillCommonInfo(sysFileInfo, excelFileInfo);

        String excelReplacePng = excelFileInfo.getFileOriginName().replaceAll(".xlsx", ".png");
        excelReplacePng = excelReplacePng.replaceAll(".xls", ".png");
        sysFileInfo.setFileOriginName(excelReplacePng);

        String fileSuffix = null;
        if (ObjectUtil.isNotEmpty(sysFileInfo.getFileOriginName())) {
            fileSuffix = StrUtil.subAfter(sysFileInfo.getFileOriginName(), FILE_POSTFIX_SEPARATOR, true);
        }
        sysFileInfo.setFileSuffix(fileSuffix);

        long fileSizeKb = Convert.toLong(NumberUtil.div(new BigDecimal(bytes.length), BigDecimal.valueOf(1024)).setScale(0, BigDecimal.ROUND_HALF_UP));
        sysFileInfo.setFileSizeKb(fileSizeKb);

        String fileSizeInfo = FileUtil.readableFileSize(bytes.length);
        sysFileInfo.setFileSizeInfo(fileSizeInfo);

        String finalFileName = sysFileInfo.getFileId() + FILE_POSTFIX_SEPARATOR + fileSuffix;
        sysFileInfo.setFileObjectName(finalFileName);

        return sysFileInfo;
    }

    private static void fillCommonInfo(SysFileInfo sysFileInfo, SysFileInfo excelFileInfo) {

        Long fileId = IdWorker.getId();
        sysFileInfo.setFileId(fileId);

        sysFileInfo.setFileCode(IdWorker.getId());

        sysFileInfo.setFileVersion(1);

        sysFileInfo.setFileStatus(FileStatusEnum.NEW.getCode());

        if (FileLocationEnum.DB.getCode().equals(excelFileInfo.getFileLocation())) {
            sysFileInfo.setFileLocation(FileLocationEnum.DB.getCode());
        } else {
            FileOperatorApi fileOperatorApi = SpringUtil.getBean(FileOperatorApi.class);
            sysFileInfo.setFileLocation(fileOperatorApi.getFileLocationEnum().getCode());
        }

        String fileBucket = FileConfigExpander.getDefaultBucket();
        if (StrUtil.isNotEmpty(excelFileInfo.getFileBucket())) {
            fileBucket = excelFileInfo.getFileBucket();
        }
        sysFileInfo.setFileBucket(fileBucket);

        sysFileInfo.setSecretFlag(excelFileInfo.getSecretFlag());
    }

}
