/**
 * Product Detail Page JavaScript
 */

document.addEventListener('DOMContentLoaded', () => {
    // Initialize i18n
    const i18n = new I18nManager();
    
    // Load product data
    loadProductDetail();
    
    // Initialize navigation
    initNavigation();
    
    // Initialize back to top
    initBackToTop();
});

/**
 * Load Product Detail
 */
function loadProductDetail() {
    // Get product ID from URL
    const urlParams = new URLSearchParams(window.location.search);
    const productId = urlParams.get('id');
    
    if (!productId || !productData[productId]) {
        // Redirect to products page if invalid ID
        window.location.href = 'index.html#products';
        return;
    }
    
    const product = productData[productId];
    const currentLang = localStorage.getItem('preferredLanguage') || detectLanguage();
    
    // Update page elements
    document.getElementById('productImage').src = product.image;
    document.getElementById('productImage').alt = product.name[currentLang] || product.name.ja;
    document.getElementById('productName').textContent = product.name[currentLang] || product.name.ja;
    document.getElementById('productPrice').textContent = product.price;
    document.getElementById('breadcrumbName').textContent = product.name[currentLang] || product.name.ja;
    
    // Update description with paragraphs
    const descContent = document.getElementById('productDescription');
    const description = product.description[currentLang] || product.description.ja;
    
    if (description) {
        // Split by double newline to create paragraphs
        const paragraphs = description.split('\n\n');
        descContent.innerHTML = paragraphs.map(p => `<p>${p.trim()}</p>`).join('');
    } else {
        descContent.innerHTML = '<p>商品简介即将更新...</p>';
    }
    
    // Update page title
    document.title = `${product.name[currentLang] || product.name.ja} | 臻选好物`;
}

/**
 * Detect browser language
 */
function detectLanguage() {
    const browserLang = navigator.language || navigator.userLanguage;
    if (browserLang.startsWith('zh')) return 'zh';
    if (browserLang.startsWith('ja')) return 'ja';
    return 'en';
}

/**
 * Navigation
 */
function initNavigation() {
    const menuToggle = document.getElementById('menuToggle');
    const mobileMenu = document.getElementById('mobileMenu');
    const mobileLinks = mobileMenu.querySelectorAll('a');
    
    // Mobile menu toggle
    menuToggle.addEventListener('click', () => {
        menuToggle.classList.toggle('active');
        mobileMenu.classList.toggle('active');
        document.body.style.overflow = mobileMenu.classList.contains('active') ? 'hidden' : 'auto';
    });
    
    // Close mobile menu on link click
    mobileLinks.forEach(link => {
        link.addEventListener('click', () => {
            menuToggle.classList.remove('active');
            mobileMenu.classList.remove('active');
            document.body.style.overflow = 'auto';
        });
    });
}

/**
 * Back to Top Button
 */
function initBackToTop() {
    const backToTop = document.getElementById('backToTop');
    
    backToTop.addEventListener('click', () => {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
}

/**
 * Re-load product when language changes
 */
document.querySelectorAll('.lang-btn').forEach(btn => {
    btn.addEventListener('click', () => {
        setTimeout(loadProductDetail, 100);
    });
});

