<?php
/**
 * Asset Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Admin\Errigal_Admin as Admin;
use Errigal\Constant\{
	Errigal_Asset_Hook as Asset_Hook,
	Errigal_WP_Hook as WP_Hook,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Customiser\{
	Errigal_Custom_Breadcrumb as Breadcrumb,
	Errigal_Custom_Typography as Typography,
};
use Errigal\Navigation\Errigal_Main_Menu as Main_Menu;
use Errigal\Shared\{
	Errigal_Asset as Asset,
	Errigal_Utility as Utility,
	Errigal_Validator as Validator,
};

/**
 * This class is in charge of adding asset hooks.
 *
 * @since 1.1.0
 */
class Errigal_Asset_Hooks extends Errigal_Hooks_Base {
	/**
	 * Stylesheet name: IE.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const STYLE_THEME_IE = 'ie';

	/**
	 * IE script prefix.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const SCRIPT_PREFIX_IE = 'ie';

	/**
	 * Enqueues theme style.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function enqueue_theme_style() {
		Asset::enqueue_style(
			Asset::STYLE_THEME,
			Asset::get_theme_stylesheet_uri()
		);
	}

	/**
	 * Enqueues font styles.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function enqueue_font_styles() {
		Asset::enqueue_style(
			Typography::STYLE_BASE_GOOGLE_FONT,
			Typography::get_base_font_uri()
		);

		Asset::add_inline_style(
			Asset::STYLE_THEME,
			Typography::get_base_inline_font_style()
		);

		if ( ! ( Typography::has_unique_heading_font() || is_customize_preview() ) ) {
			return;
		}

		Asset::enqueue_style(
			Typography::STYLE_HEADING_GOOGLE_FONT,
			Typography::get_heading_font_uri()
		);

		Asset::add_inline_style(
			Asset::STYLE_THEME,
			Typography::get_heading_inline_font_style()
		);
	}

	/**
	 * Enqueues scripts.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	public function enqueue_scripts() {
		if ( Content::is_ie() ) {
			// Required to be loaded before other js files.
			Asset::enqueue_script( 'ie' );
		}

		if ( Main_Menu::can_show() ) {
			if ( wp_is_mobile() ) {
				Asset::enqueue_script( 'mobile-menu' );
			} else {
				Asset::enqueue_script( 'desktop-menu' );
			}
		}

		if (
			Breadcrumb::can_show() ||
			Breadcrumb::should_add_in_customise_preview()
		) {
			Asset::enqueue_script( 'breadcrumb' );
		}

		if ( wp_is_mobile() ) {
			Asset::enqueue_script( 'mobile' );
		} else {
			Asset::enqueue_script( 'desktop' );
		}

		if ( Content::has_comment_form() ) {
			$script_handle_comments = Asset::enqueue_script( 'comments' );

			// Comment form error comments.
			Asset::localise_script(
				$script_handle_comments,
				'ERRIGAL_COMMENT_FORM_ERRORS',
				array(
					'CommentRequired'      => esc_html__( 'Please enter your comment.', 'errigal' ),
					'AuthorRequired'       => esc_html__( 'Please enter your name.', 'errigal' ),
					'EmailAddressRequired' => esc_html__( 'Please enter your email address.', 'errigal' ),
					'EmailAddressInvalid'  => esc_html__( 'Please enter a valid email address.', 'errigal' ),
				)
			);

			if ( get_option( 'thread_comments' ) ) {
				// Enqueues comment reply script.
				wp_enqueue_script( 'comment-reply' );
			}
		}

		if ( Admin::is_admin_bar_showing() ) {
			// Required to be loaded after other js files.
			Asset::enqueue_script( 'admin-bar-showing-post' );
		}
	}

	/**
	 * Adds IE support to script file name.
	 *
	 * @since 1.1.0
	 *
	 * @see Errigal\Shared\Errigal_Asset::enqueue_script()
	 *
	 * @param string $script_file_name_without_extension Script file name without extension.
	 * @return string
	 */
	public function add_ie_support_to_script_file_name( $script_file_name_without_extension ) {
		if ( ! Validator::is_non_empty_string( $script_file_name_without_extension ) ) {
			return '';
		}

		// Adds 'ie-' prefix to script name if browsed on IE and file name is not just 'ie'.
		if (
			Content::is_ie() &&
			self::SCRIPT_PREFIX_IE !== $script_file_name_without_extension
		) {
			return sanitize_file_name(
				Utility::join(
					Utility::SEPARATOR_HYPHEN,
					self::SCRIPT_PREFIX_IE,
					$script_file_name_without_extension
				)
			);
		}

		return sanitize_file_name( $script_file_name_without_extension );
	}

	/**
	 * Adds IE support to stylesheet file name.
	 *
	 * @since 1.1.0
	 *
	 * @see Errigal\Shared\Errigal_Asset::get_stylesheet_uri()
	 *
	 * @param string $stylesheet_file_name_without_extension Stylesheet file name without extension.
	 * @return string
	 */
	public function add_ie_support_to_stylesheet_file_name( $stylesheet_file_name_without_extension ) {
		if ( ! Validator::is_non_empty_string( $stylesheet_file_name_without_extension ) ) {
			return '';
		}

		// Adds 'ie-' prefix to script name if browsed on IE and file name is not just 'ie'.
		if ( Content::is_ie() ) {
			return sanitize_file_name(
				str_replace(
					Asset::STYLE_THEME,
					self::STYLE_THEME_IE,
					$stylesheet_file_name_without_extension
				)
			);
		}

		return sanitize_file_name( $stylesheet_file_name_without_extension );
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		$this->add_style_hooks();
		$this->add_script_hooks();
		$this->add_ie_support_hooks();
	}

	/**
	 * Adds style hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	private function add_style_hooks() {
		add_action( WP_Hook::WP_HEAD, array( Typography::class, 'render_preconnect_links' ), WP_Hook::PRIORITY_LOWEST );
		add_action( WP_Hook::WP_ENQUEUE_SCRIPTS, array( $this, 'enqueue_theme_style' ) );
		add_action( WP_Hook::WP_ENQUEUE_SCRIPTS, array( $this, 'enqueue_font_styles' ) );
	}

	/**
	 * Adds script hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	private function add_script_hooks() {
		add_action( WP_Hook::WP_ENQUEUE_SCRIPTS, array( $this, 'enqueue_scripts' ) );
	}

	/**
	 * Adds IE support hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	private function add_ie_support_hooks() {
		add_filter( Asset_Hook::SCRIPT_FILE_NAME, array( $this, 'add_ie_support_to_script_file_name' ) );
		add_filter( Asset_Hook::STYLESHEET_FILE_NAME, array( $this, 'add_ie_support_to_stylesheet_file_name' ) );
	}
}
