/**
 * HuSNS Mobile Modal Functions
 * 弹窗相关通用函数
 */

(function() {
    'use strict';

    /**
     * 打开弹窗
     */
    function openModal(modalId) {
        var modal = document.getElementById(modalId);
        if (modal) {
            modal.classList.add('active');
            modal.style.display = 'flex';
            document.body.style.overflow = 'hidden';
        }
    }

    /**
     * 关闭弹窗
     */
    function closeModal(modalId) {
        var modal = document.getElementById(modalId);
        if (modal) {
            modal.classList.remove('active');
            modal.style.display = 'none';
            document.body.style.overflow = '';
        }
    }

    /**
     * 切换弹窗
     */
    function toggleModal(modalId) {
        var modal = document.getElementById(modalId);
        if (modal) {
            if (modal.classList.contains('active')) {
                closeModal(modalId);
            } else {
                openModal(modalId);
            }
        }
    }

    // 点击遮罩关闭弹窗
    document.addEventListener('click', function(e) {
        if (e.target.classList.contains('modal-overlay') && e.target.classList.contains('active')) {
            closeModal(e.target.id);
        }
    });

    // ESC 关闭弹窗
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            document.querySelectorAll('.modal-overlay.active').forEach(function(modal) {
                closeModal(modal.id);
            });
        }
    });

    // ========== Toast 提示 ==========

    /**
     * 显示Toast提示
     */
    function showToast(message, duration) {
        duration = duration || 2000;
        var toast = document.getElementById('toast');
        if (toast) {
            toast.textContent = message;
            toast.style.display = 'block';
            setTimeout(function() {
                toast.classList.add('show');
                setTimeout(function() {
                    toast.classList.remove('show');
                    setTimeout(function() {
                        toast.style.display = 'none';
                    }, 300);
                }, duration);
            }, 10);
        }
    }

    // ========== TabBar 导航处理 ==========

    /**
     * 处理发布按钮点击
     */
    function handlePublishClick() {
        if (typeof userId !== 'undefined' && userId) {
            openModal('publishModal');
        } else {
            openLoginModal();
        }
    }

    /**
     * 处理通知按钮点击
     */
    function handleNotificationClick() {
        var baseUrl = document.body.dataset.baseUrl || '';
        if (typeof userId !== 'undefined' && userId) {
            window.location.href = baseUrl + '/?r=mobile/notification';
        } else {
            openLoginModal();
        }
    }

    /**
     * 处理个人中心按钮点击
     */
    function handleProfileClick() {
        var baseUrl = document.body.dataset.baseUrl || '';
        if (typeof userId !== 'undefined' && userId) {
            window.location.href = baseUrl + '/?r=mobile/profile';
        } else {
            openLoginModal();
        }
    }

    /**
     * 打开登录弹窗
     */
    function openLoginModal() {
        openModal('loginModal');
    }

    // ========== 全局初始化 ==========

    document.addEventListener('DOMContentLoaded', function() {
        // 初始化视频缩略图
        if (typeof generateVideoThumbnails === 'function') {
            generateVideoThumbnails();
        }

        // 初始化举报提交
        if (typeof HuSNS !== 'undefined' && HuSNS.initReportSubmit) {
            HuSNS.initReportSubmit();
        }
    });

    // ========== 导出全局函数 ==========

    window.openModal = openModal;
    window.closeModal = closeModal;
    window.toggleModal = toggleModal;
    window.showToast = showToast;
    window.handlePublishClick = handlePublishClick;
    window.handleNotificationClick = handleNotificationClick;
    window.handleProfileClick = handleProfileClick;
    window.openLoginModal = openLoginModal;

})();
