(function() {
    'use strict';

    let toastId = 0;

    const container = document.createElement('div');
    container.className = 'toast-container';
    container.id = 'toastContainer';
    document.body.appendChild(container);

    function escapeHtml(text) {
        const d = document.createElement('div');
        d.textContent = text || '';
        return d.innerHTML;
    }

    window.Notify = {
        toast: function(message, type, title) {
            if (!message) return;
            type = type || 'info';
            const icons = { success: '✓', error: '✕', info: 'ℹ', warning: '⚠' };
            const titles = { success: title || 'Успешно', error: title || 'Ошибка', info: title || 'Информация', warning: title || 'Внимание' };
            const icon = icons[type] || 'ℹ';
            const t = titles[type];

            const id = ++toastId;
            const el = document.createElement('div');
            el.className = 'toast toast-' + type;
            el.id = 'toast-' + id;
            el.innerHTML =
                '<div class="toast-icon">' + icon + '</div>' +
                '<div class="toast-content">' +
                    '<div class="toast-title">' + escapeHtml(t) + '</div>' +
                    '<div class="toast-message">' + escapeHtml(message) + '</div>' +
                '</div>' +
                '<button class="toast-close" onclick="Notify.dismiss(' + id + ')">✕</button>';

            container.appendChild(el);

            const timeout = setTimeout(function() {
                Notify.dismiss(id);
            }, 4000);

            el._timeout = timeout;
            el._id = id;

            return id;
        },

        success: function(message) {
            return this.toast(message, 'success');
        },

        error: function(message) {
            return this.toast(message, 'error');
        },

        info: function(message) {
            return this.toast(message, 'info');
        },

        warning: function(message) {
            return this.toast(message, 'warning');
        },

        dismiss: function(id) {
            var el = document.getElementById('toast-' + id);
            if (!el) return;
            if (el._timeout) clearTimeout(el._timeout);
            if (el.classList.contains('toast-removing')) return;
            el.classList.add('toast-removing');
            setTimeout(function() {
                if (el.parentNode) el.parentNode.removeChild(el);
            }, 250);
        },

        confirm: function(message, title, callback) {
            if (typeof title === 'function') {
                callback = title;
                title = 'Подтверждение';
            }
            title = title || 'Подтверждение';

            var overlay = document.createElement('div');
            overlay.className = 'confirm-overlay';
            overlay.innerHTML =
                '<div class="confirm-dialog">' +
                    '<div class="confirm-dialog-header">' +
                        '<div class="confirm-dialog-icon confirm-dialog-icon-warning">⚠</div>' +
                        '<div class="confirm-dialog-title">' + escapeHtml(title) + '</div>' +
                    '</div>' +
                    '<div class="confirm-dialog-message">' + escapeHtml(message) + '</div>' +
                    '<div class="confirm-dialog-actions">' +
                        '<button class="btn btn-outline confirm-cancel">Отмена</button>' +
                        '<button class="btn btn-danger confirm-ok">Подтвердить</button>' +
                    '</div>' +
                '</div>';

            document.body.appendChild(overlay);

            var result = false;

            function close() {
                if (overlay.parentNode) overlay.parentNode.removeChild(overlay);
                if (callback) callback(result);
            }

            overlay.querySelector('.confirm-cancel').addEventListener('click', function() {
                result = false;
                close();
            });

            overlay.querySelector('.confirm-ok').addEventListener('click', function() {
                result = true;
                close();
            });

            overlay.addEventListener('click', function(e) {
                if (e.target === overlay) {
                    result = false;
                    close();
                }
            });

            document.addEventListener('keydown', function handler(e) {
                if (e.key === 'Escape') {
                    result = false;
                    close();
                    document.removeEventListener('keydown', handler);
                }
            });

            return overlay;
        }
    };

})();
