<?php
namespace HTMega_Scroll_To_Top_Pro;

use Elementor\Controls_Manager;

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

/**
 * Sitewide auto-enable for Scroll To Top — a pro-only convenience so
 * users don't have to flip the per-page toggle on every page. Per-page
 * enable/style controls live in the free plugin
 * (extensions/scroll-to-top/class.scroll-to-top.php) and work fully
 * standalone without this class.
 */
class HTMegaScrollToTopPro_Elementor {

    private static $_instance = null;
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function __construct() {
        add_action( 'elementor/documents/register_controls', [ $this, 'register_controls' ], 11 );
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ], 20 );
    }

    /**
     * Injects a "Disable for this page" control into the free plugin's
     * already-built section, only relevant when the sitewide global
     * toggle is on. Elementor's start_injection/end_injection is the
     * supported API for extending a section registered by other code.
     */
    public function register_controls( $element ) {
        $stt_global = $this->get_global_setting( 'stt_global' );

        if ( 'on' !== $stt_global ) {
            return;
        }

        $section = $element->get_controls( 'section_htmega_stt_section' );
        if ( ! $section ) {
            return;
        }

        $element->start_injection( [
            'of' => 'htmega_stt_enable',
        ] );

        $element->add_control(
            'htmega_stt_disable',
            [
                'label'        => __( 'Disable Scroll To Top', 'htmega-pro' ),
                'description'  => __( 'Disable Scroll To Top for this page', 'htmega-pro' ),
                'type'         => Controls_Manager::SWITCHER,
                'default'      => 'no',
                'label_on'     => __( 'Yes', 'htmega-pro' ),
                'label_off'    => __( 'No', 'htmega-pro' ),
                'return_value' => 'yes',
            ]
        );

        $element->end_injection();
    }

    /**
     * Enqueue the button sitewide when the global toggle is on. Script/style
     * handles are registered by the free plugin's asset registration,
     * which runs earlier on the same wp_enqueue_scripts hook (priority 10
     * vs this class's priority 20).
     */
    public function enqueue_scripts() {
        $settings = $this->get_module_settings();

        if ( ! $settings
            || 'on' !== ( $settings['stt_enable'] ?? '' )
            || 'on' !== ( $settings['stt_global'] ?? '' )
        ) {
            return;
        }

        $stt_select_to_show_pages = isset( $settings['stt_select_to_show_pages'] ) ? $settings['stt_select_to_show_pages'] : 'all';

        $should_load = ( 'all' === $stt_select_to_show_pages && ( is_single() || is_page() ) )
            || ( 'pages' === $stt_select_to_show_pages && is_page() )
            || ( 'posts' === $stt_select_to_show_pages && is_single() );

        if ( ! $should_load ) {
            return;
        }

        wp_enqueue_script( 'htmega-stt-script' );
        wp_enqueue_style( 'htmega-stt-css' );

        $stt_localize_data = [
            'stt_bg_color'       => isset( $settings['stt_bg_color'] ) ? $settings['stt_bg_color'] : '#000000',
            'stt_color'          => isset( $settings['stt_color'] ) ? $settings['stt_color'] : '#ffffff',
            'stt_bg_color_hover' => isset( $settings['stt_bg_color_hover'] ) ? $settings['stt_bg_color_hover'] : '#000000',
            'stt_color_hover'    => isset( $settings['stt_color_hover'] ) ? $settings['stt_color_hover'] : '#ffffff',
            'position'           => isset( $settings['stt_position'] ) ? $settings['stt_position'] : 'right',
            'stt_bottom_space'   => isset( $settings['stt_bottom_space'] ) ? $settings['stt_bottom_space'] : 30,
        ];

        wp_localize_script( 'htmega-stt-script', 'stt', $stt_localize_data );
    }

    private function get_module_settings() {
        $settings = htmega_get_option( 'htmega_stt', 'htmega_stt_module_settings' );
        return json_decode( $settings, true );
    }

    private function get_global_setting( $key ) {
        $settings = $this->get_module_settings();
        return isset( $settings[ $key ] ) ? $settings[ $key ] : 'off';
    }
}

HTMegaScrollToTopPro_Elementor::instance();
