package com.tzld.piaoquan.api.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Slf4j
public class PropertiesUtils {

    public static Properties properties;

    static {
        Properties mainProperties = new Properties();
        properties = new Properties();
        try {
            InputStream mainStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
            mainProperties.load(mainStream);
            // 先通过虚拟机参数 -Denv=value去取配置文件的值
            // 如果没有设置-Denv的虚拟机参数则说明是本地开发环境，则读取application.properties文件中spring.profiles.active的值
            String env = System.getProperty("env");
            if (StringUtils.isEmpty(env)) {
                env = mainProperties.getProperty("spring.profiles.active");
            }
            if (StringUtils.isEmpty(env)) {
                log.error("设置spring.profiles.active or 设置虚拟机启动参数 -Denv错误！！！");
                System.exit(1);
            }
            InputStream activeStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("application-" + env + ".properties");
            properties.load(activeStream);
            properties.put("spring.profiles.active", env);
            log.info("开发环境为: " + env);
        } catch (IOException e) {
            log.error(e.getMessage());
            System.exit(1);
        }
    }

    public static Properties getProperties() {
        return properties;
    }

    public static String getProjectEnv() {
        return getProperties().getProperty("spring.profiles.active");
    }

    public static String getValue(String key) {
        return PropertiesUtils.getProperties().getProperty(key);
    }


    public static boolean getSwaggerEnabled() {
        return Boolean.parseBoolean(getProperties().getProperty("swagger.enabled"));
    }

    public static String getSwaggerBasePath() {
        return getProperties().getProperty("swagger.basePath");
    }


    public static String getSwaggerProtocols() {
        return getProperties().getProperty("swagger.protocols");
    }


    public static String getVideoBucket() {
        return getProperties().getProperty("oss.longvideo.bucket");
    }

    public static String getDownloadDomain() {
        return getProperties().getProperty("oss.longvideo.cdnDomain");
    }

    public static String getTranscodeLocation() {
        return getProperties().getProperty("oss.longvideo.transcode.location");
    }

    public static String getTranscodePipelineId() {
        return getProperties().getProperty("oss.longvideo.transcode.PipelineId");
    }

    public static String getReadOnlyAccessKeyId() {
        return getProperties().getProperty("oss.video.readonly.accessKey");
    }

    public static String getReadOnlyAccessKeySecret() {
        return getProperties().getProperty("oss.video.readonly.secretKey");
    }

}
