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

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

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

/**
 * Initialize scroll to top feature based on theme settings
 */
function axvart_scroll_top_init() {
    // Get site settings
    $scroll_top_enabled = get_option( 'axvart_scroll_top', false );
    
    // Only enable if explicitly set to 1, '1', or true
    // This ensures OFF (0, '0', false, null, empty) is treated as disabled
    if ( $scroll_top_enabled == 1 || $scroll_top_enabled === true ) {
        add_action( 'wp_enqueue_scripts', 'axvart_scroll_top_assets', 20 );
        add_action( 'wp_footer', 'axvart_render_scroll_top' );
    }
}

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

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

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

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

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