import { EventEmitter } from 'events';
import { LogEvent, LogLevel, LogSource } from '../common/LogTypes';

export interface LogEmitter {

}

export class LogManager extends EventEmitter {

    private events: LogEvent[] = []

    public static Events = {
        Log: 'log'
    }

    private idGenerator = 1;

    constructor() {
        super()
    }

    public log(msg: string, level: LogLevel, source: LogSource): void {
        if (level === LogLevel.Debug) return

        const event = {
            id: this.idGenerator++,
            date: new Date(),
            level,
            msg,
            src: source
        }

        if (this.events.length > 200) {
            this.events.pop()
        }

        this.events.push(event)

        this.emit(LogManager.Events.Log, event)
    }

    public getAll(): LogEvent[] {
        return this.events
    }
}