/**
 * Mobile Sidebar Utility for HNF Resort Management System
 * Handles mobile sidebar toggle functionality across all pages
 */

$(document).ready(function() {
    initializeMobileSidebar();
});

function initializeMobileSidebar() {
    // Check if we're on mobile
    function isMobile() {
        return $(window).width() <= 768;
    }

    // Mobile sidebar toggle button
    $(document).on('click', '#mobileSidebarToggle', function(e) {
        e.preventDefault();
        e.stopPropagation();
        
        if (!isMobile()) return;
        
        const sidebar = $('.sidebar');
        const overlay = $('#sidebarMobileOverlay, .sidebar-mobile-overlay');
        
        if (sidebar.hasClass('show')) {
            closeMobileSidebar();
        } else {
            openMobileSidebar();
        }
    });

    // Hide sidebar when clicking overlay
    $(document).on('click', '#sidebarMobileOverlay, .sidebar-mobile-overlay', function() {
        closeMobileSidebar();
    });

    // Hide sidebar when clicking outside on mobile
    $(document).on('click', function(e) {
        if (!isMobile()) return;
        
        const sidebar = $('.sidebar');
        
        // Check if click is outside sidebar and toggle button
        if (!$(e.target).closest('.sidebar, #mobileSidebarToggle, .mobile-header').length) {
            if (sidebar.hasClass('show')) {
                closeMobileSidebar();
            }
        }
    });

    // Close sidebar when window is resized to desktop
    $(window).resize(function() {
        if (!isMobile()) {
            closeMobileSidebar();
        }
    });

    // Handle sidebar navigation clicks on mobile
    $(document).on('click', '.sidebar .nav-link', function() {
        if (isMobile()) {
            // Small delay to allow navigation to start
            setTimeout(closeMobileSidebar, 100);
        }
    });

    // Handle escape key to close sidebar
    $(document).keyup(function(e) {
        if (e.keyCode === 27 && isMobile()) { // Escape key
            closeMobileSidebar();
        }
    });

    // Swipe gesture support for mobile
    let touchStartX = 0;
    let touchEndX = 0;

    $(document).on('touchstart', function(e) {
        touchStartX = e.originalEvent.changedTouches[0].screenX;
    });

    $(document).on('touchend', function(e) {
        if (!isMobile()) return;
        
        touchEndX = e.originalEvent.changedTouches[0].screenX;
        const sidebar = $('.sidebar');
        
        // Swipe right to open sidebar (from left edge)
        if (touchStartX < 50 && touchEndX - touchStartX > 100) {
            if (!sidebar.hasClass('show')) {
                openMobileSidebar();
            }
        }
        
        // Swipe left to close sidebar
        if (sidebar.hasClass('show') && touchStartX - touchEndX > 100) {
            closeMobileSidebar();
        }
    });
}

function openMobileSidebar() {
    const sidebar = $('.sidebar');
    const overlay = $('#sidebarMobileOverlay, .sidebar-mobile-overlay');
    
    sidebar.addClass('show');
    overlay.addClass('show');
    
    // Prevent body scroll on mobile
    if ($(window).width() <= 768) {
        $('body').css('overflow', 'hidden');
    }
    
    // Add accessibility attributes
    sidebar.attr('aria-hidden', 'false');
    $('#mobileSidebarToggle').attr('aria-expanded', 'true');
}

function closeMobileSidebar() {
    const sidebar = $('.sidebar');
    const overlay = $('#sidebarMobileOverlay, .sidebar-mobile-overlay');
    
    sidebar.removeClass('show');
    overlay.removeClass('show');
    
    // Restore body scroll
    $('body').css('overflow', '');
    
    // Add accessibility attributes
    sidebar.attr('aria-hidden', 'true');
    $('#mobileSidebarToggle').attr('aria-expanded', 'false');
}

// Export functions for use in other scripts
window.mobileSidebar = {
    open: openMobileSidebar,
    close: closeMobileSidebar,
    initialize: initializeMobileSidebar
};