<?php
/**
 * Event Layout Manager - Provides flexible display options
 */
if (!defined('ABSPATH')) exit;

// Get layout settings
$em_layout_mode = $event_settings['em_layout_mode'] ?? 'default';
$em_layout_style = $event_settings['em_layout_style'] ?? 'modern';
$em_layout_density = $event_settings['em_layout_density'] ?? 'comfortable';

// Define layout classes based on settings
$layout_classes = array();
$container_classes = array();

// Layout Mode Classes
switch ($em_layout_mode) {
    case 'compact':
        $layout_classes[] = 'em-layout-compact';
        $container_classes[] = 'em-max-w-4xl';
        break;
    case 'wide':
        $layout_classes[] = 'em-layout-wide';
        $container_classes[] = 'em-max-w-7xl';
        break;
    case 'modal':
        $layout_classes[] = 'em-layout-modal';
        $container_classes[] = 'em-max-w-2xl';
        break;
    case 'list':
        $layout_classes[] = 'em-layout-list';
        $container_classes[] = 'em-max-w-5xl';
        break;
    default:
        $layout_classes[] = 'em-layout-default';
        $container_classes[] = 'em-max-w-6xl';
}

// Layout Style Classes
switch ($em_layout_style) {
    case 'minimal':
        $layout_classes[] = 'em-style-minimal';
        break;
    case 'elegant':
        $layout_classes[] = 'em-style-elegant';
        break;
    case 'bold':
        $layout_classes[] = 'em-style-bold';
        break;
    default:
        $layout_classes[] = 'em-style-modern';
}

// Layout Density Classes
switch ($em_layout_density) {
    case 'compact':
        $layout_classes[] = 'em-density-compact';
        break;
    case 'spacious':
        $layout_classes[] = 'em-density-spacious';
        break;
    default:
        $layout_classes[] = 'em-density-comfortable';
}

// Combine all classes
$all_layout_classes = implode(' ', array_merge($layout_classes, $container_classes));
?>

<!-- Layout Manager Styles -->
<style>
/* Layout Mode Styles */
.em-layout-compact {
    --em-section-padding: 1rem;
    --em-component-gap: 1rem;
    --em-border-radius: 0.5rem;
}

.em-layout-wide {
    --em-section-padding: 2rem;
    --em-component-gap: 2rem;
    --em-border-radius: 1rem;
}

.em-layout-modal {
    --em-section-padding: 1.5rem;
    --em-component-gap: 1.5rem;
    --em-border-radius: 0.75rem;
    max-height: 80vh;
    overflow-y: auto;
}

.em-layout-list {
    --em-section-padding: 1rem;
    --em-component-gap: 0.5rem;
    --em-border-radius: 0.375rem;
}

.em-layout-default {
    --em-section-padding: 1.5rem;
    --em-component-gap: 1.5rem;
    --em-border-radius: 0.75rem;
}

/* Layout Style Variations */
.em-style-minimal {
    --em-shadow-level: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
    --em-border-width: 1px;
    --em-accent-opacity: 0.05;
}

.em-style-minimal .em-card {
    box-shadow: var(--em-shadow-level);
    border: var(--em-border-width) solid var(--em-gray-200);
}

.em-style-elegant {
    --em-shadow-level: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --em-border-width: 0;
    --em-accent-opacity: 0.1;
}

.em-style-elegant .em-card {
    box-shadow: var(--em-shadow-level);
    border: none;
    background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
}

.em-style-bold {
    --em-shadow-level: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --em-border-width: 2px;
    --em-accent-opacity: 0.15;
}

.em-style-bold .em-card {
    box-shadow: var(--em-shadow-level);
    border: var(--em-border-width) solid var(--em-primary);
    border-opacity: 0.2;
}

.em-style-modern {
    --em-shadow-level: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --em-border-width: 1px;
    --em-accent-opacity: 0.08;
}

/* Layout Density Variations */
.em-density-compact {
    --em-text-scale: 0.9;
    --em-spacing-scale: 0.75;
}

.em-density-compact .em-px-6 { padding-left: 1rem; padding-right: 1rem; }
.em-density-compact .em-py-6 { padding-top: 1rem; padding-bottom: 1rem; }
.em-density-compact .em-py-8 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
.em-density-compact .em-gap-6 { gap: 1rem; }
.em-density-compact .em-mb-6 { margin-bottom: 1rem; }
.em-density-compact .em-mb-8 { margin-bottom: 1.5rem; }

.em-density-spacious {
    --em-text-scale: 1.1;
    --em-spacing-scale: 1.25;
}

.em-density-spacious .em-px-6 { padding-left: 2rem; padding-right: 2rem; }
.em-density-spacious .em-py-6 { padding-top: 2rem; padding-bottom: 2rem; }
.em-density-spacious .em-py-8 { padding-top: 2.5rem; padding-bottom: 2.5rem; }
.em-density-spacious .em-gap-6 { gap: 2rem; }
.em-density-spacious .em-mb-6 { margin-bottom: 2rem; }
.em-density-spacious .em-mb-8 { margin-bottom: 2.5rem; }

.em-density-comfortable {
    --em-text-scale: 1;
    --em-spacing-scale: 1;
}

/* Responsive Layout Adjustments */
@media (max-width: 768px) {
    .em-layout-wide,
    .em-layout-default {
        --em-section-padding: 1rem;
        --em-component-gap: 1rem;
    }
    
    .em-density-spacious {
        --em-spacing-scale: 1;
    }
    
    .em-density-spacious .em-px-6 { padding-left: 1.5rem; padding-right: 1.5rem; }
    .em-density-spacious .em-py-6 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
}

/* Modal-specific styles */
.em-layout-modal {
    position: relative;
}

.em-layout-modal::before {
    content: '';
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--em-gray-300), transparent);
    z-index: 10;
}

/* List layout specific styles */
.em-layout-list .em-registration-section,
.em-layout-list .em-gallery-section,
.em-layout-list .em-speakers-section,
.em-layout-list .em-organizers-section,
.em-layout-list .em-sponsors-section,
.em-layout-list .em-schedule-section {
    border-left: 3px solid var(--em-primary);
    border-radius: 0 var(--em-border-radius) var(--em-border-radius) 0;
    margin-left: 1rem;
}

/* Component-specific layout adjustments */
.em-layout-compact .em-grid-cols-4 {
    grid-template-columns: repeat(2, 1fr);
}

.em-layout-compact .em-grid-cols-3 {
    grid-template-columns: repeat(2, 1fr);
}

.em-layout-list .em-grid-cols-4,
.em-layout-list .em-grid-cols-3 {
    grid-template-columns: 1fr;
}

.em-layout-modal .em-grid-cols-4 {
    grid-template-columns: repeat(2, 1fr);
}

/* Animation delays for staggered entrance */
.em-layout-default > *:nth-child(1) { animation-delay: 0ms; }
.em-layout-default > *:nth-child(2) { animation-delay: 100ms; }
.em-layout-default > *:nth-child(3) { animation-delay: 200ms; }
.em-layout-default > *:nth-child(4) { animation-delay: 300ms; }
.em-layout-default > *:nth-child(5) { animation-delay: 400ms; }
.em-layout-default > *:nth-child(6) { animation-delay: 500ms; }

/* Print layout adjustments */
@media print {
    .em-layout-modal,
    .em-layout-wide,
    .em-layout-default {
        max-width: none !important;
        max-height: none !important;
        overflow: visible !important;
    }
    
    .em-layout-list .em-registration-section,
    .em-layout-list .em-gallery-section,
    .em-layout-list .em-speakers-section,
    .em-layout-list .em-organizers-section,
    .em-layout-list .em-sponsors-section,
    .em-layout-list .em-schedule-section {
        border-left: none;
        margin-left: 0;
        page-break-inside: avoid;
    }
}
</style>

<script>
jQuery(document).ready(function($) {
    // Apply layout classes to the main container
    const layoutClasses = '<?php echo esc_js($all_layout_classes); ?>';
    $('.awl_em_main_div_<?php echo esc_attr($event_id); ?>').addClass(layoutClasses);
    
    // Layout-specific JavaScript enhancements
    const layoutMode = '<?php echo esc_js($em_layout_mode); ?>';
    
    if (layoutMode === 'modal') {
        // Add modal-specific behaviors
        $('.awl_em_main_div_<?php echo esc_attr($event_id); ?>').addClass('em-scrollbar');
        
        // Add escape key handler for modal
        $(document).on('keydown', function(e) {
            if (e.key === 'Escape' && $('.em-layout-modal').length) {
                // Trigger modal close event (if parent has modal functionality)
                $(document).trigger('em-modal-close');
            }
        });
    }
    
    if (layoutMode === 'list') {
        // Add list-specific behaviors
        $('.em-registration-section, .em-gallery-section, .em-speakers-section, .em-organizers-section, .em-sponsors-section, .em-schedule-section').each(function(index) {
            $(this).css('animation-delay', (index * 100) + 'ms');
        });
    }
    
    // Add layout change functionality (for dynamic switching)
    window.emChangeLayout = function(newMode, newStyle, newDensity) {
        const $container = $('.awl_em_main_div_<?php echo esc_attr($event_id); ?>');
        
        // Remove existing layout classes
        $container.removeClass(function(index, className) {
            return (className.match(/(^|\s)em-(layout|style|density)-\S+/g) || []).join(' ');
        });
        
        // Add new layout classes
        const newClasses = [];
        if (newMode) newClasses.push('em-layout-' + newMode);
        if (newStyle) newClasses.push('em-style-' + newStyle);
        if (newDensity) newClasses.push('em-density-' + newDensity);
        
        $container.addClass(newClasses.join(' '));
        
        // Trigger layout change event
        $(document).trigger('em-layout-changed', {
            mode: newMode,
            style: newStyle,
            density: newDensity
        });
    };
    
    console.log('Layout Manager initialized with mode: ' + layoutMode);
});
</script>
