<?php
declare(strict_types=1);

namespace EHxMe\Classes;

use EHxMe\Helpers\Helper;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Class RegisterScripts
 *
 * This class is responsible for registering and enqueuing scripts and styles for both the admin and frontend of the WordPress plugin.
 */
class RegisterScripts 
{
    /**
     * Constructor for the RegisterScripts class.
     *
     * Initializes the sets up WordPress hooks.
     *
     * @return void
     */
    public function __construct() 
    {
        // Register admin scripts
        add_action('admin_enqueue_scripts', [$this, 'registerAdminScript']);

        // Register frontend scripts
        add_action('wp_enqueue_scripts', [$this, 'registerScript']);
    }

    /**
     * Register admin scripts and styles.
     *
     * This method enqueues admin-specific CSS and JS files. It also localizes the 'ehxme-admin-js' script with necessary data.
     */
    public function registerAdminScript() 
    {
        wp_enqueue_style(
            handle: 'ehxme-admin-css',
            src: EHXME_PLUGIN_URL . 'assets/css/admin.css',
            ver: EHXME_VERSION
        );

        wp_enqueue_style(
            handle: 'ehxme-fonticons-css',
            src: EHXME_PLUGIN_URL . 'assets/libs/legacy/fonticons/fonticons-fa.css',
            ver: EHXME_VERSION
        );

        wp_enqueue_style(
            handle: 'ehxme-admin-jquery-ui-css',
            src: EHXME_PLUGIN_URL . 'assets/libs/jquery-ui/jquery-ui.min.css',
            ver: EHXME_VERSION
        );

        wp_enqueue_script(
            handle: 'ehxme-admin-sortable-js',
            src: EHXME_PLUGIN_URL . 'assets/libs/sortablejs/sortable.min.js',
            deps: ['jquery'],
            ver: EHXME_VERSION,
            args: true
        );

        wp_enqueue_script(handle: 'jquery-ui-tooltip', deps: ['jquery']);
        
        wp_enqueue_script(
            handle: 'ehxme-admin-helper-js',
            src: EHXME_PLUGIN_URL . 'assets/js/helper.js',
            deps: ['jquery'],
            ver: EHXME_VERSION,
            args: true
        );
        wp_enqueue_script(
            handle: 'ehxme-admin-js',
            src: EHXME_PLUGIN_URL . 'assets/js/admin.js',
            deps: ['jquery'],
            ver: EHXME_VERSION,
            args: true
        );

        wp_localize_script('ehxme-admin-js', 'ehxme_obj', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce(Helper::NONCE_NAME),
        ]);
    }

    /**
     * Register frontend scripts and styles.
     *
     * This method enqueues frontend-specific CSS and JS files. It also registers and enqueues Google Map and Stripe scripts if enabled in the plugin settings.
     */
    public function registerScript() 
    {
        wp_register_style(
            handle: 'ehxme-select2',
            src: EHXME_PLUGIN_URL . 'assets/libs/select2/select2.min.css',
            ver: EHXME_VERSION,
        );

        wp_register_style(
            handle: 'ehxme-datatable',
            src: EHXME_PLUGIN_URL . 'assets/libs/datatables/datatable.css',
            ver: EHXME_VERSION,
        );

        wp_enqueue_style(
            handle: 'ehxme-main-css',
            src: EHXME_PLUGIN_URL . 'assets/css/style.css',
            ver: EHXME_VERSION
        );

        $stripe_enable = (bool) Settings::extractSettingValue('stripe_enable', false);
        if ($stripe_enable) {
            wp_register_script(
                handle: 'ehxme-stripe',
                src: 'https://js.stripe.com/v3/',
                deps: [],
                ver: EHXME_VERSION,
                args: true
            );
        }

        wp_register_script(
            handle: 'ehxme-select2',
            src: EHXME_PLUGIN_URL . 'assets/libs/select2/select2.full.min.js',
            deps: ['jquery'],
            ver: EHXME_VERSION,
            args: true
        );

        wp_register_script(
            handle: 'ehxme-datatable',
            src: EHXME_PLUGIN_URL . 'assets/libs/datatables/datatable.js',
            deps: ['jquery'],
            ver: EHXME_VERSION,
            args: true
        );

        wp_enqueue_script(
            handle: 'ehxme-helper-js',
            src: EHXME_PLUGIN_URL . 'assets/js/helper.js',
            deps: ['jquery'],
            ver: EHXME_VERSION,
            args: true
        );
        wp_enqueue_script(
            handle: 'ehxme-main-js',
            src: EHXME_PLUGIN_URL . 'assets/js/main.js',
            deps: ['jquery','ehxme-select2'],
            ver: EHXME_VERSION,
            args: true
        );
        wp_localize_script('ehxme-main-js', 'ehxme_obj', [
            'ajax_url' => admin_url('admin-ajax.php'),
        ]);
    }
}
