import { LogManager } from 'aurelia-framework';
import { Logger } from 'aurelia-logging';
import * as toastr from 'toastr';

enum ToastType { Info, Error }

export class CustomLogger {
    logManager: Logger;

    constructor() {
        this.logManager = LogManager.getLogger('application-insights-plugin');
    }

    public Log(message: string, data: any, source: string, showToast: boolean) {
        this.LogIt(message, data, source, showToast, ToastType.Info);
    }

    public LogError(message: string, data: any, source: string, showToast: boolean) {
        this.LogIt(message, data, source, showToast, ToastType.Error);
    }

    private LogIt(message: string, data: any, source: string, showToast: boolean, toastType: ToastType): void {
        source = source ? '[' + source + '] ' : '';
        if (toastType == ToastType.Error) {
            this.logManager.error(message, data);
        }
        else {
            this.logManager.info(message, data);
        }

        if (showToast) {
            if (toastType == ToastType.Error) {
                toastr.error(message);
            }
            else {
                toastr.info(message);
            }
        }
    }
}
