package com.yanzuoguang.cloud;

import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 整个模块的所有配置信息
 *
 * @author 颜佐光
 */
@Component
public class CloudConfig {

    public static final int DEFAULT_PRINT_TIME = 2 * 60 * 1000;

    @Value("${spring.application.name}")
    protected String applicationName;

    @Value("${yzg.logCommon:false}")
    protected boolean logCommon;

    @Value("${yzg.logAll:false}")
    protected boolean logAll = false;

    @Value("${yzg.log.base:false}")
    private boolean logBase;

    @Value("${yzg.cacheTime:120}")
    protected int cacheTime = 120;

    @Value("${yzg.reqSize:51200}")
    private int reqSize = 51200;
    /**
     * 1个请求最长时间(毫秒)
     */
    @Value("${yzg.log.time.max:30000}")
    private int logTimeMax;
    /**
     * 1个请求间隔记录时间(毫秒)
     */
    @Value("${yzg.log.time.split:60000}")
    private int logTimeSplit;
    /**
     * 日志不需要过滤的地址
     */
    @Value("${yzg.log.notFilter:^.*login.*$}")
    private String logNotFilter;

    /**
     * 日志需要过滤的地址
     */
    @Value("${yzg.log.filter:^.*(log|yzg).*$}")
    private String logFilter;

    /**
     * 服务器存储地址,注意权限
     */
    @Value("${yzg.upload.server:}")
    private String serverUrl;

    /**
     * 外网地址
     */
    @Value("${yzg.upload.display:}")
    private String displayUrl;

    @Value("${yzg.gateway:^.*gateway.*$}")
    private String gateWay;

    /**
     * 外网地址
     */
    @Value("${yzg.count.print.time:" + DEFAULT_PRINT_TIME + "}")
    private int logCountTime;

    public String getApplicationName() {
        return applicationName;
    }

    public boolean isLogCommon() {
        return logCommon;
    }

    public boolean isLogAll() {
        return logAll;
    }

    public boolean isLogBase() {
        return logBase;
    }

    public int getCacheTime() {
        return cacheTime;
    }

    public int getReqSize() {
        return reqSize;
    }

    public int getLogTimeMax() {
        return logTimeMax;
    }

    public int getLogTimeSplit() {
        return logTimeSplit;
    }

    public String getLogNotFilter() {
        return logNotFilter;
    }

    public String getLogFilter() {
        return logFilter;
    }

    public String getGateWay() {
        return gateWay;
    }

    public String getServerUrl() {
        if (StringHelper.isEmpty(this.serverUrl)) {
            throw YzgError.getRuntimeException("057");
        }
        return serverUrl;
    }

    public String getDisplayUrl() {
        if (StringHelper.isEmpty(this.serverUrl)) {
            throw YzgError.getRuntimeException("058");
        }
        return displayUrl;
    }


    /**
     * 获取行记的显示的图片
     *
     * @return
     */
    public String getTempFolder(String folder, Date date) {
        return String.format("%s/%s/%s/%s",
                folder,
                DateHelper.getDateTimeString("yyyy", date),
                DateHelper.getDateTimeString("MM", date),
                DateHelper.getDateTimeString("dd", date)
        );
    }

    /**
     * 是否属于网关服务,网关服务不进行监控
     *
     * @return
     */
    public boolean isGateWay() {
        return this.applicationName.toLowerCase().matches(this.gateWay);
    }

    public int getLogCountTime() {
        return logCountTime;
    }
}