<?php
namespace HTMega_Reading_Progress_Bar_Pro;

use Elementor\Controls_Manager;

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

/**
 * Sitewide auto-enable for the Reading Progress Bar — 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/reading-progress-bar/class.reading-progress-bar.php) and
 * work fully standalone without this class.
 */
class HTMegaReadingProgressBarPro_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 ) {
        $rpbar_global = $this->get_global_setting( 'rpbar_global' );

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

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

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

        $element->add_control(
            'htmega_rpbar_disable',
            [
                'label'        => __( 'Disable Reading Progress Bar', 'htmega-pro' ),
                'description'  => __( 'Disable Reading Progress Bar 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 bar 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['rpbar_enable'] ?? '' )
            || 'on' !== ( $settings['rpbar_global'] ?? '' )
        ) {
            return;
        }

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

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

        if ( ! $should_load ) {
            return;
        }

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

        $rpbar_localize_data = [
            'bg_color'       => isset( $settings['rpbar_background_color'] ) ? $settings['rpbar_background_color'] : 'transparent',
            'fill_color'     => isset( $settings['rpbar_fill_color'] ) ? $settings['rpbar_fill_color'] : '#fill_color',
            'loading_height' => isset( $settings['rpbar_loading_height'] ) ? $settings['rpbar_loading_height'] : 5,
            'position'       => isset( $settings['rpbar_position'] ) ? $settings['rpbar_position'] : 'top',
        ];

        wp_localize_script( 'htmega-rpbar-script', 'rpbar', $rpbar_localize_data );
    }

    private function get_module_settings() {
        $settings = htmega_get_option( 'htmega_rpbar', 'htmega_rpbar_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';
    }
}

HTMegaReadingProgressBarPro_Elementor::instance();
