/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.trace;

import java.util.function.IntPredicate;
import org.slf4j.Logger;
import static org.slf4j.spi.LocationAwareLogger.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public enum LoggerLevel implements IntPredicate {
    
    TRACE(TRACE_INT) {
        @Override
        public boolean isEnabled(Logger logger) {
            return logger.isTraceEnabled();
        }
    },
    DEBUG(DEBUG_INT) {
        @Override
        public boolean isEnabled(Logger logger) {
            return logger.isDebugEnabled();
        }
    },
    INFO(INFO_INT) {
        @Override
        public boolean isEnabled(Logger logger) {
            return logger.isInfoEnabled();
        }
    },
    WARN(WARN_INT) {
        @Override
        public boolean isEnabled(Logger logger) {
            return logger.isWarnEnabled();
        }
    },
    ERROR(ERROR_INT) {
        @Override
        public boolean isEnabled(Logger logger) {
            return logger.isErrorEnabled();
        }
    },
    UNKNOWN(Integer.MAX_VALUE) {
        @Override
        public boolean isEnabled(Logger logger) {
            return true;
        }
    };
    
    private final int level;
    
    LoggerLevel(int level) {
        this.level = level;
    }
    
    public int level() {
        return level;
    }
    
    public abstract boolean isEnabled(Logger logger);
    
    @Override // NOPMD
    public boolean test(int level) {
        return level >= this.level;
    }

    public static LoggerLevel valueOf(Logger logger) {
        if(logger.isTraceEnabled()) {
            return TRACE;
        }
        if(logger.isDebugEnabled()) {
            return DEBUG;
        }
        if(logger.isInfoEnabled()) {
            return INFO;
        }
        if(logger.isWarnEnabled()) {
            return WARN;
        }
        if(logger.isErrorEnabled()) {
            return ERROR;
        }
        return UNKNOWN;
    }
    
    public static LoggerLevel valueOf(int level) {
        switch(level) {
            case TRACE_INT:
                return TRACE;
            case DEBUG_INT:
                return DEBUG;
            case INFO_INT:
                return INFO;
            case WARN_INT:
                return WARN;
            case ERROR_INT:
                return ERROR;
            default:
                return UNKNOWN;
        }
    }
    
}
