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

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

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

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

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

    // Register a dummy script handle to attach inline JS
    wp_register_script( 'cryzel-scroll-top', '', array(), $version, true );
    wp_enqueue_script( 'cryzel-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('.cryzel-scroll-top');
            if (! scrollTopBtn) return;
            scrollTopBtn.addEventListener('click', function() {
                window.scrollTo({top: 0, behavior: 'smooth'});
            });
        });

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

    // Inline CSS for button styling and animation (uses Cryzel presets)
    $css  = ".cryzel-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 .= ".cryzel-scroll-top svg path{stroke:var(--wp--preset--color--base);fill:none;}";
    wp_add_inline_style( 'cryzel-style', $css );
}

/**
 * Render Scroll Top Button markup.
 */
function cryzel_render_scroll_top() {
    echo '<div class="cryzel-scroll-top" aria-label="' . esc_attr__( 'Scroll to top', 'cryzel' ) . '">';
    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>';
} 