/**
 * HuSNS - 移动端美观对话框组件
 */

(function(window) {
    'use strict';

    // 图标映射
    var icons = {
        info: 'ℹ️',
        success: '✓',
        warning: '⚠️',
        error: '✕',
        confirm: '❓'
    };

    /**
     * 创建对话框 DOM
     */
    function createDialog(options) {
        var overlay = document.createElement('div');
        overlay.className = 'husns-dialog-overlay';
        
        var dialog = document.createElement('div');
        dialog.className = 'husns-dialog';
        
        // 图标
        var icon = document.createElement('div');
        icon.className = 'husns-dialog-icon ' + options.type;
        icon.textContent = icons[options.type] || icons.info;
        
        // 标题
        var title = document.createElement('h3');
        title.className = 'husns-dialog-title';
        title.textContent = options.title || '';
        
        // 内容
        var content = document.createElement('p');
        content.className = 'husns-dialog-content';
        content.textContent = options.message || '';
        
        // 按钮区域
        var buttons = document.createElement('div');
        buttons.className = 'husns-dialog-buttons' + (options.type === 'confirm' ? '' : ' single');
        
        if (options.type === 'confirm') {
            // 取消按钮
            var cancelBtn = document.createElement('button');
            cancelBtn.className = 'husns-dialog-btn secondary';
            cancelBtn.textContent = options.cancelText || '取消';
            cancelBtn.addEventListener('click', function() {
                closeDialog();
                if (options.cancelCallback) {
                    options.cancelCallback();
                }
            });
            buttons.appendChild(cancelBtn);
            
            // 确认按钮
            var confirmBtn = document.createElement('button');
            confirmBtn.className = 'husns-dialog-btn primary';
            confirmBtn.textContent = options.confirmText || '确定';
            confirmBtn.addEventListener('click', function() {
                closeDialog();
                if (options.confirmCallback) {
                    options.confirmCallback();
                }
            });
            buttons.appendChild(confirmBtn);
        } else {
            // 确定按钮
            var okBtn = document.createElement('button');
            okBtn.className = 'husns-dialog-btn primary';
            okBtn.textContent = options.okText || '确定';
            okBtn.addEventListener('click', function() {
                closeDialog();
                if (options.okCallback) {
                    options.okCallback();
                }
            });
            buttons.appendChild(okBtn);
        }
        
        dialog.appendChild(icon);
        dialog.appendChild(title);
        dialog.appendChild(content);
        dialog.appendChild(buttons);
        overlay.appendChild(dialog);
        
        return overlay;
    }

    /**
     * 关闭对话框
     */
    function closeDialog() {
        var overlay = document.querySelector('.husns-dialog-overlay');
        if (overlay) {
            overlay.classList.remove('active');
            setTimeout(function() {
                if (overlay.parentNode) {
                    overlay.parentNode.removeChild(overlay);
                }
            }, 300);
        }
    }

    /**
     * 信息对话框
     */
    function showInfo(message, options) {
        options = options || {};
        var opts = Object.assign({
            type: 'info',
            title: '提示',
            message: message,
            okText: '确定',
            okCallback: options.callback || options.okCallback
        }, options);
        
        var overlay = createDialog(opts);
        document.body.appendChild(overlay);
        
        // 添加动画
        setTimeout(function() {
            overlay.classList.add('active');
        }, 10);
        
        // ESC 键关闭
        document.addEventListener('keydown', function escHandler(e) {
            if (e.key === 'Escape') {
                closeDialog();
                document.removeEventListener('keydown', escHandler);
            }
        });
    }

    /**
     * 成功对话框
     */
    function showSuccess(message, options) {
        options = options || {};
        var opts = Object.assign({
            type: 'success',
            title: '成功',
            message: message,
            okText: '确定',
            okCallback: options.callback || options.okCallback
        }, options);
        
        var overlay = createDialog(opts);
        document.body.appendChild(overlay);
        
        setTimeout(function() {
            overlay.classList.add('active');
        }, 10);
    }

    /**
     * 警告对话框
     */
    function showWarning(message, options) {
        options = options || {};
        var opts = Object.assign({
            type: 'warning',
            title: '警告',
            message: message,
            okText: '确定',
            okCallback: options.callback || options.okCallback
        }, options);
        
        var overlay = createDialog(opts);
        document.body.appendChild(overlay);
        
        setTimeout(function() {
            overlay.classList.add('active');
        }, 10);
    }

    /**
     * 错误对话框
     */
    function showError(message, options) {
        options = options || {};
        var opts = Object.assign({
            type: 'error',
            title: '错误',
            message: message,
            okText: '确定',
            okCallback: options.callback || options.okCallback
        }, options);
        
        var overlay = createDialog(opts);
        document.body.appendChild(overlay);
        
        setTimeout(function() {
            overlay.classList.add('active');
        }, 10);
    }

    /**
     * 确认对话框
     */
    function showConfirm(message, options) {
        options = options || {};
        var opts = Object.assign({
            type: 'confirm',
            title: '确认',
            message: message,
            confirmText: '确定',
            cancelText: '取消',
            confirmCallback: options.callback || options.confirmCallback,
            cancelCallback: options.cancelCallback
        }, options);
        
        var overlay = createDialog(opts);
        document.body.appendChild(overlay);
        
        setTimeout(function() {
            overlay.classList.add('active');
        }, 10);
    }

    /**
     * 加入圈子成功提示
     */
    function showJoinSuccess() {
        showSuccess('加入圈子成功', {
            okCallback: function() {
                // 可以在这里添加额外逻辑
            }
        });
    }

    /**
     * 退出圈子确认
     */
    function showExitConfirm(callback) {
        showConfirm('确定要退出该圈子吗？', {
            title: '退出圈子',
            confirmText: '退出',
            confirmCallback: callback
        });
    }

    /**
     * 退出圈子成功提示
     */
    function showExitSuccess() {
        showSuccess('退出圈子成功', {
            okCallback: function() {
                // 可以在这里添加额外逻辑
            }
        });
    }

    /**
     * 未加入圈子提示
     */
    function showNotJoined() {
        showWarning('还未加入该圈子', {
            title: '提示'
        });
    }

    // 暴露全局接口
    window.HuSNS_Dialog = {
        info: showInfo,
        success: showSuccess,
        warning: showWarning,
        error: showError,
        confirm: showConfirm,
        joinSuccess: showJoinSuccess,
        exitConfirm: showExitConfirm,
        exitSuccess: showExitSuccess,
        notJoined: showNotJoined
    };

})(window);
