/**
 * ExploreXR Admin Dashboard JavaScript
 * Handles functionality for the ExploreXR dashboard
 */
jQuery(document).ready(function($) {
    
    // Copy shortcode functionality
    $('.copy-shortcode').on('click', function() {
        const shortcode = $(this).data('shortcode');
        
        // Use Clipboard API to copy text
        navigator.clipboard.writeText(shortcode).then(function() {
            // Show success notification
            const notification = $('#explorexr-copied-notification');
            notification.fadeIn(300);
            
            // Hide notification after 2 seconds
            setTimeout(function() {
                notification.fadeOut(300);
            }, 2000);
        }).catch(function(err) {
            if (typeof ExploreXRLogger !== 'undefined') {
                ExploreXRLogger.error('Could not copy text: ', err);
            }
            // Fallback for older browsers
            const textArea = document.createElement('textarea');
            textArea.value = shortcode;
            document.body.appendChild(textArea);
            textArea.select();
            document.execCommand('copy');
            document.body.removeChild(textArea);
            
            // Show success notification
            const notification = $('#explorexr-copied-notification');
            notification.fadeIn(300);
            setTimeout(function() {
                notification.fadeOut(300);
            }, 2000);
        });
    });
    
    // Model viewer modal functionality
    const modal = $('#explorexr-model-modal');
    const modelViewer = $('#explorexr-model-viewer');
    const modelTitle = $('#explorexr-model-title');
    
    // Open modal when clicking View Model
    $('.view-3d-model').on('click', function(e) {
        e.preventDefault();
        if (typeof ExploreXRLogger !== 'undefined') {
            ExploreXRLogger.log('View 3D Model button clicked');
        }
        const modelUrl = $(this).data('model-url');
        const modelName = $(this).data('model-name');
        const posterUrl = $(this).data('poster-url');
        const animationName = $(this).data('animation-name');

        if (typeof ExploreXRLogger !== 'undefined') {
            ExploreXRLogger.log('Loading 3D model from URL:', modelUrl);
            ExploreXRLogger.log('Modal element:', modal);
            ExploreXRLogger.log('Model viewer element:', modelViewer);
        }
        
        // Reset any previous error messages
        $('.error-details').text('');
        
        // Register error handler before setting source
        if (modelViewer[0]) {
            modelViewer[0].addEventListener('error', function(event) {
                if (typeof ExploreXRLogger !== 'undefined') {
                    ExploreXRLogger.error('Model viewer error:', event);
                }
                $('.error-details').text('Error type: ' + (event.detail?.type || 'unknown') + 
                                         ' - Path: ' + modelUrl);
            });
            
            // Add event listener for when model is successfully loaded
            modelViewer[0].addEventListener('load', function() {
                if (typeof ExploreXRLogger !== 'undefined') {
                    ExploreXRLogger.log('Model loaded successfully');
                }
            });
        }
        
        // Update model viewer source and title
        modelViewer.attr('src', modelUrl);
        modelTitle.text('3D Model Preview: ' + modelName);

        if (typeof ExploreXRLogger !== 'undefined') {
            ExploreXRLogger.log('Model viewer source set to:', modelViewer.attr('src'));
            ExploreXRLogger.log('Modal title set to:', modelTitle.text());
        }
        
        // Add poster if available
        if (posterUrl) {
            modelViewer.attr('poster', posterUrl);
        } else {
            modelViewer.removeAttr('poster');
        }
        
        // Enable single animation if available (Free Version feature)
        if (animationName) {
            modelViewer.attr('animation-name', animationName);
            modelViewer.attr('autoplay', '');
        } else {
            modelViewer.removeAttr('animation-name');
            modelViewer.removeAttr('autoplay');
        }
        
        // Configure basic camera controls (Free Version feature)
        modelViewer.attr('camera-controls', '');
        modelViewer.attr('camera-orbit', '0deg 75deg 105%');
        
        // Add loading UI options (Free Version feature)
        modelViewer.attr('data-loading-display', 'both');

        if (typeof ExploreXRLogger !== 'undefined') {
            ExploreXRLogger.log('About to show modal');
        }
        // Show modal
        modal.css('display', 'block');
        if (typeof ExploreXRLogger !== 'undefined') {
            ExploreXRLogger.log('Modal should now be visible');
        }
    });
    
    // Close modal
    $('.explorexr-model-close').on('click', function() {
        modal.css('display', 'none');
        modelViewer.attr('src', '');
        modelViewer.removeAttr('poster');
    });
    
    // Close modal when clicking outside of the content
    $(window).on('click', function(e) {
        if (e.target === modal[0]) {
            modal.css('display', 'none');
            modelViewer.attr('src', '');
            modelViewer.removeAttr('poster');
        }
    });
    
    // System status card interaction
    $('.explorexr-status-card').hover(
        function() {
            $(this).find('.explorexr-status-action').css('opacity', 1);
        },
        function() {
            $(this).find('.explorexr-status-action').css('opacity', 0.7);
        }
    );
    
    // Premium features interaction
    $('.explorexr-premium-card').hover(
        function() {
            $(this).find('.explorexr-premium-action').css('opacity', 1);
        },
        function() {
            $(this).find('.explorexr-premium-action').css('opacity', 0.7);
        }
    );
});
