/**
 * Simple Metadata Section Toggle
 * 
 * Handles showing/hiding the metadata grid with a simple button
 */

// Global function to toggle metadata section visibility
function toggleMetadataSection() {
    console.log('toggleMetadataSection called');
    
    const content = document.getElementById('metadataContent');
    const arrow = document.getElementById('metadataArrow');
    
    if (!content) {
        console.log('No metadata content found');
        return;
    }
    
    const isVisible = content.style.display !== 'none';
    
    if (isVisible) {
        // Hide the content
        content.style.display = 'none';
        if (arrow) arrow.textContent = '▼';
        console.log('Metadata hidden');
    } else {
        // Show the content
        content.style.display = 'block';
        if (arrow) arrow.textContent = '▲';
        console.log('Metadata shown');
        
        // Trigger MathJax rendering for LaTeX content
        if (typeof MathJax !== 'undefined') {
            MathJax.typesetPromise([content]).catch(function (err) {
                console.log('MathJax typeset failed: ' + err.message);
            });
        }
    }
    
    // Save state to localStorage
    localStorage.setItem('metadataVisible', (!isVisible).toString());
}

// Function to expand long text
function expandText(button) {
    const longText = button.previousElementSibling;
    const fullText = button.nextElementSibling;
    
    if (fullText.style.display === 'none') {
        longText.style.display = 'none';
        fullText.style.display = 'inline';
        button.textContent = 'Show less';
    } else {
        longText.style.display = 'inline';
        fullText.style.display = 'none';
        button.textContent = 'Show more';
    }
}

// Copy metadata file path to clipboard
async function copyMetadataPath() {
    const pathElement = document.getElementById('metadata-file-path');
    const copyBtn = document.querySelector('.copy-path-btn');
    
    if (!pathElement || !copyBtn) {
        console.log('Path element or copy button not found');
        return;
    }
    
    const path = pathElement.textContent;
    console.log('Attempting to copy path:', path);
    
    try {
        // Try using the modern clipboard API
        if (navigator.clipboard && window.isSecureContext) {
            await navigator.clipboard.writeText(path);
        } else {
            // Fallback for older browsers
            const textArea = document.createElement('textarea');
            textArea.value = path;
            textArea.style.position = 'fixed';
            textArea.style.left = '-999999px';
            textArea.style.top = '-999999px';
            document.body.appendChild(textArea);
            textArea.focus();
            textArea.select();
            document.execCommand('copy');
            textArea.remove();
        }
        
        // Visual feedback
        const originalText = copyBtn.innerHTML;
        copyBtn.innerHTML = '✅ Copied!';
        copyBtn.classList.add('copied');
        
        setTimeout(() => {
            copyBtn.innerHTML = originalText;
            copyBtn.classList.remove('copied');
        }, 2000);
        
        console.log('Path copied successfully');
        
    } catch (err) {
        console.error('Failed to copy path: ', err);
        
        // Show error feedback
        const originalText = copyBtn.innerHTML;
        copyBtn.innerHTML = '❌ Failed';
        
        setTimeout(() => {
            copyBtn.innerHTML = originalText;
        }, 2000);
    }
}

// Make functions globally available
window.toggleMetadataSection = toggleMetadataSection;
window.expandText = expandText;
window.copyMetadataPath = copyMetadataPath;

// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
    console.log('Metadata section script loaded');
    
    const content = document.getElementById('metadataContent');
    if (content) {
        // Check if user previously had it expanded
        const wasVisible = localStorage.getItem('metadataVisible') === 'true';
        
        if (wasVisible) {
            content.style.display = 'block';
            const arrow = document.getElementById('metadataArrow');
            if (arrow) arrow.textContent = '▲';
        } else {
            content.style.display = 'none';
            const arrow = document.getElementById('metadataArrow');
            if (arrow) arrow.textContent = '▼';
        }
        
        console.log('Metadata section initialized, visible:', wasVisible);
    }
});
