(function($) {
    'use strict';

    const RedirectManager = {
        init: function() {
            if (!nenghuiRedirectParams.enable) return;
            
            // 如果已经检查过（用户取消或已跳转），不再打扰
            if (sessionStorage.getItem('nenghui_redirect_checked')) {
                return;
            }

            this.checkLanguage();
        },

        checkLanguage: function() {
            const lang = navigator.language || navigator.userLanguage;
            // 检查是否为中文 (zh, zh-CN, zh-TW, etc.)
            if (lang && lang.toLowerCase().indexOf('zh') === 0) {
                this.showModal();
            } else {
                // 如果非中文，且开启了IP检查，则检查IP
                if (nenghuiRedirectParams.check_ip) {
                    this.checkIP();
                }
            }
        },

        checkIP: function() {
            // 使用 WordPress AJAX 请求后端检查 IP
            $.ajax({
                url: nenghuiRedirectParams.ajax_url,
                type: 'POST',
                data: {
                    action: 'nenghui_check_user_ip',
                    nonce: nenghuiRedirectParams.nonce
                },
                success: (response) => {
                    if (response.success && response.data.is_china) {
                        this.showModal();
                    } else {
                        // 标记已检查，避免重复请求
                        sessionStorage.setItem('nenghui_redirect_checked', 'true');
                    }
                },
                error: () => {
                    // IP check failed silently
                }
            });
        },

        showModal: function() {
            // 创建模态框 HTML
            const modalHtml = `
                <div class="nenghui-redirect-modal-overlay" id="nenghuiRedirectModal">
                    <div class="nenghui-redirect-modal">
                        <div class="nenghui-redirect-title">访问提示 / Notice</div>
                        <div class="nenghui-redirect-message">
                            检测到您可能来自中国，建议访问我们的中国区官网以获得更好的体验。<br><br>
                            It seems you are visiting from China. Would you like to visit our China site for better experience?
                        </div>
                        <div class="nenghui-redirect-buttons">
                            <button class="nenghui-btn nenghui-btn-secondary" id="nenghuiRedirectCancel">
                                留在当前站<br>Stay Here
                            </button>
                            <button class="nenghui-btn nenghui-btn-primary" id="nenghuiRedirectConfirm">
                                前往中国官网<br>Go to China Site
                            </button>
                        </div>
                    </div>
                </div>
            `;

            $('body').append(modalHtml);
            
            // 强制重绘以触发动画
            const modal = $('#nenghuiRedirectModal');
            setTimeout(() => modal.addClass('active'), 10);

            // 绑定事件
            $('#nenghuiRedirectCancel').on('click', () => {
                modal.removeClass('active');
                setTimeout(() => modal.remove(), 300);
                sessionStorage.setItem('nenghui_redirect_checked', 'true');
            });

            $('#nenghuiRedirectConfirm').on('click', () => {
                sessionStorage.setItem('nenghui_redirect_checked', 'true');
                window.location.href = nenghuiRedirectParams.target_url;
            });
        }
    };

    $(document).ready(function() {
        RedirectManager.init();
    });

})(jQuery);
