
export class SovNotification {

    static init() {
        toastr.options.timeOut = 3000;
        toastr.options.positionClass = 'toast-bottom-right';
        toastr.options.onHidden = function () {
            if ($('#toast-container .toast').length == 1) {
                $('#toast-close-all').remove();
            }
        };
    }

    static set(notifications) {
        var currentNotifications = new Array();
        for (var type in notifications) {
            for (var key in notifications[type]) {
                if (!currentNotifications.includes(notifications[type][key])) {
                    currentNotifications.push(notifications[type][key]);
                    SovNotification.add(type, notifications[type][key]);
                }
            }
        }
    }

    static add(type, text) {

        toastr[type](text);

        let $container = $('#toast-container') ;
        let selectorButtonCloseAll = '#toast-close-all' ;
        let countMessages = $container.find('.toast').length ;

        if ($(selectorButtonCloseAll).length == 0 && countMessages > 2) {
            $container.prepend('<button id="toast-close-all"><i class="fa fa-times"></i></button>');
        }

        $(selectorButtonCloseAll).off('click').on('click', function () {
            toastr.remove();
            if (countMessages == 0) {
                $('#toast-close-all').remove();
            }
        });
    }

}
