package com.fdkankan.modeling.constants;

import cn.hutool.core.util.StrUtil;
import com.fdkankan.common.util.FileUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class SysConstants {
    private static final Logger log = LoggerFactory.getLogger(SysConstants.class);

    public static String hostName;
    public static int modelTimeOut;

    public static List<String> residenceServiceHostName = new ArrayList<>();

    public static volatile Boolean SYSTEM_BUILDING = false;
    public static volatile Boolean SYSTEM_OFFING = false;

    public static ExecutorService executorService = Executors.newFixedThreadPool(1);

    public static final String DINGTALK_MSG_PATTERN = "**环境**: %s\n\n" +
            "**服务器名称**: %s\n\n" +
            "**失败原因**: %s\n\n" +
            "**num**: %s\n\n" +
            "**server-path**: %s\n\n" +
            "**algorithm-log**: [https://4dkk.4dage.com/build_log/%s/console.log](https://4dkk.4dage.com/build_log/%s/console.log)";

    @Value("${hostName.filePath:/opt/hosts/hosts.txt}")
    public void setHostName(String filePath){
        try {
            SysConstants.hostName = FileUtils.readFile(filePath);
            //去除换行符
            if(StrUtil.isNotEmpty(SysConstants.hostName)){
                SysConstants.hostName = SysConstants.hostName.trim().replaceAll("\\s","");
            }
            log.info("从文件({})中获取服务器名称:{}", filePath,hostName);
        } catch (Exception e) {
            log.error("从文件中获取服务器名称失败,文件路径{}", filePath);
            e.printStackTrace();
        }
    }

    /**
     * 默认超时时间2天
     * @param timeOut
     */
    @Value("${model.timeOut:48}")
    public void setModelTimeOut(int timeOut){
        SysConstants.modelTimeOut = timeOut;
    }

    /**
     * 常驻服务实例ID集合
     */
    @Value("${residenceService.hostNames:#{null}}")
    public void setResidenceServiceHostName(String[] hostNames){
        if(!ObjectUtils.isEmpty(hostNames)){
            SysConstants.residenceServiceHostName = Arrays.asList(hostNames);
        }
    }
}
