<?php

// Hook cursor HTML into <head>
add_action('wp_head', 'px_cursor_html', 9999);

// Hook cursor JS into footer
add_action('wp_footer', 'px_cursor_js', 9999);

// Function to output cursor HTML
function px_cursor_html() {

    // Prevent execution inside Divi Builder
    if (defined('ET_FB_ENABLED') && ET_FB_ENABLED) return;
    
    // Get cursor enable/disable option
    $enable = et_get_option('divi_PX_cursor_enable', 'off');
    
    // Proceed only if enabled and not in admin panel
    if ($enable == 'on' && !is_admin()) {
        
        // Get available cursor styles
        $px_cursor_images = px_get_cursor_styles();
        
        // Get selected cursor from options
        $selected = get_option('divi_cursor_images'); 
        
        // Fallback to default cursor if not valid
        if (empty($selected) || !isset($px_cursor_images[$selected])) {
            $selected = 'px_cursor2'; 
        }
        
        // Get cursor customization options
        $divi_PX_cursor_size = et_get_option('divi_PX_cursor_size', '40');
        $divi_PX_cursor_border_color = et_get_option('divi_PX_cursor_border', '#000000');
        $divi_PX_cursor_background_color = et_get_option('divi_PX_cursor_bg', 'transparent');

        // Include selected cursor template file if exists
        if (isset($px_cursor_images[$selected])) {
            $file_path = get_stylesheet_directory() . '/extra-options/cursor/' . $selected . '.php';
            
            if (file_exists($file_path)) {
                include $file_path;
            }
        }
    }
}

// Function to output cursor JavaScript
function px_cursor_js() {

    // Prevent execution inside Divi Builder
    if (defined('ET_FB_ENABLED') && ET_FB_ENABLED) return;
    
    // Get cursor enable option (default ON here)
    $enable = et_get_option('divi_PX_cursor_enable', 'on');
    
    // Proceed only if enabled and not in admin panel
    if ($enable == 'on' && !is_admin()) {
        ?>
        <script type="text/javascript">
            (function($) {

                // Run when DOM is ready
                $(document).ready(function() {

                    // Select cursor element
                    const cursor = document.querySelector('.px_cursor');

                    // Exit if cursor not found
                    if (!cursor) return;
                    
                    // Initialize mouse and cursor positions (center of screen)
                    let mx = window.innerWidth / 2, my = window.innerHeight / 2;
                    let px = mx, py = my;
                    
                    // Track mouse movement
                    $(document).on("mousemove", function(e) {
                        mx = e.clientX;
                        my = e.clientY;

                        // Show cursor on movement
                        cursor.style.opacity = "1";
                        cursor.style.display = "block";
                    });
                    
                    // Smooth animation loop for cursor movement
                    function animate() {

                        // Apply easing for smooth follow effect
                        px += (mx - px) * 0.15;
                        py += (my - py) * 0.15;

                        // Update cursor position
                        cursor.style.left = px + "px";
                        cursor.style.top  = py + "px";

                        // Continue animation
                        requestAnimationFrame(animate);
                    }

                    // Start animation loop
                    animate();
                    
                    // Hide cursor when mouse leaves window
                    $(document).on("mouseleave", function() {
                        cursor.style.opacity = "0";
                    });
                });

            })(jQuery);
        </script>
        <?php
    }
}
?>