<?php

/**
 * Blynex Block Assets
 *
 * This file handles the registration and enqueuing of theme assets,
 * including styles and scripts for both the frontend and editor.
 *
 * @package Blynex
 * @since 1.0.0
 */

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

// Make sure WordPress functions are available
require_once(ABSPATH . 'wp-includes/functions.php');
require_once(ABSPATH . 'wp-includes/plugin.php');

if (!class_exists('Blynex_Block_Assets')) {
    /**
     * Blynex_Block_Assets Class
     *
     * Manages the loading of theme assets for blocks and editor.
     */
    class Blynex_Block_Assets {

        /**
         * Constructor
         *
         * Sets up the hooks for enqueuing block assets.
         */
        public function __construct() {
            // Frontend scripts and styles
            add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);

            // Editor scripts and block styles
            add_action('enqueue_block_assets', [$this, 'enqueue_block_assets']);
            add_action('enqueue_block_editor_assets', [$this, 'enqueue_block_editor_assets']);
        }

        /**
         * Enqueue frontend assets
         */
        public function enqueue_frontend_assets() {
            if (!class_exists('WooCommerce')) {
                return;
            }

            // Enqueue WooCommerce styles
            wp_enqueue_style(
                'blynex-woocommerce-styles',
                get_template_directory_uri() . '/assets/css/woocommerce.css',
                [],
                wp_get_theme()->get('Version')
            );

            // Enqueue jQuery
            wp_enqueue_script('jquery');

            // Enqueue Quick View script
            wp_enqueue_script(
                'blynex-quick-view',
                get_template_directory_uri() . '/assets/js/quick-view.js',
                ['jquery'],
                wp_get_theme()->get('Version'),
                true
            );

            // Add WooCommerce AJAX URL for Quick View functionality
            wp_localize_script('blynex-quick-view', 'blynex_quick_view', array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('blynex-quick-view-nonce')
            ));

            // Enqueue Skip Link script for accessibility
            wp_enqueue_script(
                'blynex-skip-link',
                get_template_directory_uri() . '/assets/js/skip-link.js',
                [],
                wp_get_theme()->get('Version'),
                true
            );
        }

        /**
         * Enqueue block assets
         *
         * Registers and enqueues styles and scripts for blocks.
         * Handles both frontend and editor assets.
         */
        public function enqueue_block_assets() {
            // Get theme version
            $theme_version = wp_get_theme()->get('Version');

            // Load shared block styles for both frontend and editor
            wp_register_style(
                'blynex-block-styles',
                get_template_directory_uri() . '/build/style-index.css',
                [],
                $theme_version
            );
            wp_register_style(
                'blynex-block-styles-rtl',
                get_template_directory_uri() . '/build/style-index-rtl.css',
                [],
                $theme_version
            );
            // Block library styles are included in the main block styles
            // wp_register_style(
            //     'blynex-block-library',
            //     get_template_directory_uri() . '/build/style-index.css',
            //     [],
            //     $theme_version
            // );
            // wp_register_style(
            //     'blynex-block-library-rtl',
            //     get_template_directory_uri() . '/build/index-rtl.css',
            //     [],
            //     $theme_version
            // );

            wp_enqueue_style('blynex-block-styles');
            // wp_enqueue_style('blynex-block-library'); // Commented out to avoid duplication
            wp_enqueue_style('blynex-block-styles-rtl');
            // wp_enqueue_style('blynex-block-library-rtl');

            // Load frontend image filter script on frontend
            if (!is_admin()) {
                wp_register_script(
                    'blynex-image-filter-frontend',
                    get_template_directory_uri() . '/assets/js/image-filter-frontend.js',
                    array(),
                    $theme_version,
                    true
                );
                wp_enqueue_script('blynex-image-filter-frontend');
                return;
            }

            // Only enqueue editor-specific scripts/styles in admin
            if (is_admin()) {
                // Editor script is handled by enqueue_block_editor_assets to avoid duplication
                // Optionally enqueue helper script
                wp_register_script(
                    'blynex-helper-script',
                    get_template_directory_uri() . '/assets/js/helper.js',
                    array('wp-dom-ready', 'wp-blocks'),
                    $theme_version,
                    true
                );
                wp_enqueue_script('blynex-helper-script');
            }
        }

        /**
         * Enqueue block editor assets
         *
         * Specifically loads assets for the block editor.
         */
        public function enqueue_block_editor_assets() {
            // Get theme version for cache busting
            $theme_version = wp_get_theme()->get('Version');

            // Load editor scripts
            $asset_path = get_template_directory() . '/build/index.asset.php';

            if (file_exists($asset_path)) {
                $asset_data = require $asset_path;

                // Register main editor script
                wp_register_script(
                    'blynex-block-editor-script',
                    get_template_directory_uri() . '/build/index.js',
                    $asset_data['dependencies'],
                    $asset_data['version'],
                    true
                );

                // Enqueue the script
                wp_enqueue_script('blynex-block-editor-script');

                wp_localize_script('blynex-block-editor-script', 'blynexParams', array(
                    'zolo_pro_status' => get_option('zolo_pro_status') ? 'active' : 'inactive',
                    'zoloblocks_installer' => array(
                        'ajax_url' => admin_url('admin-ajax.php'),
                        'nonce' => wp_create_nonce('blynex_zoloblocks_nonce'),
                        'strings' => array(
                            'installing' => __('Installing ZoloBlocks plugin...', 'blynex'),
                            'activating' => __('Activating ZoloBlocks plugin...', 'blynex'),
                            'success' => __('ZoloBlocks plugin is ready!', 'blynex'),
                            'reloading' => __('Reloading page to initialize ZoloBlocks plugin...', 'blynex'),
                            'error' => __('Error: ', 'blynex'),
                            'permission_error' => __('You do not have permission to install plugins.', 'blynex'),
                            'plugin_required' => __('ZoloBlocks plugin is required to use the template library.', 'blynex'),
                            'plugin_not_installed' => __('ZoloBlocks plugin is not installed. Would you like to install it now?', 'blynex'),
                            'plugin_not_activated' => __('ZoloBlocks plugin is installed but not activated. Would you like to activate it now?', 'blynex'),
                            'install_activate' => __('Install & Activate', 'blynex'),
                            'activate' => __('Activate', 'blynex'),
                            'cancel' => __('Cancel', 'blynex')
                        )
                    )
                ));
            }
        }
    }

    // Initialize the class
    add_action('after_setup_theme', function () {
        new Blynex_Block_Assets();
    });
}
