package com.zcxk.core.utils.log;

import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Andy
 * @description: 全局日志类
 * @date 2021/7/16
 * @version V1.0
 **/
public final class GwsLogger {

    public static final String GWS_LOG = "GWS";
    public static final String CON_QUOTE = "`";

    /**
     * 定义基础的Log日志
     */
    private final static Logger gwslog = LoggerFactory.getLogger(GWS_LOG);

    private static ConcurrentHashMap<GwsLoggerTypeEnum, Logger> logMaps = new ConcurrentHashMap<>();

    /**
     * 全局日志logger调用
     */
    public static Logger getLogger(GwsLoggerTypeEnum loggerType) {
        Logger logger = logMaps.get(loggerType);
        if (logger == null) {
            logger = LoggerFactory.getLogger(loggerType.name());
            // 发现未定义的日志发现未创建，则自动创建，只需在枚举类创建即可
            logMaps.put(loggerType, logger);
        }
        return logger;
    }

    /**
     * 全局日志默认GWS枚举
     */
    public static Logger getLogger() {
        return logMaps.get(GwsLoggerTypeEnum.GWS);
    }

    /**
     * 输出已定义日志类别的info日志
     */
    public static void info(GwsLoggerTypeEnum loggerType, String msg, Object... args) {
        try {
            Logger logger = getLogger(loggerType);
            msg = addImportantToLog(msg);
            logger.info(msg, args);
        } catch (Exception e) {
        }
    }

    /**
     * 输出已定义日志类别的info日志
     */
    public static void info(GwsLoggerTypeEnum loggerType, Throwable throwable, String msg,
        Object... args) {
        Logger logger = getLogger(loggerType);
        FormattedMessage message = new FormattedMessage(msg, args);
        String formattedMessage = message.getFormattedMessage();
        String importFormattedMessage = addImportantToLog(formattedMessage);
        logger.info(importFormattedMessage, throwable);
    }

    /**
     * 输出默认的GWS类别info日志
     */
    public static void info(String msg, Object... args) {
        Logger logger = getLogger(GwsLoggerTypeEnum.GWS);
        msg = addImportantToLog(msg);
        logger.info(msg, args);
    }

    /**
     * 输出默认的GWS类别info日志
     */
    public static void info(Throwable throwable, String msg, Object... args) {
        Logger logger = getLogger();
        FormattedMessage message = new FormattedMessage(msg, args);
        String formattedMessage = message.getFormattedMessage();
        String importFormattedMessage = addImportantToLog(formattedMessage);
        logger.info(importFormattedMessage, throwable);
    }

    /**
     * 输出已定义日志类别的info日志
     */
    public static void debug(GwsLoggerTypeEnum loggerType, String msg, Object... args) {
        try {
            Logger logger = getLogger(loggerType);
            msg = addImportantToLog(msg);
            logger.debug(msg, args);
        } catch (Exception e) {
        }
    }

    /**
     * 输出默认的GWS类别debug日志
     */
    public static void debug(String msg, Object... args) {
        Logger logger = getLogger();
        msg = addImportantToLog(msg);
        logger.debug(msg, args);
    }

    /**
     * 输出默认GWS日志类别的debug日志
     */
    public static void debug(Throwable throwable, String msg, Object... args) {
        Logger logger = getLogger();
        FormattedMessage message = new FormattedMessage(msg, args);
        String formattedMessage = message.getFormattedMessage();
        String importFormattedMessage = addImportantToLog(formattedMessage);
        logger.debug(importFormattedMessage, throwable);
    }

    /**
     * 输出已定义日志类别的info日志
     */
    public static void warn(GwsLoggerTypeEnum loggerType, String msg, Object... args) {
        try {
            Logger logger = getLogger(loggerType);
            msg = addImportantToLog(msg);
            logger.warn(msg, args);
        } catch (Exception e) {
        }
    }

    /**
     * 输出默认的GWS类别warn日志
     */
    public static void warn(String msg, Object... args) {
        Logger logger = getLogger();
        msg = addImportantToLog(msg);
        logger.warn(msg, args);
    }

    /**
     * 输出默认GWS日志类别的warn日志
     */
    public static void warn(Throwable throwable, String msg, Object... args) {
        Logger logger = getLogger();
        FormattedMessage message = new FormattedMessage(msg, args);
        String formattedMessage = message.getFormattedMessage();
        String importFormattedMessage = addImportantToLog(formattedMessage);
        logger.warn(importFormattedMessage, throwable);
    }

    /**
     * 输出默认GWS日志类别的错误日志
     */
    public static void error(String msg, Object... args) {
        Logger logger = getLogger();
        msg = addImportantToLog(msg);
        logger.error(msg, args);
    }

    /**
     * 输出默认GWS日志类别的错误日志
     */
    public static void error(Throwable throwable, String msg, Object... args) {
        Logger logger = getLogger();
        FormattedMessage message = new FormattedMessage(msg, args);
        String formattedMessage = message.getFormattedMessage();
        String importFormattedMessage = addImportantToLog(formattedMessage);
        logger.error(importFormattedMessage, throwable);
    }


    /**
     * 增加重要信息到日志中
     */
    private static String addImportantToLog(String msg) {
        StringBuilder sb = new StringBuilder();
        sb.append(msg);
        if (!msg.endsWith(GwsLogger.CON_QUOTE)) {
            sb.append(GwsLogger.CON_QUOTE);
        }
        return sb.toString();
    }

    static {
        logMaps.put(GwsLoggerTypeEnum.GWS, gwslog);
    }
}
