class LimitlessNotification {

    // returns instance.
    set(title, msg, type, confirm = false, bar = false,  pos = null, icon = null) {
        setTimeout(function() {
            if (!isModuleSet()) {
                console.warn('Warning - pnotify.min.js is not loaded.');
                return;
            }

            let options = setOption(title, msg, type, icon, bar, pos, confirm);

            return new PNotify(options);
        },1);
    }
}

// Define directions
const stack_top_left = {"dir1": "down", "dir2": "right", "push": "top"};
const stack_bottom_left = {"dir1": "up", "dir2": "right", "push": "top"};
const stack_bottom_right = {"dir1": "up", "dir2": "left", "push": "top"};
const stack_bar_top = {"dir1": "down", "dir2": "right", "push": "top"};
const stack_bar_bottom = {"dir1": "up", "dir2": "right", "push": "top"};
const stack_modal = {"dir1": "down", "dir2": "right", "push": "top", "modal": true};

function setPosition(position, isBar, isConfirmation) {

    let pos = { };

    // if confirmation
    if(isConfirmation) {
        pos.addclass = 'stack-modal';
        pos.stack = stack_modal;

        return pos;
    }

    // if basics
    switch(position) {
        case 'top-left':
            pos.addclass = isBar ? 'stack-custom-top' : 'stack-top-left';
            pos.stack = isBar ? stack_bar_top : stack_top_left;
            break;
        case 'bottom-left':
            pos.addclass = isBar ? 'stack-custom-bottom' : 'stack-bottom-left';
            pos.stack = isBar ? stack_bar_bottom : stack_bottom_left;
            break;
        case 'bottom-right':
            pos.addclass = isBar ? 'stack-custom-bottom' : 'stack-bottom-right';
            pos.stack = isBar ? stack_bar_bottom : stack_bottom_right;
            break;
    }

    // additional bar conditions
    if(isBar)
    {
        switch (position) {
            case 'top-center':
            case 'top':
                pos.addclass = 'stack-custom-top';
                pos.stack = stack_bar_top;
                break;
            case 'bottom-center':
            case 'bottom':
                pos.addclass = 'stack-custom-bottom';
                pos.stack = stack_bar_bottom;
        }
    }

    if(!position && isBar) {
        pos.addclass = 'stack-custom-top';
        pos.stack = stack_bar_top;
    }

    return pos;
}

function setConfirmationOptions() {

    return {
        hide: false,
        confirm: {
            confirm: true,
            buttons: [
                {
                    text: 'Yes',
                    addClass: 'btn btn-sm btn-primary'
                },
                {
                    addClass: 'btn btn-sm btn-link'
                }
            ]
        },
        buttons: {
            closer: false,
            sticker: false
        }
    }
}

function getDefaultIcon(type, icon) {

    if(!icon) {
        icon = 'icon-warning22';
        switch (type) {
            case 'success':
                icon = 'icon-checkmark3';
                break;
            case 'error':
                icon = 'icon-blocked';
                break;
            case 'warning':
                icon = 'icon-warning22';
                break;
            case 'info':
                icon = 'icon-info22';
                break;
        }
    }

    return icon;
}

function getType(type) {
    switch (type) {
        case 'success':
        case 'error':
        case 'info':
        case 'error':
        case 'warning':
            break;
    }
    return type;
}

function setOption(title, msg, type, icon, bar, pos, confirm) {

    let isBar = (bar == true || bar == 'true');
    let isConfirmation = (confirm == true || confirm == 'true');

    let options = {
        title: title,
        text: msg,
        icon: getDefaultIcon(type, icon),
        type: type,
    }

    // set stack and position
    options = {...options, ...setPosition(pos, isBar, isConfirmation)};

    // set width to 100% if bar
    if(isBar && !isConfirmation) options.width = '100%';

    if(isConfirmation) options = {...options, ...setConfirmationOptions()};

    return options;
}

function isModuleSet() {
    return (typeof PNotify != 'undefined');
}

$(document).ready(function() {

    setTimeout(function() {

        if (!isModuleSet()) {
            console.warn('Warning - pnotify.min.js is not loaded.');
            return;
        }

        let elementObjects = $('.LimitlessNotification');

        elementObjects.each(function() {

            let obj = $(this);

            let title = obj.attr('data-title');
            let msg = obj.attr('data-message');
            let type = obj.attr('data-type');
            let icon = obj.attr('data-icon');
            let bar = obj.attr('data-bar');
            let pos = obj.attr('data-pos');
            let confirm = obj.attr('data-confirm');

            let options = setOption(title, msg, type, icon, bar, pos, confirm);
            new PNotify(options);

        });

    },1);

})