var notifyDefaultDelay = 5000;
var notifyDefaultTemplate = '<div data-notify="container" class="col-xs-11 col-sm-3 alert alert-{0}" role="alert">'+
'<div class="row">'+
'<div class="col-2 rounded-left">'+
'<span data-notify="icon"></span>'+
'</div>'+
'<div class="col">'+
'<button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button>'+
'<h5 data-notify="title">{1}</h5>'+
'<div data-notify="message">{2}</div>'+
'<div data-notify="button"></div>'+
'</div>'+
'<div class="progress" data-notify="progressbar">'+
    '<div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>'+
'</div>'+
'<a href="{3}" target="{4}" data-notify="url"></a>'+
'</div>'+
'</div>'

function notifyOk(title, message, url = undefined) {
    $.notify({
        icon: 'fas fa-check',
        title: title,
        message: message,
        url: url
    },{
        type: 'success'
    });
}

function notifyInfo(title, message, url = undefined) {
    $.notify({
        icon: 'fas fa-info',
        title: title,
        message: message,
        url: url
    },{
        type: 'info'
    });
}

function notifyError(xhr) {
    var message = errorMessage(xhr);
    var reason = errorReason(xhr);
    var recovery = errorRecovery(xhr);
    var domain = errorDomain(xhr)
    var code = errorCode(xhr)

    var url = undefined
    var delay = notifyDefaultDelay
    var template = notifyDefaultTemplate
    if (domain == 'terms' && code == 1) {
        url = $('meta[name=application-config]').attr('data-terms')
        delay = 0
        template = template.replace('<div data-notify="button"></div>', '<div data-notify="button"><a href="{3}" target="{4}" class="btn btn-secondary">'+$('meta[name=application-config]').attr('data-terms-anchor')+'</a></div>')
    }

    $.notify({
        icon: 'fas fa-exclamation',
        title: (message != undefined ? message : ''),
        message: (reason != undefined ? '<p>'+reason+'</p>' : '') + (recovery != undefined ? '<p>'+recovery+'</p>' : ''),
        url: url
    },{
        type: 'danger',
        delay: delay,
        template: template
    });
}

$(function(){
    $.notifyDefaults({
        animate: {
            enter: 'animated bounceInDown',
            exit: 'animated bounceOutUp'
        },
        allow_dismiss: true,
        delay: notifyDefaultDelay,
        template: notifyDefaultTemplate
    });
});