package com.atlassian.stash.internal.log;

import ch.qos.logback.classic.Level;
import org.slf4j.Logger;

/**
 * Enhancer for the standard Logback {@code Level} concept which provides the useful ability to log at a given level
 * simply by having that level and a target logger.
 */
public enum EnhancedLevel {

    ALL(Level.ALL),
    TRACE(Level.TRACE) {
        
        @Override
        public void log(Logger logger, String message, Object... parameters) {
            logger.trace(message, parameters);
        }
    },
    DEBUG(Level.DEBUG),
    INFO(Level.INFO) {

        @Override
        public void log(Logger logger, String message, Object... parameters) {
            logger.info(message, parameters);
        }
    },
    WARN(Level.WARN) {

        @Override
        public void log(Logger logger, String message, Object... parameters) {
            logger.warn(message, parameters);
        }
    },
    ERROR(Level.ERROR) {

        @Override
        public void log(Logger logger, String message, Object... parameters) {
            logger.error(message, parameters);
        }
    },
    OFF(Level.OFF) {

        @Override
        public void log(Logger logger, String message, Object... parameters) {
            //No reason to log--OFF level means nothing will actually make it through
        }
    };

    private final Level level;

    private EnhancedLevel(Level level) {
        this.level = level;
    }

    public Level getLevel() {
        return this.level;
    }
    
    public void log(Logger logger, String message, Object... parameters) {
        //Default implementation (used by ALL and DEBUG)
        logger.debug(message, parameters);
    }

    public static EnhancedLevel fromLevel(Level level) {
        for (EnhancedLevel value : values()) {
            if (value.getLevel() == level) {
                return value;
            }
        }
        return OFF;
    }
}
