<?php // phpcs:ignore
/**
 * Wemeet_Assets Class
 *
 * This class handles the loading of assets for the Meeting Hub plugin.
 *
 * @package SOVLIX\WEMEET
 */

namespace SOVLIX\WEMEET;

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

if ( ! class_exists( 'Wemeet_Assets' ) ) {
	/**
	 * Wemeet_Assets Class
	 *
	 * Manages the loading of assets, such as styles and scripts, for the Meeting Hub plugin.
	 *
	 * @since 1.0.0
	 */
	class Wemeet_Assets {

		/**
		 * Wemeet_Assets constructor.
		 *
		 * Initializes the Wemeet_Assets class and sets up actions for enqueueing assets.
		 */
		public function __construct() {
			add_action( 'admin_enqueue_scripts', array( $this, 'wemeet_admin_enqueue' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'wemeet_frontend_enqueue' ), 9999 );
		}

		/**
		 * Meeting Hub enqueue for admin.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function wemeet_admin_enqueue() {
			$create_meeting_url     = admin_url( 'admin.php?page=wemeet#/meeting/create' );
			$wemeet_webex_settings = get_option( 'wemeet_webex_settings' );
			$current_user_id        = get_current_user_id();
			$webex_access_token     = get_user_meta( $current_user_id, 'wemeet_webex_access_token', true );
			$is_pr_avaliable  = false;

			wp_enqueue_media();
			wp_enqueue_style( 'dashicons' );
			$screen = get_current_screen();

			wp_enqueue_style( 'wemeet-admin-style', WEMEET_ASSETS . '/css/admin.css', array(), WEMEET_VERSION, 'all' );
			wp_enqueue_style( 'wemeet-admin-fonts', WEMEET_ASSETS . '/css/fonts.css', array(), WEMEET_VERSION, 'all' );

			if ( $screen && ( strpos( $screen->id, 'wemeet' ) !== false ) ) {

				wp_enqueue_script( 'wemeet-admin-build', WEMEET_ASSETS . '/dist/main.build.js', array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-i18n' ), WEMEET_VERSION, true );
				wp_enqueue_script( 'wemeet-admin-js', WEMEET_ASSETS . '/js/admin.js', array( 'jquery' ), WEMEET_VERSION, true );

				// Set script translations.
				wp_set_script_translations( 'wemeet-admin-build', 'wemeet', plugin_dir_path( WEMEET_FILE ) . 'languages/' );

				// Localize the script and pass the create meeting URL.
				wp_localize_script(
					'wemeet-admin-build',
					'wemeetMeetingsData',
					array(
						'createMeetingUrl'         => $create_meeting_url,
						'webex_auth'               => ! empty( $webex_access_token ) ? 'connected' : '',
						'wemeet_webex_settings'   => $wemeet_webex_settings,
						'wemeet_timezone'         => wemeet_timezone_offset(),
						'wemeet_password'         => wp_generate_password( 8, false ),
						'installed'                => wemeet_pro_plugin_exists(),
						'active'                   => $is_pr_avaliable,
						'license'                  => $is_pr_avaliable,
						'pricing_url'              => '',
						'assets_path'              => WEMEET_ASSETS,
						'checkout_url'             => '',
						'is_paying'                => '',
						'ajax_url'                 => admin_url( 'admin-ajax.php' ),
						'nonce'                    => wp_create_nonce( 'wemeet_admin_nonce' ),
						'siteurl'                  => get_site_url(),
						'mce_btn_name'             => __( 'Meeting', 'wemeet' ),
						'hide_floating_create_btn' => false,
						'hide_floating_update_btn' => false,
						'is_admin'                 => is_admin(),
						'strings'                  => Wemeet_Translate::strings(),
					)
				);

				wp_enqueue_style( 'wemeet-admin-build-style', WEMEET_ASSETS . '/dist/main.build.css', array(), WEMEET_VERSION, 'all' );
			}
		}

		/**
		 * Meeting Hub enqueue for frontend.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function wemeet_frontend_enqueue() {
			$wemeet_webex_settings = get_option( 'wemeet_webex_settings' );
			$current_user_id        = get_current_user_id();
			$webex_access_token     = get_user_meta( $current_user_id, 'wemeet_webex_access_token', true );

			wp_enqueue_style( 'dashicons' );

			$is_pr_avaliable = false;

			if ( is_user_logged_in() ) {

				wp_enqueue_script( 'wemeet-frontend-main-build', WEMEET_ASSETS . '/dist/main.build.js', array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-i18n' ), WEMEET_VERSION, true );

				wp_enqueue_media();
				wp_enqueue_style( 'dashicons' );

				wp_localize_script(
					'wemeet-frontend-main-build',
					'wemeetMeetingsData',
					array(
						'webex_auth'               => ! empty( $webex_access_token ) ? 'connected' : '',
						'wemeet_webex_settings'   => $wemeet_webex_settings,
						'wemeet_timezone'         => wemeet_timezone_offset(),
						'wemeet_password'         => wp_generate_password( 8, false ),
						'installed'                => wemeet_pro_plugin_exists(),
						'active'                   => $is_pr_avaliable,
						'license'                  => $is_pr_avaliable,
						'checkout_url'             => '',
						'siteurl'                  => get_site_url(),
						'hide_floating_create_btn' => true,
						'hide_floating_update_btn' => true,
						'mce_btn_name'             => __( 'Meeting', 'wemeet' ),
						'is_admin'                 => current_user_can( 'manage_options' ),
						'ajax_url'                 => admin_url( 'admin-ajax.php' ),
						'nonce'                    => wp_create_nonce( 'wemeet_admin_nonce' ),
						'strings'                  => Wemeet_Translate::strings(),
					)
				);
			}

			wp_enqueue_style( 'wemeet-admin-fonts', WEMEET_ASSETS . '/css/fonts.css', array(), WEMEET_VERSION, 'all' );

			wp_enqueue_script( 'wemeet-frontend-js', WEMEET_ASSETS . '/js/frontend.js', array( 'jquery', 'moment' ), WEMEET_VERSION, true );

			wp_enqueue_script( 'wemeet-frontend-build', WEMEET_ASSETS . '/dist/frontend.build.js', array( 'wp-blocks', 'wp-element', 'wp-i18n' ), WEMEET_VERSION, true );

			wp_enqueue_style( 'wemeet-frontend-build-style', WEMEET_ASSETS . '/dist/main.build.css', array(), WEMEET_VERSION, 'all' );

			// Localize script to pass data to JavaScript.
			wp_localize_script(
				'wemeet-frontend-build',
				'wemeet_frontend_data',
				array(
					'ajax_url'        => admin_url( 'admin-ajax.php' ),
					'nonce'           => wp_create_nonce( 'wemeet_frontend_nonce' ),
					'strings'         => Wemeet_Translate::strings(),
					'is_admin'        => current_user_can( 'manage_options' ),
					'page_id'         => get_the_ID(),
					'visibility_mode' => get_post_meta( get_the_ID(), '_wemeet_visibility_mode', true ) ? get_post_meta( get_the_ID(), '_wemeet_visibility_mode', true ) : 'user',
					'selected_roles'  => get_post_meta( get_the_ID(), '_wemeet_visibility_roles', true ) ? get_post_meta( get_the_ID(), '_wemeet_visibility_roles', true ) : array(),
					'selected_users'  => get_post_meta( get_the_ID(), '_wemeet_visibility_users', true ) ? get_post_meta( get_the_ID(), '_wemeet_visibility_users', true ) : array(),
				)
			);

			wp_localize_script(
				'wemeet-frontend-js',
				'wemeet_frontend_params',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'wemeet_frontend_nonce' ),
				)
			);

			wp_enqueue_script( 'wemeet-countdown-js', WEMEET_ASSETS . '/js/countdown.js', array( 'jquery', 'moment' ), WEMEET_VERSION, true );

			wp_localize_script(
				'wemeet-countdown-js',
				'wemeettran',
				array(
					'strings' => Wemeet_Translate::strings(),
				)
			);
			wp_enqueue_style( 'wemeet-meeting-css', WEMEET_ASSETS . '/css/meeting-style.css', array(), WEMEET_VERSION, 'all' );
		}
	}
}
