<?php // phpcs:ignore
/**
 * Goodmeet_Assets Class
 *
 * This class handles the loading of assets for the goodmeet plugin.
 *
 * @package SOVLIX\GOODMEET
 */

namespace SOVLIX\GOODMEET;

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

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

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

		/**
		 * Goodmeet enqueue for admin.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function goodmeet_admin_enqueue() {
			$create_meeting_url            = admin_url( 'admin.php?page=goodmeet#/meeting/create' );
			$goodmeet_google_account       = get_option( 'goodmeet_google_meet_account' );
			$goodmeet_google_meet_settings = get_option( 'goodmeet_google_meet_settings' );
			$current_user_id               = get_current_user_id();

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

			wp_enqueue_style( 'goodmeet_admin-style', GOODMEET_ASSETS . '/css/admin.css', array(), GOODMEET_VERSION, 'all' );
			wp_enqueue_style( 'goodmeet_admin-fonts', GOODMEET_ASSETS . '/css/fonts.css', array(), GOODMEET_VERSION, 'all' );

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

				wp_enqueue_script( 'goodmeet_admin-build', GOODMEET_ASSETS . '/dist/main.build.js', array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-i18n' ), GOODMEET_VERSION, true );
				wp_enqueue_script( 'goodmeet_admin-js', GOODMEET_ASSETS . '/js/admin.js', array( 'jquery' ), GOODMEET_VERSION, true );

				// Localize the script and pass the create meeting URL.
				wp_localize_script(
					'goodmeet_admin-build',
					'goodmeetMeetingsData',
					array(
						'createMeetingUrl'              => $create_meeting_url,
						'google_account'                => $goodmeet_google_account,
						'goodmeet_google_meet_settings' => $goodmeet_google_meet_settings,
						'goodmeet_timezone'             => goodmeet_timezone_offset(),
						'goodmeet_password'             => wp_generate_password( 8, false ),
						'installed'                     => goodmeet_pro_plugin_exists(),
						'active'                        => goodmeet_fs()->can_use_premium_code__premium_only(),
						'license'                       => goodmeet_fs()->can_use_premium_code__premium_only(),
						'google_meet_redirect_url'      => admin_url( 'admin.php?page=goodmeet-settings&platform=google-meet' ),
						'assets_path'                   => GOODMEET_ASSETS,
						'checkout_url'                  => goodmeet_fs()->get_upgrade_url(),
						'is_paying'                     => goodmeet_fs()->is_paying(),
						'ajax_url'                      => admin_url( 'admin-ajax.php' ),
						'nonce'                         => wp_create_nonce( 'goodmeet_admin_nonce' ),
						'siteurl'                       => get_site_url(),
						'mce_btn_name'                  => __( 'Meeting', 'goodmeet' ),
						'hide_floating_create_btn'      => false,
						'hide_floating_update_btn'      => false,
						'is_admin'                      => is_admin(),
						'strings'                       => Goodmeet_Translate::strings(),
					)
				);

				wp_enqueue_style( 'goodmeet_admin-build-style', GOODMEET_ASSETS . '/dist/main.build.css', array(), GOODMEET_VERSION, 'all' );
			}
		}

		/**
		 * Goodmeet enqueue for frontend.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function goodmeet_frontend_enqueue() {

			$goodmeet_google_meet_settings = get_option( 'goodmeet_google_meet_settings' );
			$goodmeet_google_account       = get_option( 'goodmeet_google_meet_account' );
			wp_enqueue_style( 'dashicons' );

			if ( is_user_logged_in() ) {

				wp_enqueue_script( 'goodmeet_frontend-main-build', GOODMEET_ASSETS . '/dist/main.build.js', array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-i18n' ), GOODMEET_VERSION, true );

				wp_enqueue_media();
				wp_enqueue_style( 'dashicons' );

				wp_localize_script(
					'goodmeet_frontend-main-build',
					'goodmeetMeetingsData',
					array(
						'google_account'                => $goodmeet_google_account,
						'goodmeet_google_meet_settings' => $goodmeet_google_meet_settings,
						'goodmeet_timezone'             => goodmeet_timezone_offset(),
						'goodmeet_password'             => wp_generate_password( 8, false ),
						'installed'                     => goodmeet_pro_plugin_exists(),
						'active'                        => goodmeet_fs()->can_use_premium_code__premium_only(),
						'license'                       => goodmeet_fs()->can_use_premium_code__premium_only(),
						'checkout_url'                  => '',
						'siteurl'                       => get_site_url(),
						'hide_floating_create_btn'      => true,
						'hide_floating_update_btn'      => true,
						'mce_btn_name'                  => __( 'Meeting', 'goodmeet' ),
						'is_admin'                      => is_admin(),
						'ajax_url'                      => admin_url( 'admin-ajax.php' ),
						'nonce'                         => wp_create_nonce( 'goodmeet_admin_nonce' ),
						'strings'                       => Goodmeet_Translate::strings(),
					)
				);
			}

			wp_enqueue_style( 'goodmeet_admin-fonts', GOODMEET_ASSETS . '/css/fonts.css', array(), GOODMEET_VERSION, 'all' );

			wp_enqueue_script( 'goodmeet_frontend-js', GOODMEET_ASSETS . '/js/frontend.js', array( 'jquery', 'moment' ), GOODMEET_VERSION, true );

			wp_enqueue_script( 'goodmeet_frontend-build', GOODMEET_ASSETS . '/dist/frontend.build.js', array( 'wp-blocks', 'wp-element', 'wp-i18n' ), GOODMEET_VERSION, true );

			wp_enqueue_style( 'goodmeet_frontend-build-style', GOODMEET_ASSETS . '/dist/main.build.css', array(), GOODMEET_VERSION, 'all' );

			// Localize script to pass data to JavaScript.
			wp_localize_script(
				'goodmeet_frontend-build',
				'goodmeet_frontend_data',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'goodmeet_frontend_nonce' ),
				)
			);

			wp_localize_script(
				'goodmeet_frontend-js',
				'goodmeet_frontend_params',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'goodmeet_frontend_nonce' ),
				)
			);

			wp_enqueue_script( 'goodmeet_countdown-js', GOODMEET_ASSETS . '/js/countdown.js', array( 'jquery', 'moment' ), GOODMEET_VERSION, true );

			wp_localize_script(
				'goodmeet_countdown-js',
				'goodmeettran',
				array(
					'strings' => Goodmeet_Translate::strings(),
				)
			);
			wp_enqueue_style( 'goodmeet_meeting-css', GOODMEET_ASSETS . '/css/meeting-style.css', array(), GOODMEET_VERSION, 'all' );
		}
	}
}
