define([
    '../../core/utils/data',
    '../../core/utils/helper',
    '../../core/utils/method',
    '../../core/models/index'
], (data, helper, method, model) => {
    (function($) {
        new method('toast', {
            init: function (message) {
                const context = this;

                if (typeof message === 'undefined') {
                    return false;
                }
                else if (typeof message === 'object') {
                    message = JSON.stringify(message);
                }

                let $toastBox = $('.toast-box');
                if ($toastBox.length <= 0) {
                    $toastBox = $(context.$helper.parseTemplate('toastBox'));
                    $toastBox.appendTo('body');
                    $toastBox.css({
                        marginLeft: -$toastBox.width() / 2
                    });
                }

                const $toast = $(context.$helper.parseTemplate('toast', {
                    message: message.replace(/\r?\n/gi, '<br />')
                }));
                const $real = $($toast.clone()).add('<br />');
                $toast.addClass('toast-empty');
                $toast.appendTo('.toast-box');
                $real.addClass('toast-real');

                const height = $toast.height();
                $toast.addClass('toast-anim');
                setTimeout(() => {
                    $toast.addClass('toast-anim-start').stop().animate({
                        paddingTop: '1em',
                        paddingBottom: '1em',
                        marginBottom: '1em',
                    }, 300);
                    $toast.height(height);
                    setTimeout(() => {
                        $toast.remove();
                        $real.appendTo($toastBox);
                        setTimeout(() => {
                            $real.addClass('toast-anim-start');
                            const t = Math.min(Math.max(1000 / 20 * message.length, 1500), 5000);
                            setTimeout(() => {
                                $real.removeClass('toast-anim-start');
                                setTimeout(() => {
                                    $real.remove();
                                }, 300);
                            }, t);
                        }, 25);
                    }, 300);
                }, 25);
            },
            template: {
                toast: '<div class="toast">{message}</div>',
                toastBox: '<div class="toast-box"></div>'
            }
        });
    }(jQuery));
});