<?php
/**
 * Scroll Top Feature
 *
 * @package Eryndo
 */

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

// Set up scroll top hooks based on settings
add_action( 'wp', 'eryndo_scroll_top_init' );

/**
 * Initialize scroll to top feature based on theme settings
 */
function eryndo_scroll_top_init() {
    // Get site settings
    $scroll_top_enabled = get_option( 'eryndo_scroll_top', false );
    
    // Only enable the feature if the option is set to true
    if ( $scroll_top_enabled ) {
        add_action( 'wp_enqueue_scripts', 'eryndo_scroll_top_assets', 20 );
        add_action( 'wp_footer', 'eryndo_render_scroll_top' );
    }
}

/**
 * Enqueue Scroll Top CSS and JS.
 */
function eryndo_scroll_top_assets() {
    $version = wp_get_theme()->get( 'Version' );

    // Register a dummy script handle to attach inline JS
    wp_register_script( 'eryndo-scroll-top', '', array(), $version, true );
    wp_enqueue_script( 'eryndo-scroll-top' );

    // Inline JS for smooth scroll and show/hide
    $inline_js = "(function(){
        function docReady(fn) {
            if (document.readyState === 'complete' || document.readyState === 'interactive') {
                setTimeout(fn, 1);
            } else {
                document.addEventListener('DOMContentLoaded', fn);
            }
        }
        docReady(function() {
            var scrollTopBtn = document.querySelector('.eryndo-scroll-top');
            if (! scrollTopBtn) return;
            scrollTopBtn.addEventListener('click', function() {
                window.scrollTo({top: 0, behavior: 'smooth'});
            });
        });

        window.addEventListener('scroll', function() {
            var scrollTopBtn = document.querySelector('.eryndo-scroll-top');
            if (! scrollTopBtn) return;
            scrollTopBtn.style.transform = window.scrollY >= 100 ? 'scale(1)' : 'scale(0)';
        }, true);
    })();";
    wp_add_inline_script( 'eryndo-scroll-top', $inline_js );

    // Inline CSS for button styling and animation (uses Eryndo presets)
    $css  = ".eryndo-scroll-top{display:flex;align-items:center;justify-content:center;position:fixed;right:var(--wp--preset--spacing--30);bottom:var(--wp--preset--spacing--30);width:var(--wp--preset--spacing--40);height:var(--wp--preset--spacing--40);background-color:var(--wp--preset--color--contrast);border-radius:50%;cursor:pointer;transform:scale(0);transition:.2s;z-index:9999;}";
    $css .= ".eryndo-scroll-top svg path{stroke:var(--wp--preset--color--base);fill:none;}";
    wp_add_inline_style( 'eryndo-style', $css );
}

/**
 * Render Scroll Top Button markup.
 */
function eryndo_render_scroll_top() {
    echo '<div class="eryndo-scroll-top" aria-label="' . esc_attr__( 'Scroll to top', 'eryndo' ) . '">';
    echo '<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">';
    echo '<path d="M18 15L12 9L6 15" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>'; 
    echo '</svg>';
    echo '</div>';
} 