package com.jeequan.jeepay.components.oss.model;

import com.jeequan.jeepay.components.oss.constant.OssSavePlaceEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 定义文件上传的配置信息
 *
 * @author terrfly
 * @date 2021/6/8 16:38
 */
@Data
@AllArgsConstructor
public class OssFileConfig {

    /** 用户头像 **/
    public interface BIZ_TYPE {
        String AVATAR = "avatar"; /** 用户头像 **/
        String IF_BG = "ifBG"; /** 接口类型卡片背景图片 **/
        String CERT = "cert";  /** 接口参数 **/
        String WX_FILE = "wxFile"; /** 微信相关文件 **/
        String APPLYMENT = "applyment";  /** 进件资料 **/
        String QRC = "qrc";  /** 码牌相关图片 **/
        String OEM = "oem";  /** OEM站点信息 **/
        String NOTICE = "notice";  /** 公告内的图片 **/
        String CHANNEL_BILL = "channelBill";  /** 渠道对账文件 **/
        String APP_FILE = "appFile";  /** 升级文件 **/
        String AGENT_FILE = "agentFile";  /** 代理授权文件 **/
        String AUDIO = "audio";  /** 音频文件 **/
        String MATERIAL = "material";  /** 素材文件 **/
    }

    /** 图片类型后缀格式 **/
    public static final Set<String> IMG_SUFFIX = new HashSet<>(Arrays.asList("jpg", "png", "jpeg", "gif", "ico", "svg", "bmp"));

    /** 证书相关文件格式 **/
    public static final Set<String> CERT_SUFFIX = new HashSet<>(Arrays.asList("crt", "p12", "pem", "pfx", "cer", "sm2", "key"));

    /** 站点文件格式 **/
    public static final Set<String> AGENT_FILE_SUFFIX = new HashSet<>(Arrays.asList("pdf", "docx" ,"xlsx"));

    public static final Set<String> AUDIO_SUFFIX = new HashSet<>(Arrays.asList("mp3", "wav"));

    public static final Set<String> MATERIAL_SUFFIX = new HashSet<>(Arrays.asList("jgp", "JFIF"));

    /** 全部后缀格式的文件标识符 **/
    public static final String ALL_SUFFIX_FLAG = "*";

    /** 不校验文件大小标识符 **/
    public static final Long ALL_MAX_SIZE = -1L;

    /** 允许上传的最大文件大小的默认值 **/
    public static final Long DEFAULT_MAX_SIZE = 5 * 1024 * 1024L;

    private static final Map<String, OssFileConfig> ALL_BIZ_TYPE_MAP = new HashMap<>();
    static{
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.AVATAR, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.IF_BG, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.APPLYMENT, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.CERT, new OssFileConfig(OssSavePlaceEnum.PRIVATE, CERT_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.WX_FILE, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.QRC, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.OEM, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.NOTICE, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.CHANNEL_BILL, new OssFileConfig(OssSavePlaceEnum.PUBLIC, new HashSet<>(Collections.singletonList(ALL_SUFFIX_FLAG)), DEFAULT_MAX_SIZE) );

        // appFile , 默认大小50M
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.APP_FILE, new OssFileConfig(OssSavePlaceEnum.PUBLIC, new HashSet<>(Collections.singletonList("apk")), 50 * 1024 * 1024L) );

        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.AGENT_FILE, new OssFileConfig(OssSavePlaceEnum.PUBLIC, AGENT_FILE_SUFFIX, DEFAULT_MAX_SIZE) );
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.AUDIO, new OssFileConfig(OssSavePlaceEnum.PUBLIC, AUDIO_SUFFIX, DEFAULT_MAX_SIZE) );
        //素材文件
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.MATERIAL, new OssFileConfig(OssSavePlaceEnum.PUBLIC, MATERIAL_SUFFIX, DEFAULT_MAX_SIZE) );
    }

    /** 存储位置 **/
    private OssSavePlaceEnum ossSavePlaceEnum;

    /** 允许的文件后缀, 默认全部类型 **/
    private Set<String> allowFileSuffix = new HashSet<>(Arrays.asList(ALL_SUFFIX_FLAG));

    /** 允许的文件大小, 单位： Byte **/
    private Long maxSize = DEFAULT_MAX_SIZE;


    /** 是否在允许的文件类型后缀内 **/
    public boolean isAllowFileSuffix(String fixSuffix){

        if(this.allowFileSuffix.contains(ALL_SUFFIX_FLAG)){ //允许全部
            return true;
        }

        return this.allowFileSuffix.contains(StringUtils.defaultIfEmpty(fixSuffix, "").toLowerCase());
    }

    /** 是否在允许的大小范围内 **/
    public boolean isMaxSizeLimit(Long fileSize){

        if(ALL_MAX_SIZE.equals(this.maxSize)){ //允许全部大小
            return true;
        }

        return this.maxSize >= ( fileSize == null ? 0L : fileSize);
    }


    public static OssFileConfig getOssFileConfigByBizType(String bizType){
        return ALL_BIZ_TYPE_MAP.get(bizType);
    }

}
