package com.qmth.themis.business.config;

import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.UploadFileEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

/**
 * @Description: 读取系统配置文件
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/22
 */
@Component
public class SystemConfig {
    private final static Logger log = LoggerFactory.getLogger(SystemConfig.class);
    private Properties props = null;

    public SystemConfig() {
        try {
            Resource resource = new ClassPathResource("/application.properties");
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            log.error("请求出错", e);
        }
    }

    /**
     * 获取属性
     *
     * @param key
     * @return
     */
    public String getProperty(String key) {
        return Objects.isNull(props) ? null : props.getProperty(key);
    }

    /**
     * 获取属性
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getProperty(String key, String defaultValue) {
        return Objects.isNull(props) ? null : props.getProperty(key, defaultValue);
    }

    /**
     * 获取properyies属性
     *
     * @return
     */
    public Properties getProperties() {
        return props;
    }

    /**
     * 获取oss环境
     *
     * @param type
     * @return
     */
    public Map<String, Object> getOssEnv(Integer type) {
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put(SystemConstant.END_POINT, props.getProperty("aliyun.oss.endpoint", "aliyun.oss.name"));
        mapParameter.put(SystemConstant.ACCESS_KEY_ID, props.getProperty("aliyun.oss.accessKeyId"));
        mapParameter.put(SystemConstant.ACCESS_KEY_SECRET, props.getProperty("aliyun.oss.accessKeySecret"));
        mapParameter.put(SystemConstant.BUCKET, props.getProperty("aliyun.oss.bucket"));
        mapParameter.put(SystemConstant.OSS, Boolean.valueOf(props.getProperty("sys.config.oss")));
        mapParameter.put(SystemConstant.ATTACHMENT_TYPE, Arrays.asList(props.getProperty("sys.config.attachmentType").split(",")));
        String uploadType = UploadFileEnum.convertToName(type);
        mapParameter.put(SystemConstant.UPLOAD_TYPE, uploadType);
        return mapParameter;
    }

    /**
     * 获取腾讯云环境
     *
     * @return
     */
    public Map<String, Object> getTencentYunEnv() {
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put(SystemConstant.TENCENT_APPID, props.getProperty("tencentyun.sdk.appId"));
        mapParameter.put(SystemConstant.TENCENT_KEY, props.getProperty("tencentyun.sdk.key"));
        return mapParameter;
    }
}
