import {expect} from "chai";
import {ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF} from "../../../source/logging/logger.mjs";
import {Handler} from "../../../source/logging/handler.mjs";
import {LogEntry} from "../../../source/logging/logentry.mjs";

describe('Logging', function () {

    describe('Handler.setLogLevel().getLogLevel()', function () {

        [
            [ALL], [TRACE], [DEBUG], [INFO], [WARN], [ERROR], [FATAL], [OFF],
        ].forEach(function (data) {

            let a = data.shift()

            it('the function should return the set value ' + a, function () {
                expect(new Handler().setLogLevel(a).getLogLevel()).to.be.equal(a);
            });
        });
    });


    describe('Handler.log()', function () {
        it('should return instanceof Handler', function () {
            expect(new Handler().log(new LogEntry(0, "test"))).to.be.true;
        });
    });

    describe('Handler.log()', function () {

        let handler;

        class TestHandler extends Handler {
            log(entry) {
                super.log(entry);

            }
        }

        beforeEach(function () {
            let handler = new TestHandler();
        })

        it('should return instanceof Handler', function () {
            expect(new Handler().log(new LogEntry(0, "test"))).to.be.true;
        });


    });

    describe('.log() with loglevel', function () {

        let TestHandler;

        before(() => {
            TestHandler = class extends Handler {
                constructor() {
                    super();
                    this.calls = 0;
                }

                log(loglevel) {
                    if (super.log(loglevel) === true) {
                        this.calls++
                        return true;
                    }
                    return false;
                }
            }
        });

        [ // ALL > TRACE > DEBUG > INFO > WARN > ERROR > FATAL > OFF  (ALL = 0xff;OFF = 0x00;
            [ALL, 6],
            [TRACE, 6],
            [DEBUG, 5],
            [INFO, 4],
            [WARN, 3],
            [ERROR, 2],
            [FATAL, 1],
            [OFF, 0]
        ].forEach(function (data) {

            let a = data.shift()
            let b = data.shift()

            it('is.log(' + a + ') should log ' + b, function () {
                let handler = new TestHandler().setLogLevel(a);
                handler.log(new LogEntry(TRACE));
                handler.log(new LogEntry(DEBUG));
                handler.log(new LogEntry(INFO));
                handler.log(new LogEntry(WARN));
                handler.log(new LogEntry(ERROR));
                handler.log(new LogEntry(FATAL));

                expect(handler.calls).is.equal(b)
            });
        });
    });

    describe('Handler.setLogLevel()', function () {
        it('should return instanceof Handler', function () {
            expect(new Handler().setLogLevel(ALL)).to.instanceOf(Handler);
        });
    });

    describe('Handler.getLogLevel()', function () {
        it('new logger should be 0', function () {
            expect(new Handler().getLogLevel()).to.be.equal(OFF);
        });
    });

    describe('Handler.setAll()', function () {
        it(' should instanceof Handler', function () {
            expect(new Handler().setAll()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setTrace()', function () {
        it('new logger should instanceof Handler', function () {
            expect(new Handler().setTrace()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setDebug()', function () {
        it(' should instanceof Handler', function () {
            expect(new Handler().setDebug()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setInfo()', function () {
        it('should return instanceof Handler', function () {
            expect(new Handler().setInfo()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setWarn()', function () {
        it(' should instanceof Handler', function () {
            expect(new Handler().setWarn()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setError()', function () {
        it('should return instanceof Handler', function () {
            expect(new Handler().setError()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setFatal()', function () {
        it('should return instanceof Handler', function () {
            expect(new Handler().setFatal()).to.instanceOf(Handler);
        });
    });
    describe('Handler.setOff()', function () {
        it('should return instanceof Handler', function () {
            expect(new Handler().setOff()).to.instanceOf(Handler);
        });
    });
});