<?php
/**
 * Video Help Helper Functions
 * Functions to manage and display video tutorials throughout the application
 */

/**
 * Get video help data for a specific page
 * @param string $page_name The page identifier (e.g., 'item_list', 'pos')
 * @param mysqli $conn Database connection
 * @return array|false Video data or false if not found
 */
function get_video_help($page_name, $conn) {
    $page_name = $conn->real_escape_string($page_name);
    $result = $conn->query("SELECT * FROM video_help WHERE page_name = '$page_name' AND is_active = 1");
    return $result->fetch_assoc();
}

/**
 * Display video help button for a page
 * @param string $page_name The page identifier
 * @param mysqli $conn Database connection
 * @param string $button_text Custom button text (optional)
 * @param string $button_class Custom CSS class (optional)
 * @return string HTML for video help button or empty string if no video found
 */
function display_video_help_button($page_name, $conn, $button_text = null, $button_class = 'btn-outline-info') {
    $video = get_video_help($page_name, $conn);
    
    if (!$video) {
        return '';
    }
    
    $text = $button_text ?: '<i class="fa fa-play-circle"></i> Watch Video';
    $class = $button_class ?: 'btn-outline-info';
    
    return sprintf(
        '<button type="button" class="btn btn-sm %s" onclick="showVideoHelp(\'%s\', \'%s\', \'%s\')" title="%s">
            %s
        </button>',
        $class,
        htmlspecialchars($video['video_url']),
        htmlspecialchars($video['video_title']),
        htmlspecialchars($video['description']),
        htmlspecialchars($video['video_title']),
        $text
    );
}

/**
 * Display video help button with icon only
 * @param string $page_name The page identifier
 * @param mysqli $conn Database connection
 * @param string $icon_class Custom icon class (optional)
 * @param string $button_class Custom CSS class (optional)
 * @return string HTML for video help button or empty string if no video found
 */
function display_video_help_icon($page_name, $conn, $icon_class = 'fa-play-circle', $button_class = 'btn-outline-info') {
    $video = get_video_help($page_name, $conn);
    
    if (!$video) {
        return '';
    }
    
    return sprintf(
        '<button type="button" class="btn btn-sm %s" onclick="showVideoHelp(\'%s\', \'%s\', \'%s\')" title="%s">
            <i class="fa %s"></i>
        </button>',
        $button_class,
        htmlspecialchars($video['video_url']),
        htmlspecialchars($video['video_title']),
        htmlspecialchars($video['description']),
        htmlspecialchars($video['video_title']),
        $icon_class
    );
}

/**
 * Display video help modal HTML (include this once per page)
 * @return string HTML for video help modal
 */
function display_video_help_modal() {
    return '
    <!-- Video Help Modal -->
    <div class="modal fade" id="videoHelpModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="videoHelpTitle">Video Tutorial</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div id="videoHelpDescription" class="alert alert-info mb-3"></div>
                    <div class="ratio ratio-16x9">
                        <iframe id="videoHelpFrame" src="" frameborder="0" allowfullscreen></iframe>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    
    <script>
    function showVideoHelp(videoUrl, videoTitle, description) {
        // Set modal content
        document.getElementById("videoHelpTitle").textContent = videoTitle;
        document.getElementById("videoHelpDescription").textContent = description;
        
        // Convert YouTube URL to embed format if needed
        let embedUrl = videoUrl;
        if (videoUrl.includes("youtube.com/watch")) {
            const videoId = videoUrl.split("v=")[1];
            if (videoId) {
                embedUrl = "https://www.youtube.com/embed/" + videoId;
            }
        } else if (videoUrl.includes("youtu.be/")) {
            const videoId = videoUrl.split("youtu.be/")[1];
            if (videoId) {
                // Remove any query parameters
                const cleanVideoId = videoId.split("?")[0];
                embedUrl = "https://www.youtube.com/embed/" + cleanVideoId;
            }
        } else if (videoUrl.includes("vimeo.com")) {
            const videoId = videoUrl.split("vimeo.com/")[1];
            if (videoId) {
                embedUrl = "https://player.vimeo.com/video/" + videoId;
            }
        }
        
        document.getElementById("videoHelpFrame").src = embedUrl;
        
        // Show modal
        new bootstrap.Modal(document.getElementById("videoHelpModal")).show();
    }
    
    // Clear video when modal is closed
    document.getElementById("videoHelpModal").addEventListener("hidden.bs.modal", function () {
        document.getElementById("videoHelpFrame").src = "";
    });
    </script>';
}

/**
 * Check if video help exists for a page
 * @param string $page_name The page identifier
 * @param mysqli $conn Database connection
 * @return bool True if video help exists and is active
 */
function has_video_help($page_name, $conn) {
    $video = get_video_help($page_name, $conn);
    return $video !== false;
}

/**
 * Get all active video help entries
 * @param mysqli $conn Database connection
 * @return mysqli_result Result set of all active video help entries
 */
function get_all_video_help($conn) {
    return $conn->query("SELECT * FROM video_help WHERE is_active = 1 ORDER BY page_title");
}
?> 