<?php
/**
 * Template for lazy loading large models when they become visible.
 * Uses Intersection Observer API to load models only when scrolled into view.
 * 
 * @package ExploreXR
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Available variables:
 * $model_instance_id - Unique ID for this model instance
 * $model_file - URL to the 3D model
 * $width - Display width
 * $height - Display height
 * $model_poster - URL to the poster image
 * $model_attributes - Array of model attributes
 */
?>

<div class="ExploreXR-lazy-container"
     id="<?php echo esc_attr($model_css_id); ?>"
     data-width="<?php echo esc_attr($width); ?>"
     data-height="<?php echo esc_attr($height); ?>"
     data-lazy-load="true"
     data-instance-id="<?php echo esc_attr($model_instance_id); ?>-container">
    
    <?php if (!empty($model_poster)) : ?>
        <!-- Display poster image as placeholder while model is not loaded -->
        <div class="ExploreXR-lazy-placeholder" id="<?php echo esc_attr($model_instance_id); ?>-placeholder">
            <?php if (!empty($model_poster_id)) : ?>
                <?php 
                echo wp_get_attachment_image($model_poster_id, 'large', false, array(
                    'alt' => esc_attr__('3D Model Loading...', 'explorexr-premium'),
                    'class' => 'ExploreXR-lazy-poster-image',
                    'loading' => 'lazy'
                ));
                ?>
            <?php else : ?>
                <img src="<?php echo esc_url($model_poster); ?>" 
                     alt="<?php esc_attr_e('3D Model Loading...', 'explorexr-premium'); ?>"
                     class="ExploreXR-lazy-poster-image"
                     loading="lazy" />
            <?php endif; ?>
            
            <!-- Loading indicator -->
            <div class="ExploreXR-lazy-loading-indicator" style="display: none;">
                <div class="ExploreXR-spinner"></div>
                <p><?php esc_html_e('Loading 3D Model...', 'explorexr-premium'); ?></p>
            </div>
        </div>
    <?php else : ?>
        <!-- No poster - show simple loading placeholder -->
        <div class="ExploreXR-lazy-placeholder ExploreXR-lazy-no-poster" id="<?php echo esc_attr($model_instance_id); ?>-placeholder">
            <div class="ExploreXR-lazy-loading-indicator">
                <div class="ExploreXR-spinner"></div>
                <p><?php esc_html_e('Preparing 3D Model...', 'explorexr-premium'); ?></p>
            </div>
        </div>
    <?php endif; ?>
    
    <!-- Hidden model-viewer that will be revealed when loaded -->
    <div class="ExploreXR-lazy-viewer-wrapper" 
         id="<?php echo esc_attr($model_instance_id); ?>-wrapper"
         style="display: none;"
         data-model-url="<?php echo esc_url($model_file); ?>"
         data-model-attributes="<?php echo esc_attr(wp_json_encode($model_attributes)); ?>"
         data-model-id="<?php echo esc_attr($model_id); ?>"
         data-instance-id="<?php echo esc_attr($model_instance_id); ?>">
        <!-- Model viewer will be dynamically inserted here by JavaScript -->
    </div>
</div>

<style>
.ExploreXR-lazy-container {
    position: relative;
    background: #f5f5f5;
    border-radius: 8px;
    overflow: hidden;
}

.ExploreXR-lazy-placeholder {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.ExploreXR-lazy-no-poster {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.ExploreXR-lazy-poster-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.ExploreXR-lazy-loading-indicator {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    z-index: 10;
    background: rgba(0, 0, 0, 0.7);
    padding: 20px 30px;
    border-radius: 8px;
    color: white;
}

.ExploreXR-spinner {
    width: 40px;
    height: 40px;
    margin: 0 auto 10px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: explorexr-spin 1s linear infinite;
}

@keyframes explorexr-spin {
    to { transform: rotate(360deg); }
}

.ExploreXR-lazy-loading-indicator p {
    margin: 0;
    font-size: 14px;
    font-weight: 500;
}

.ExploreXR-lazy-viewer-wrapper {
    width: 100%;
}

.ExploreXR-lazy-viewer-wrapper model-viewer {
    width: 100%;
    min-height: 400px;
}
</style>
