<?php declare(strict_types=1);

/**
 * Fired during plugin activation
 *
 * @link       https://webequipe.com
 * @since      1.0.0
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/includes
 * @author     Webequipe <support@webequipe.com>
 */

namespace Webequipe\SpinWinWheel;

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

/**
 * Assets handlers class
 */
class Assets
{
    /**
     * Get plugin base URL
     */
    private function get_plugin_url() {
        $url = str_replace( '/assets', '', WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS );
        // Ensure trailing slash
        return trailingslashit( $url );
    }

    /**
     * Class constructor
     */
    public function __construct()
    {
        add_action('wp_enqueue_scripts', [$this, 'register_assets']);
        add_action('admin_enqueue_scripts', [$this, 'register_assets']);
        add_filter( 'script_loader_tag', [ $this, 'add_defer_attribute' ], 10, 3 );
    }

    /**
     * Add defer to plugin frontend scripts.
     *
     * @param string $tag    Script tag HTML.
     * @param string $handle Script handle.
     * @param string $src    Script source URL.
     * @return string
     */
    public function add_defer_attribute( $tag, $handle, $src ) {
        $deferred_handles = array(
            'wsww-bootstrap-js',
            'wsww-frontend-script',
            'wsww-ajax-script',
        );

        if ( ! in_array( $handle, $deferred_handles, true ) ) {
            return $tag;
        }

        if ( false !== strpos( $tag, ' defer' ) ) {
            return $tag;
        }

        return str_replace( ' src=', ' defer src=', $tag );
    }

    /**
     * All available scripts
     *
     * @return array
     */
    public function get_scripts()
    {
        return [
            'd3-v3' => [
                'src' => WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/js/d3.v3.min.js',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/js/d3.v3.min.js') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/js/d3.v3.min.js') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
                'deps' => [],
                'in_footer' => false,
            ],
            'wsww-bootstrap-js' => [
                'src' => WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/js/bootstrap.bundle.min.js',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/js/bootstrap.bundle.min.js') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/js/bootstrap.bundle.min.js') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
                'deps' => ['jquery'],
                'in_footer' => true,
            ],
            'wsww-frontend-script' => [
                'src' => $this->get_plugin_url() . 'public/js/webequipe-spin-win-wheel-public.js',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/public/js/webequipe-spin-win-wheel-public.js') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/public/js/webequipe-spin-win-wheel-public.js') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
                'deps' => ['jquery', 'd3-v3'],
                'in_footer' => true,
            ],
            'wsww-ajax-script' => [
                'src' => WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/js/wsww-ajax.js',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/js/wsww-ajax.js') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/js/wsww-ajax.js') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
                'deps' => ['jquery', 'wsww-bootstrap-js'],
                'in_footer' => true,
            ]

        ];

    }

    /**
     * All available styles
     *
     * @return array
     */
    public function get_styles()
    {
        return [
            'wsww-bootstrap-style' => [
                'src' => WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/css/bootstrap.min.css',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/css/bootstrap.min.css') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/css/bootstrap.min.css') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
            ],
            'wsww-spin-style' => [
                'src' => WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/css/spin.css',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/css/spin.css') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/assets/css/spin.css') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
                'deps' => ['wsww-bootstrap-style'],
            ],
            'wsww-frontend-style' => [
                'src' => $this->get_plugin_url() . 'public/css/webequipe-spin-win-wheel-public.css',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/public/css/webequipe-spin-win-wheel-public.css') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/public/css/webequipe-spin-win-wheel-public.css') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
                'deps' => ['wsww-spin-style'],
            ],
            'wsww-admin-style' => [
                'src' => $this->get_plugin_url() . 'admin/css/webequipe-spin-win-wheel-admin.css',
                'version' => file_exists(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/admin/css/webequipe-spin-win-wheel-admin.css') ? filemtime(WEBEQUIPE_SPIN_WIN_WHEEL_PATH . '/admin/css/webequipe-spin-win-wheel-admin.css') : WEBEQUIPE_SPIN_WIN_WHEEL_VERSION,
            ],
        ];
    }

    /**
     * Register scripts and styles
     *
     * @return void
     */
    public function register_assets()
    {
        $scripts = $this->get_scripts();
        $styles = $this->get_styles();

        foreach ($scripts as $handle => $script) {
            $deps = isset($script['deps']) ? $script['deps'] : false;

            wp_register_script($handle, $script['src'], $deps, $script['version'], $script['in_footer']);
        }

        foreach ($styles as $handle => $style) {
            $deps = isset($style['deps']) ? $style['deps'] : false;

            wp_register_style($handle, $style['src'], $deps, $style['version']);
        }

        wp_localize_script('wsww-ajax-script', 'wswwSpinObj', [
            'ajaxurl' => admin_url('admin-ajax.php'),
            'error' => __('Something went wrong', 'webequipe-spin-win-wheel'),
            'nonce' => wp_create_nonce('wsww-email-enquiry-form'),
            'spinWinNonce' => wp_create_nonce('wsww-spin-win-form'),
        ]);
    }

    // Enqueue necessary scripts and styles for the color picker
    public function enqueue_color_picker()
    {
        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('wp-color-picker');
    }
}
