<?php
/**
 * File: Ecre_Assets.php
 *
 * The Ecre_Assets class handles the enqueuing of styles and scripts for the Wooecho Rewards plugin.
 *
 * @package ECRE
 * @since   1.0.0
 */

namespace ECRE;

if ( ! class_exists( 'Ecre_Assets' ) ) {
	/**
	 * The Ecre_Assets class handles the enqueuing of styles and scripts for the Wooecho Rewards plugin.
	 *
	 * @since 1.0.0
	 */
	class Ecre_Assets {
		/**
		 * Indicates whether WooCommerce is activated.
		 *
		 * @var bool
		 */
		public $is_woocommerce_activate = false;
		/**
		 * Ecre_Assets constructor.
		 *
		 * Initializes the Ecre_Assets class and registers actions to enqueue styles and scripts
		 * for both the WordPress admin area and the frontend, based on plugin settings and context.
		 *
		 * @param bool  $is_pro                  Indicates whether the user has a professional (pro) account.
		 * @param array $erce_settings           The current plugin settings array.
		 * @param bool  $is_woocommerce_activate Indicates whether WooCommerce is activated.
		 *
		 * @since 1.0.0
		 */
		public function __construct( $is_pro, $erce_settings, $is_woocommerce_activate ) {
			add_action( 'admin_enqueue_scripts', array( $this, 'ecre_admin_enqueue' ) );
			if ( $is_woocommerce_activate ) {
				add_action( 'wp_enqueue_scripts', array( $this, 'ecre_frontend_enqueue' ), 9999 );
			}

			$this->is_woocommerce_activate = $is_woocommerce_activate;
		}

		/**
		 * Enqueue Ecre_Assets for the admin area.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function ecre_admin_enqueue() {
			// Enqueue admin Ecre_Assets here.
			wp_enqueue_style( 'echo-rewards-admin-fonts', ECRE_ASSETS . '/css/fonts.css', array(), ECRE_VERSION, 'all' );
			wp_enqueue_media();
			$is_pro = false;

			if ( function_exists( 'echo_rewards_pro' ) ) {
				$is_pro = echo_rewards_pro()->license_status;
			}

			$plugin_list      = get_option( 'active_plugins' );
			$ecre_pro_path    = 'echo-rewards-pro/echo-rewards.php';
			$is_pro_latest    = false;
			$is_pro_activated = false;

			if ( in_array( $ecre_pro_path, $plugin_list, true ) ) {
				$is_pro_latest    = ecre_is_pro_latest_version( $ecre_pro_path );
				$is_pro_activated = true;
			}

			$screen = get_current_screen();

			if ( $screen && 'toplevel_page_echo-rewards' === $screen->id ) {
				$woocommerce_plugin_path = WP_PLUGIN_DIR . '/woocommerce/woocommerce.php';

				wp_enqueue_script( 'ecre-admin-build', ECRE_URL . '/build/admin_main.build.js', array( 'wp-blocks', 'wp-element', 'wp-i18n' ), ECRE_VERSION, true );
				wp_enqueue_script( 'ecre-sizzle', ECRE_URL . '/assets/js/sizzle.min.js', array(), ECRE_VERSION, true );

				wp_localize_script(
					'ecre-admin-build',
					'ecreAdmin',
					array(
						'ajaxurl'                    => admin_url( 'admin-ajax.php' ),
						'nonce'                      => wp_create_nonce( 'ecre_admin_nonce' ),
						'currency'                   => function_exists( 'ecre_custom_currency_symbol' ) ? ecre_custom_currency_symbol() : null,
					'currency_settings'          => function_exists( 'ecre_get_currency_settings' ) ? ecre_get_currency_settings() : array(),
						'strings'                    => Ecre_Strings::get(),
						'is_pro'                     => $is_pro,
						'is_pro_latest'              => $is_pro_latest,
						'is_pro_activated'           => $is_pro_activated,
						'site_url'                   => site_url(),
						'imgdir'                     => ECRE_IMG_DIR,
						'woocommerce_installed'      => file_exists( $woocommerce_plugin_path ) ? true : false,
						'is_woocommerce_subscriptions_active' => function_exists( 'ecre_is_woocommerce_subscriptions_active' ) ? ecre_is_woocommerce_subscriptions_active() : false,
						'woocommerce_order_statuses' => function_exists( 'ecre_get_woocommerce_order_statuses' ) ? ecre_get_woocommerce_order_statuses() : null,
						'default_settings'           => function_exists( 'ecre_default_settings' ) ? ecre_default_settings() : array(),
					)
				);
				wp_enqueue_style( 'ecre-admin-main-css', ECRE_URL . '/build/admin.build.css', array(), ECRE_VERSION, 'all' );
				wp_enqueue_style( 'ecre-admin-css', ECRE_ASSETS . '/css/admin.css', array(), ECRE_VERSION, 'all' );

				if ( ! $this->is_woocommerce_activate ) {
					$css = '
						#toplevel_page_echo-rewards ul.wp-submenu li:nth-child(2) {
							display: none !important;
						}
					';
					wp_add_inline_style( 'admin-menu', $css );
				}
			}
		}

		/**
		 * Enqueue Ecre_Assets for the frontend.
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function ecre_frontend_enqueue() {
			wp_enqueue_style( 'echo-rewards-free-frontend-fonts', ECRE_ASSETS . '/css/fonts.css', array(), ECRE_VERSION, 'all' );
			wp_enqueue_script( 'ecre-frontend-build', ECRE_URL . '/build/frontend_main.build.js', array( 'wp-blocks', 'wp-element', 'wp-i18n' ), ECRE_VERSION, true );
			wp_enqueue_script( 'ecre-sizzle-frontend', ECRE_URL . '/assets/js/sizzle.min.js', array(), ECRE_VERSION, true );

			$is_pro = false;

			if ( function_exists( 'echo_rewards_pro' ) ) {
				$is_pro = echo_rewards_pro()->license_status;
			}

			$plugin_list      = get_option( 'active_plugins' );
			$ecre_pro_path    = 'echo-rewards-pro/echo-rewards.php';
			$is_pro_latest    = false;
			$is_pro_activated = false;

			if ( in_array( $ecre_pro_path, $plugin_list, true ) ) {
				$is_pro_latest    = ecre_is_pro_latest_version( $ecre_pro_path );
				$is_pro_activated = true;
			}

			$user_id              = get_current_user_id();
			$total_points         = intval( get_user_meta( $user_id, 'ecre_reward_points', true ) );
			$total_applied_points = intval( get_user_meta( $user_id, 'ecre_applied_reward_points', true ) );

			$available_points      = intval( get_user_meta( $user_id, 'ecre_available_points', true ) );
			$total_available_point = intval( get_user_meta( $user_id, 'ecre_available_points', true ) );

			// Retrieve the meta value for a specific user.
			$reward_point_expiry_date   = get_user_meta( $user_id, 'ecre_reward_point_expiry_date', true );
			$reward_point_expiry_date   = gmdate( 'jS F, Y', strtotime( $reward_point_expiry_date ) );
			$reward_point_expiry_status = ecre_get_expiry_status( $reward_point_expiry_date );

			wp_localize_script(
				'ecre-frontend-build',
				'ecreFrontend',
				array(
					'ajaxurl'                       => admin_url( 'admin-ajax.php' ),
					'nonce'                         => wp_create_nonce( 'ecre_admin_nonce' ),
					'currency'                      => ecre_custom_currency_symbol(),
					'has_orders'                    => ecre_has_orders(),
					'is_user_logged_in'             => is_user_logged_in(),
					'site_url'                      => site_url(),
					'total_points'                  => $total_points,
					'total_applied_points'          => $total_applied_points,
					'reward_point_expiry'           => $reward_point_expiry_date,
					'reward_point_expiry_status'    => $reward_point_expiry_status,
					'reward_point_expiry_status_Ca' => ! empty( $reward_point_expiry_status ) ? ucfirst( $reward_point_expiry_status ) : $reward_point_expiry_status,
					'available_points'              => $available_points,
					'strings'                       => Ecre_Strings::get(),
					'is_pro'                        => $is_pro,
					'is_pro_latest'                 => $is_pro_latest,
					'is_pro_activated'              => $is_pro_activated,
					'default_settings'              => ecre_default_settings(),
				)
			);

			wp_enqueue_style( 'ecre-frontend-main-css', ECRE_URL . '/build/frontend.build.css', array(), ECRE_VERSION, 'all' );
		}
	}
}
