<?php

namespace SoftMultimediaPlayer;

defined( 'ABSPATH' ) || exit;
/**
 * Enqueue Class
 */
class Enqueue {
    protected static $instance = null;

    /**
     * Constructor for the Enqueue class.
     *
     * @since 1.0.0
     */
    function __construct() {
        add_action( 'admin_enqueue_scripts', [$this, 'admin_assets'] );
        add_action( 'wp_enqueue_scripts', [$this, 'frontend_scripts'] );
    }

    /**
     * Enqueue admin styles and scripts
     *
     * @param string $hook The current admin page hook.
     *
     * @return void
     */
    function admin_assets( $hook ) {
        wp_enqueue_style(
            'sweetalert2',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/sweetalert2/sweetalert2.min.css',
            array(),
            '11.4.8'
        );
        wp_enqueue_style(
            'smp-admin',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/css/admin.css',
            array('wp-components'),
            SOFT_MULTIMEDIA_PLAYER_VERSION
        );
        wp_style_add_data( 'smp-admin', 'rtl', 'replace' );
        wp_register_script(
            'sweetalert2',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/sweetalert2/sweetalert2.min.js',
            [],
            '11.4.8',
            true
        );
        $deps = array(
            'jquery',
            'wp-util',
            'wp-i18n',
            'wp-element',
            'wp-components',
            'jquery-ui-dialog',
            'sweetalert2'
        );
        wp_enqueue_style( 'smp-select2', SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/select2/css/select2.min.css' );
        wp_enqueue_script(
            'smp-select2',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/select2/js/select2.full.min.js',
            ['jquery'],
            '4.0.13',
            true
        );
        wp_enqueue_script(
            'smp-admin',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/js/admin.js',
            $deps,
            SOFT_MULTIMEDIA_PLAYER_VERSION,
            true
        );
        $page = ( isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : '' );
        if ( 'soft-multimedia-player' == $page ) {
            wp_enqueue_media();
        }
        if ( 'soft-multimedia-player-settings' == $page ) {
            //tinymce editor
            wp_enqueue_media();
            wp_enqueue_editor();
            //code editor
            wp_enqueue_script( 'wp-theme-plugin-editor' );
            wp_enqueue_style( 'wp-codemirror' );
            wp_enqueue_code_editor( array(
                'type' => 'text/css',
            ) );
        }
        if ( $page == 'soft-multimedia-player-statistics' || 'index.php' === $hook ) {
            wp_enqueue_script(
                'multimedia-player-chart',
                SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/Chart.bundle.min.js',
                ['jquery-ui-datepicker'],
                '2.8.0',
                true
            );
        }
        wp_localize_script( 'smp-admin', 'smp', $this->get_localize_data( $hook ) );
    }

    /**
     * Enqueue frontend styles and scripts
     *
     * @return void
     */
    function frontend_scripts() {
        $deps = array(
            'jquery',
            'wp-components',
            'wp-element',
            'wp-util',
            'wp-i18n',
            'sweetalert2'
        );
        wp_register_style(
            'sweetalert2',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/sweetalert2/sweetalert2.min.css',
            array(),
            '11.4.8'
        );
        wp_enqueue_style(
            'smp',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/css/frontend.css',
            array('sweetalert2'),
            SOFT_MULTIMEDIA_PLAYER_VERSION
        );
        wp_style_add_data( 'smp', 'rtl', 'replace' );
        wp_register_script(
            'sweetalert2',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/vendor/sweetalert2/sweetalert2.min.js',
            [],
            '11.4.8',
            true
        );
        wp_enqueue_script(
            'smp',
            SOFT_MULTIMEDIA_PLAYER_ASSETS . '/js/frontend.js',
            $deps,
            SOFT_MULTIMEDIA_PLAYER_VERSION,
            true
        );
        wp_localize_script( 'smp', 'smp', $this->get_localize_data() );
    }

    /**
     * Get localize data for the current request
     *
     * @param string $hook Optional. The current admin page hook.
     *
     * @return array Localize data.
     */
    public function get_localize_data( $hook = null ) {
        $data = array(
            'isAdmin'    => is_admin(),
            'homeUrl'    => home_url(),
            'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
            'players'    => smp_get_players(),
            'pluginUrl'  => SOFT_MULTIMEDIA_PLAYER_URL,
            'players'    => smp_get_players(),
            'settings'   => smp_get_settings(),
            'nonce'      => wp_create_nonce( 'multimedia-player' ),
            'genres'     => smp_get_genres(),
            'isPro'      => smp_fs()->can_use_premium_code__premium_only(),
            'upgradeUrl' => smp_fs()->get_upgrade_url(),
        );
        // admin page data
        if ( is_admin() ) {
            $data['adminUrl'] = admin_url();
            $data['adminEmail'] = get_option( 'admin_email' );
            $data['multimediaPlayerExcludeList'] = smp_get_exclude_list();
        }
        return $data;
    }

    /**
     * Returns the singleton instance of the Enqueue class.
     *
     * @return Enqueue The instance of the Enqueue class.
     */
    public static function instance() {
        if ( is_null( self::$instance ) ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

}

Enqueue::instance();