<?php

/**
 * Enqueue all the necessary scripts and styles required by the plugin.
 *
 * @package Flexible_FAQs
 */
namespace WPGO_Plugins\Flexible_Faqs;

/**
 * Enqueue plugin scripts.
 */
class Enqueue_Scripts
{
    /**
     * Common root paths/directories.
     *
     * @var $module_roots
     */
    protected  $module_roots ;
    /**
     * Main class constructor.
     *
     * @param Array $module_roots Root plugin path/dir.
     * @param Array $new_features_arr New features data.
     * @param Array $plugin_data Plugin data.
     * @param Array $custom_plugin_data Custom plugin data.
     */
    public function __construct(
        $module_roots,
        $new_features_arr,
        $plugin_data,
        $custom_plugin_data,
        $utilities_fw
    )
    {
        $this->module_roots = $module_roots;
        $this->new_features_arr = $new_features_arr;
        $this->plugin_data = $plugin_data;
        $this->custom_plugin_data = $custom_plugin_data;
        $this->utilities_fw = $utilities_fw;
        $this->enq_pfx = $this->custom_plugin_data->enqueue_prefix;
        $this->text_expand_attribute_defaults = $this->custom_plugin_data->shortcode_text_expand_attribute_defaults;
        $this->box_expand_attribute_defaults = $this->custom_plugin_data->shortcode_box_expand_attribute_defaults;
        $this->accordion_expand_attribute_defaults = $this->custom_plugin_data->shortcode_accordion_expand_attribute_defaults;
        $this->block_text_expand_attribute_defaults = $this->custom_plugin_data->block_text_expand_attribute_defaults;
        $this->block_box_expand_attribute_defaults = $this->custom_plugin_data->block_box_expand_attribute_defaults;
        $this->block_accordion_expand_attribute_defaults = $this->custom_plugin_data->block_accordion_expand_attribute_defaults;
        $this->plugin_settings = get_option( $this->custom_plugin_data->db_option_prefix . '_options', array() );
        $this->load_scripts_everywhere = ( array_key_exists( 'load-faq-scripts', $this->plugin_settings ) ? intval( $this->plugin_settings['load-faq-scripts'] ) : 0 );
        // $this->frontend_assets = array(
        // 'css' => array(),
        // 'js'  => array(),
        // );
        // $rel                           = 'assets/js/flexible-faqs.js';
        // $this->frontend_assets['js'][] = array(
        // 'url'    => plugins_url( $rel, $this->module_roots['file'] ),
        // 'ver'    => filemtime( $this->module_roots['dir'] . $rel ),
        // 'handle' => $this->enq_pfx . '-plugin-js',
        // );
        // $rel                            = 'assets/css/flexible-faqs.css';
        // $this->frontend_assets['css'][] = array(
        // 'url'    => plugins_url( $rel, $this->module_roots['file'] ),
        // 'ver'    => filemtime( $this->module_roots['dir'] . $rel ),
        // 'handle' => $this->enq_pfx . '-plugin-css',
        // );
        // // Pro only styles and scripts.
        // if ( flexible_faqs_fs()->can_use_premium_code__premium_only() ) {
        // $rel                           = 'classes/modules/js/flexible-faqs-pro.js';
        // $this->frontend_assets['js'][] = array(
        // 'url'    => plugins_url( $rel, $this->module_roots['file'] ),
        // 'ver'    => filemtime( $this->module_roots['dir'] . $rel ),
        // 'handle' => $this->enq_pfx . '-plugin-js-pro',
        // );
        // }
        // $this->js_deps = [ 'wp-element', 'wp-i18n', 'wp-hooks', 'wp-components', 'wp-blocks', 'wp-editor', 'wp-compose' ];
        $this->js_deps = array(
            'wp-plugins',
            'wp-element',
            'wp-edit-post',
            'wp-i18n',
            'wp-api-request',
            'wp-data',
            'wp-hooks',
            'wp-plugins',
            'wp-components',
            'wp-blocks',
            'wp-editor',
            'wp-compose'
        );
        add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_admin_settings_scripts' ) );
        add_action(
            'admin_enqueue_scripts',
            array( &$this, 'enqueue_admin_scripts' ),
            9,
            2
        );
        add_action( 'enqueue_block_assets', array( &$this, 'enqueue_block_scripts' ) );
        add_action( 'enqueue_block_editor_assets', array( &$this, 'enqueue_block_editor_scripts' ) );
        // add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
        // add_filter( 'widget_text', array( &$this, 'enqueue_widget_scripts' ), 10, 1 );
    }
    
    /**
     * Scripts for all admin pages. This is necessary as we need to modify the main admin menu from JS.
     *
     * @param String $hook Passed as parameter.
     */
    public function enqueue_admin_scripts( $hook )
    {
        // $all_admin_pages_js_url = plugins_url($all_admin_pages_js_rel, $this->module_roots['file']);
        // $all_admin_pages_js_ver = filemtime($this->module_roots['dir'] . $all_admin_pages_js_rel);
        $admin_settings_js = $this->utilities_fw->get_enqueue_version( '/assets/js/update-menu.js', $this->custom_plugin_data->plugin_data['Version'] );
        $opt_pfx = $this->custom_plugin_data->db_option_prefix;
        //$new_features_number = \WPGO_Plugins\Plugin_Framework\Upgrade_FW::calc_new_features( $opt_pfx, $this->new_features_arr, $this->plugin_data );
        // Register and localize the script with new data.
        //wp_register_script( $this->enq_pfx . '-update-menu-js', $admin_settings_js['uri'], array( 'wpgo-all-admin-pages-fw-js' ), $admin_settings_js['ver'], true );
        wp_register_script(
            $this->enq_pfx . '-update-menu-js',
            $admin_settings_js['uri'],
            array(),
            $admin_settings_js['ver'],
            true
        );
        $data = array(
            'admin_url'       => admin_url(),
            'nav_status'      => FFAQ_FREEMIUS_NAVIGATION,
            'hook'            => $hook,
            'menu_type'       => $this->custom_plugin_data->menu_type,
            'main_menu_label' => $this->custom_plugin_data->main_menu_label,
            'plugin_prefix'   => $this->enq_pfx,
        );
        // Keep the handle generic so only one instance is enqueued (if multiple WPGO plugins are installed).
        // wp_enqueue_script('wpgo-all-admin-pages-fw-js', $all_admin_pages_js_url, array(), $all_admin_pages_js_ver, true);
        wp_localize_script( $this->enq_pfx . '-update-menu-js', $this->custom_plugin_data->plugin_settings_prefix . '_admin_menu_data', $data );
        wp_enqueue_script( $this->enq_pfx . '-update-menu-js' );
    }
    
    /**
     * Scripts just for the plugin settings page(s).
     *
     * @param String $hook Passed as parameter.
     */
    public function enqueue_admin_settings_scripts( $hook )
    {
        // Don't try to enqueue if $_GET['page'] not set.
        if ( !isset( $_GET['page'] ) ) {
            return;
        }
        
        if ( 'sub' === $this->custom_plugin_data->menu_type ) {
            // Only enqueue scripts on the plugin settings page(s) (and Freemius pages).
            $pos = strpos( $hook, $this->custom_plugin_data->settings_page_hook );
            if ( 0 !== $pos ) {
                return;
            }
        } else {
            // Only enqueue scripts on the plugin settings page(s) (and Freemius pages).
            $pos = strpos( $hook, $this->custom_plugin_data->settings_page_hook_top );
            $pos1 = strpos( $hook, $this->custom_plugin_data->settings_page_hook_sub );
            // Return if at least one values doesn't match.
            if ( 0 !== $pos && 0 !== $pos1 ) {
                return;
            }
        }
        
        $admin_settings_js = $this->utilities_fw->get_enqueue_version( '/assets/js/admin-settings.js', $this->custom_plugin_data->plugin_data['Version'] );
        $admin_settings_css = $this->utilities_fw->get_enqueue_version( '/assets/css/admin-settings.css', $this->custom_plugin_data->plugin_data['Version'] );
        // Register and localize the script with new data.
        wp_register_script(
            $this->enq_pfx . '-admin-settings-js',
            $admin_settings_js['uri'],
            array(),
            $admin_settings_js['ver'],
            true
        );
        $data = array(
            'admin_url'     => admin_url(),
            'settings_page' => $_GET['page'],
            'nav_status'    => FFAQ_FREEMIUS_NAVIGATION,
        );
        wp_localize_script( $this->enq_pfx . '-admin-settings-js', $this->custom_plugin_data->plugin_settings_prefix . '_admin_data', $data );
        wp_enqueue_script( $this->enq_pfx . '-admin-settings-js' );
        // Styles for plugin admin settings page.
        wp_enqueue_style(
            $this->enq_pfx . '-admin-settings-css',
            $admin_settings_css['uri'],
            array(),
            $admin_settings_css['ver']
        );
    }
    
    /**
     * Enqueue scripts on all frontend pages if page content contains an FAQ block/shortcode.
     */
    // public function enqueue_scripts() {
    // return;
    // global $post;
    // if ( 1 != $this->load_scripts_everywhere && is_archive() ) {
    // return;
    // }
    // Include scripts only if block/shortcode is included in post content.
    // if (
    // 1 == $this->load_scripts_everywhere
    // || has_block( 'flexible-faqs/box-expand-faqs' )
    // || has_block( 'flexible-faqs/text-expand-faqs' )
    // || has_block( 'flexible-faqs/accordion-expand-faqs' )
    // || has_shortcode( $post->post_content, 'flexible-faqs' )
    // || has_shortcode( $post->post_content, 'submit-flexible-faq' )
    // || has_shortcode( $post->post_content, 'submit-faq' )
    // ) {
    // Main plugin JS and CSS.
    // foreach ( $this->frontend_assets['js'] as $script ) {
    // wp_enqueue_script( $script['handle'], $script['url'], array( 'jquery' ), $script['ver'], true );
    // }
    // foreach ( $this->frontend_assets['css'] as $style ) {
    // wp_enqueue_style( $style['handle'], $style['url'], array(), $style['ver'] );
    // }
    // }
    // }
    /**
     * I THINK THIS CAN BE DELETED NOW, AS WE HAVE THE PLUGIN SETTING TO ENQUEUE SCRIPTS ON ALL PAGES?
     *
     * Enqueue scripts on all frontend pages if any widget contains an FAQ shortcode.
     *
     * @param String $content Content of the widget passed as string parameter.
     * @return String Contents of the widget.
     */
    // public function enqueue_widget_scripts( $content = '' ) {
    // Include scripts only if shortcode is present in widget contents.
    // if ( has_shortcode( $content, 'flexible-faqs' ) || has_shortcode( $content, 'submit-faq' ) || has_shortcode( $content, 'submit-flexible-faq' ) ) {
    // foreach ( $this->frontend_assets['js'] as $script ) {
    // wp_enqueue_script( $script['handle'], $script['url'], array( 'jquery' ), $script['ver'], true );
    // }
    // foreach ( $this->frontend_assets['css'] as $style ) {
    // wp_enqueue_style( $style['handle'], $style['url'], array(), $style['ver'] );
    // }
    // }
    // return $content;
    // }
    /**
     * Add scripts for block editor only. These are always added to the block editor.
     * It's not an issue however as other related FAQ block code scripts are always loaded too.
     * This is the same as WP core blocks, which are always loaded too.
     */
    public function enqueue_block_editor_scripts()
    {
        $frontend_js = $this->utilities_fw->get_enqueue_version( '/assets/js/flexible-faqs.js', $this->custom_plugin_data->plugin_data['Version'] );
        wp_enqueue_script(
            $this->enq_pfx . '-plugin-js',
            $frontend_js['uri'],
            array(),
            $frontend_js['ver'],
            true
        );
        $frontend_css = $this->utilities_fw->get_enqueue_version( '/assets/css/flexible-faqs.css', $this->custom_plugin_data->plugin_data['Version'] );
        wp_enqueue_style(
            $this->enq_pfx . '-plugin-css',
            $frontend_css['uri'],
            array(),
            $frontend_css['ver']
        );
        $deps = $this->js_deps;
        // Block editor script.
        $block_editor_js = $this->utilities_fw->get_enqueue_version( '/assets/js/block.editor.js', $this->custom_plugin_data->plugin_data['Version'] );
        $block_editor_css = $this->utilities_fw->get_enqueue_version( '/assets/css/block.editor.styles.css', $this->custom_plugin_data->plugin_data['Version'] );
        wp_register_script(
            $this->enq_pfx . '-block-editor-js',
            $block_editor_js['uri'],
            $deps,
            $block_editor_js['ver'],
            true
        );
        $font_families = Utility::get_font_families();
        $data = array(
            'text_expand'            => $this->text_expand_attribute_defaults,
            'box_expand'             => $this->box_expand_attribute_defaults,
            'accordion_expand'       => $this->accordion_expand_attribute_defaults,
            'block_text_expand'      => $this->block_text_expand_attribute_defaults,
            'block_box_expand'       => $this->block_box_expand_attribute_defaults,
            'block_accordion_expand' => $this->block_accordion_expand_attribute_defaults,
            'expand_icons'           => $this->custom_plugin_data->expand_icons,
            'is_premium'             => $this->custom_plugin_data->is_premium,
            'font_families'          => wp_json_encode( $font_families ),
        );
        wp_localize_script( $this->enq_pfx . '-block-editor-js', $this->custom_plugin_data->plugin_settings_prefix . '_editor_data', $data );
        wp_enqueue_script( $this->enq_pfx . '-block-editor-js' );
        // Block editor styles.
        wp_enqueue_style(
            $this->enq_pfx . '-block-editor-css',
            $block_editor_css['uri'],
            array(),
            $block_editor_css['ver']
        );
    }
    
    /**
     * Add scripts for frontend and block editor.
     */
    public function enqueue_block_scripts()
    {
    }

}
/* End class definition */