<?php
/**
 *  Asset Service Provider File
 *
 * @category   ServiceProvider
 * @package    CodexShaper_Framework
 * @author     CodexShaper <info@codexshaper.com>
 * @license    https://www.gnu.org/licenses/gpl-2.0.html
 * @link       https://codexshaper.com
 * @since      1.0.0
 */

namespace CodexShaper\Framework\Providers;

use CodexShaper\Framework\Foundation\ServiceProvider;
use CodexShaper\Framework\Foundation\Traits\Hook;

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

/**
 *  Asset Service Provider Class
 *
 * @category   Class
 * @package    CodexShaper_Framework
 * @author     CodexShaper <info@codexshaper.com>
 * @license    https://www.gnu.org/licenses/gpl-2.0.html
 * @link       https://codexshaper.com
 * @since      1.0.0
 */
class AssetsServiceProvider extends ServiceProvider {

	use Hook;

	/**
	 * Boot the service provider.
	 *
	 * @return void
	 */
	public function boot() {
		// Booted code.
	}

	/**
	 * Register the service provider.
	 *
	 * @return void
	 */
	public function register() {
		$this->providers = apply_filters( 'csmf_console_providers', $this->providers );

		// Enqueue styles.
		$this->add_action( 'wp_enqueue_scripts', 'csmf_enqueue_styles' );
		// Enqueue Scripts.
		$this->add_action( 'wp_enqueue_scripts', 'csmf_enqueue_scripts' );
		// Admin styles.
		$this->add_action( 'admin_enqueue_scripts', 'csmf_admin_enqueue_styles' );
		// Admin scripts.
		$this->add_action( 'admin_enqueue_scripts', 'csmf_admin_enqueue_scripts' );
	}

	/**
	 * Plug Assets
	 *
	 * Load all plugin assets.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function csmf_enqueue_styles() {
		// Add fontawesome support for frontend.
		if ( ! wp_style_is( 'csmf-fa5' ) ) {
			wp_enqueue_style( 'csmf-fa5', csmf_plugin_base_url() . 'assets/css/vendor/fontawesome/css/all.min.css', array(), '5.15.5', 'all' );
		}

		$all_css_files = array(
			array(
				'handle' => 'csmf--frontend',
				'src'    => csmf_plugin_base_url() . 'assets/css/frontend' . CSMF_MIN_EXTENSION . '.css',
				'deps'   => array(),
				'ver'    => filemtime( csmf_plugin_base_path() . 'assets/css/frontend' . CSMF_MIN_EXTENSION . '.css' ),
				'media'  => 'all',
			),
		);

		/**
		 * Filter all css files
		 *
		 * @since 1.0.0
		 *
		 * @param array $all_css_files All css files.
		 */
		$all_css_files = apply_filters( 'csmf_css', $all_css_files );

		if ( is_array( $all_css_files ) && count( $all_css_files ) > 0 ) {
			foreach ( $all_css_files as $css ) {
				call_user_func_array( 'wp_enqueue_style', $css );
			}
		}
	}

	/**
	 * Plug Assets
	 *
	 * Load all plugin assets.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function csmf_enqueue_scripts() {

		// Localize. Send data from PHP to Javascript.
		wp_enqueue_script( 'csmf--localize', csmf_plugin_base_url() . 'assets/js/localize.js', array( 'jquery' ), filemtime( csmf_plugin_base_path() . 'assets/js/localize.js' ), true );
		$animation_option_name = csmf_config( 'options.animation.option_name' ) ?? 'csmf_animation_settings';
		$data = apply_filters(
			'csmf_localize_data',
			array(
				'ajax_url'      => admin_url( 'admin-ajax.php' ),
				'csmf_nonce'     => wp_create_nonce( 'csmf_nonce' ),
				'post_id'       => get_the_ID(),
				'csmf_animation_settings' => csmf_settings( $animation_option_name ),
				'color_palette' => apply_filters( 'csmf_color_palette', array() ),
				'i18n'          => array(
					'confirm'         => esc_html__( 'Are you sure?', 'codexshaper-framework' ),
					'typing_text'     => esc_html__( 'Please enter two or more characters', 'codexshaper-framework' ),
					'searching_text'  => esc_html__( 'Searching...', 'codexshaper-framework' ),
					'no_results_text' => esc_html__( 'No results found.', 'codexshaper-framework' ),
				),
			)
		);

		wp_localize_script( 'csmf--localize', 'CSMF_LOCALIZE_JS', $data );
		wp_enqueue_script( 'csmf--localize' );

		wp_register_script(
			'csmf--lazy-load',
			csmf_plugin_base_url() . 'assets/js/lazy-load' . CSMF_MIN_EXTENSION . '.js',
			array(),
			filemtime( csmf_plugin_base_path() . 'assets/js/lazy-load' . CSMF_MIN_EXTENSION . '.js' ),
			true
		);

		wp_register_script(
			'csmf--goodshare',
			csmf_plugin_base_url() . 'assets/js/vendor/goodshare.min.js',
			array( 'jquery' ),
			filemtime( csmf_plugin_base_path() . 'assets/js/vendor/goodshare.min.js' ),
			true
		);

		wp_enqueue_script(
			'csmf--script',
			csmf_plugin_base_url() . 'assets/js/frontend' . CSMF_MIN_EXTENSION . '.js',
			array( 'jquery' ),
			filemtime( csmf_plugin_base_path() . 'assets/js/frontend' . CSMF_MIN_EXTENSION . '.js' ),
			true
		);
	}

	/**
	 * Admin styles
	 *
	 * @return void.
	 */
	public function csmf_admin_enqueue_styles() {
		// Add fontawesome support for admin.
		if ( ! wp_style_is( 'csmf--fa5-admin' ) ) {
			wp_enqueue_style( 'csmf--fa5-admin', csmf_plugin_base_url() . 'assets/css/vendor/fontawesome/css/all.min.css', array(), '5.15.5', 'all' );
		}

		wp_enqueue_style(
			'csmf--options',
			csmf_plugin_base_url() . 'assets/css/options' . CSMF_MIN_EXTENSION . '.css',
			array(),
			filemtime( csmf_plugin_base_path() . 'assets/css/options' . CSMF_MIN_EXTENSION . '.css' ),
			'all',
		);
	}

	/**
	 * Admin scripts
	 *
	 * @return void.
	 */
	public function csmf_admin_enqueue_scripts() {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a controlled admin context.
		if ( current_user_can( 'manage_options' ) && isset( $_GET['page'] ) && $_GET['page'] === 'codexshaper-framework-settings' ) {
			wp_enqueue_script( 'jquery-ui-accordion' );
		}

		// Added wp media.
		wp_enqueue_media();

		wp_enqueue_script( 'csmf--admin-localize', csmf_plugin_base_url() . 'assets/js/admin-localize.js', array( 'jquery' ), filemtime( csmf_plugin_base_path() . 'assets/js/admin-localize.js' ), true );
		$data = apply_filters(
			'csmf_admin_localize_data',
			array(
				'admin_uRL'       => admin_url(),
				'ajax_url'        => admin_url( 'admin-ajax.php' ),
				'csmf_nonce'       => wp_create_nonce( 'csmf_nonce' ),
				'i18n'            => array(
					'confirm'         => esc_html__( 'Are you sure?', 'codexshaper-framework' ),
					'typing_text'     => esc_html__( 'Please enter two or more characters', 'codexshaper-framework' ),
					'searching_text'  => esc_html__( 'Searching...', 'codexshaper-framework' ),
					'no_results_text' => esc_html__( 'No results found.', 'codexshaper-framework' ),
				),
			)
		);

		wp_localize_script( 'csmf-admin-localize', 'CSMF_ADMIN_LOCALIZE_JS', $data );
		wp_enqueue_script( 'csmf-admin-localize' );

		wp_enqueue_script(
			'csmf--plugins-script',
			csmf_plugin_base_url() . 'assets/js/plugins' . CSMF_MIN_EXTENSION . '.js',
			array( 'jquery' ),
			filemtime( csmf_plugin_base_path() . 'assets/js/plugins' . CSMF_MIN_EXTENSION . '.js' ),
			true
		);

		wp_enqueue_script(
			'csmf--admin-script',
			csmf_plugin_base_url() . 'assets/js/admin' . CSMF_MIN_EXTENSION . '.js',
			array( 'jquery', 'csmf--plugins-script', 'csmf--admin-localize' ),
			filemtime( csmf_plugin_base_path() . 'assets/js/admin' . CSMF_MIN_EXTENSION . '.js' ),
			true
		);

		wp_localize_script( 'csmf--admin-script', 'CSMF_ADMIN_LOCALIZE_JS', $data );
	}
}
