/*
 * Copyright 2016 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.jexl3;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;

/**
 * A log implementation to help control tests results.
 */
public class CaptureLog implements Log {
    static Object caller() {
        final StackTraceElement[] stack = new Exception().fillInStackTrace().getStackTrace();
        return stack[2];
    }

    private final List<Object[]> captured = new ArrayList<>();

    public CaptureLog() {
        this("org.apache.commons.jexl3");
    }

    public CaptureLog(final String name) {
        //super(name);
    }

    public int count(final String type) {
        int count = 0;
        for (final Object[] l : captured) {
            if (type.equals(l[0].toString())) {
                count += 1;
            }
        }
        return count;
    }

    public List<String> getCapturedMessages() {
        return captured.stream()
                .map(a -> a[2])
                .filter(o -> o instanceof String)
                .map(o -> (String) o).collect(Collectors.toList());
    }

    public void clear() {
        captured.clear();
    }

    @Override
    public void debug(final Object o) {
        captured.add(new Object[]{"debug", caller(), o});
    }

    @Override
    public void debug(final Object o, final Throwable thrwbl) {
        captured.add(new Object[]{"debug", caller(), o, thrwbl});
    }

    @Override
    public void error(final Object o) {
        captured.add(new Object[]{"error", caller(), o});
    }

    @Override
    public void error(final Object o, final Throwable thrwbl) {
        captured.add(new Object[]{"error", caller(), o, thrwbl});
    }

    @Override
    public void fatal(final Object o) {
        captured.add(new Object[]{"fatal", caller(), o});
    }

    @Override
    public void fatal(final Object o, final Throwable thrwbl) {
        captured.add(new Object[]{"fatal", caller(), o, thrwbl});
    }

    @Override
    public void info(final Object o) {
        captured.add(new Object[]{"info", caller(), o});
    }

    @Override
    public void info(final Object o, final Throwable thrwbl) {
        captured.add(new Object[]{"info", caller(), o, thrwbl});
    }

    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    public boolean isEmpty() {
        return captured.isEmpty();
    }

    //@Override
    public boolean isEnabledFor(final int /*Priority*/ p) {
        return true;
    }

    @Override
    public boolean isErrorEnabled() {
        return true;
    }

    @Override
    public boolean isFatalEnabled() {
        return true;
    }

    @Override
    public boolean isInfoEnabled() {
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
        return true;
    }

    @Override
    public boolean isWarnEnabled() {
        return true;
    }

    @Override
    public void trace(final Object o) {
        captured.add(new Object[]{"trace", caller(), o});
    }

    @Override
    public void trace(final Object o, final Throwable thrwbl) {
        captured.add(new Object[]{"trace", caller(), o, thrwbl});
    }

    @Override
    public void warn(final Object o) {
        captured.add(new Object[]{"warn", caller(), o});
    }

    @Override
    public void warn(final Object o, final Throwable thrwbl) {
        captured.add(new Object[]{"warn", caller(), o, thrwbl});
    }

}
