<?php
namespace Automattic\Kkart\Blocks;

use Automattic\Kkart\Blocks\Package;
use Automattic\Kkart\Blocks\Assets\Api as AssetApi;

/**
 * Assets class.
 * Initializes block assets.
 *
 * @internal
 */
class Assets {

	/**
	 * Initialize class features on init.
	 *
	 * @since 2.5.0
	 * Moved most initialization to BootStrap and AssetDataRegistry
	 * classes as a part of ongoing refactor
	 */
	public static function init() {
		add_action( 'init', array( __CLASS__, 'register_assets' ) );
		add_action( 'body_class', array( __CLASS__, 'add_theme_body_class' ), 1 );
		add_action( 'admin_body_class', array( __CLASS__, 'add_theme_admin_body_class' ), 1 );
		add_filter( 'kkart_shared_settings', array( __CLASS__, 'get_kkart_block_data' ) );
	}

	/**
	 * Register block scripts & styles.
	 *
	 * @since 2.5.0
	 * Moved data related enqueuing to new AssetDataRegistry class
	 * as part of ongoing refactoring.
	 */
	public static function register_assets() {
		$asset_api = Package::container()->get( AssetApi::class );

		// @todo Remove fix to load our stylesheets after editor CSS.
		// See #3068 for the rationale of this fix. It should be no longer
		// necessary when the editor is loaded in an iframe (https://github.com/WordPress/gutenberg/issues/20797).
		if ( is_admin() ) {
			$block_style_dependencies = array( 'wp-edit-post' );
		} else {
			$block_style_dependencies = array();
		}

		self::register_style( 'kkart-block-vendors-style', plugins_url( $asset_api->get_block_asset_build_path( 'vendors-style', 'css' ), __DIR__ ), $block_style_dependencies );
		self::register_style( 'kkart-block-editor', plugins_url( $asset_api->get_block_asset_build_path( 'editor', 'css' ), __DIR__ ), array( 'wp-edit-blocks' ) );
		wp_style_add_data( 'kkart-block-editor', 'rtl', 'replace' );
		self::register_style( 'kkart-block-style', plugins_url( $asset_api->get_block_asset_build_path( 'style', 'css' ), __DIR__ ), array( 'kkart-block-vendors-style' ) );
		wp_style_add_data( 'kkart-block-style', 'rtl', 'replace' );

		// Shared libraries and components across all blocks.
		$asset_api->register_script( 'kkart-blocks-middleware', 'build/kkart-blocks-middleware.js', [], false );
		$asset_api->register_script( 'kkart-blocks-data-store', 'build/kkart-blocks-data.js', [ 'kkart-blocks-middleware' ], false );
		$asset_api->register_script( 'kkart-blocks', $asset_api->get_block_asset_build_path( 'blocks' ), [], false );
		$asset_api->register_script( 'kkart-vendors', $asset_api->get_block_asset_build_path( 'vendors' ), [], false );
		$asset_api->register_script( 'kkart-blocks-registry', 'build/kkart-blocks-registry.js', [], false );
		$asset_api->register_script( 'kkart-shared-context', 'build/kkart-shared-context.js', [], false );
		$asset_api->register_script( 'kkart-shared-hocs', 'build/kkart-shared-hocs.js', [], false );

		// Inline data.
		wp_add_inline_script(
			'kkart-blocks-middleware',
			"var wcStoreApiNonce = '" . esc_js( wp_create_nonce( 'kkart_store_api' ) ) . "';",
			'before'
		);

		// Individual blocks.
		$block_dependencies = array( 'kkart-vendors', 'kkart-blocks' );

		$asset_api->register_script( 'kkart-handpicked-products', $asset_api->get_block_asset_build_path( 'handpicked-products' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-best-sellers', $asset_api->get_block_asset_build_path( 'product-best-sellers' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-category', $asset_api->get_block_asset_build_path( 'product-category' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-new', $asset_api->get_block_asset_build_path( 'product-new' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-on-sale', $asset_api->get_block_asset_build_path( 'product-on-sale' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-top-rated', $asset_api->get_block_asset_build_path( 'product-top-rated' ), $block_dependencies );
		$asset_api->register_script( 'kkart-products-by-attribute', $asset_api->get_block_asset_build_path( 'products-by-attribute' ), $block_dependencies );
		$asset_api->register_script( 'kkart-featured-product', $asset_api->get_block_asset_build_path( 'featured-product' ), $block_dependencies );
		$asset_api->register_script( 'kkart-featured-category', $asset_api->get_block_asset_build_path( 'featured-category' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-categories', $asset_api->get_block_asset_build_path( 'product-categories' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-tag', $asset_api->get_block_asset_build_path( 'product-tag' ), $block_dependencies );
		$asset_api->register_script( 'kkart-all-reviews', $asset_api->get_block_asset_build_path( 'all-reviews' ), $block_dependencies );
		$asset_api->register_script( 'kkart-reviews-by-product', $asset_api->get_block_asset_build_path( 'reviews-by-product' ), $block_dependencies );
		$asset_api->register_script( 'kkart-reviews-by-category', $asset_api->get_block_asset_build_path( 'reviews-by-category' ), $block_dependencies );
		$asset_api->register_script( 'kkart-product-search', $asset_api->get_block_asset_build_path( 'product-search' ), $block_dependencies );
		$asset_api->register_script( 'kkart-all-products', $asset_api->get_block_asset_build_path( 'all-products' ), $block_dependencies );
		$asset_api->register_script( 'kkart-price-filter', $asset_api->get_block_asset_build_path( 'price-filter' ), $block_dependencies );
		$asset_api->register_script( 'kkart-attribute-filter', $asset_api->get_block_asset_build_path( 'attribute-filter' ), $block_dependencies );
		$asset_api->register_script( 'kkart-active-filters', $asset_api->get_block_asset_build_path( 'active-filters' ), $block_dependencies );

		if ( Package::is_experimental_build() ) {
			$asset_api->register_script( 'kkart-single-product-block', $asset_api->get_block_asset_build_path( 'single-product' ), $block_dependencies );
		}

		if ( Package::is_feature_plugin_build() ) {
			$asset_api->register_script( 'kkart-checkout-block', $asset_api->get_block_asset_build_path( 'checkout' ), $block_dependencies );
			$asset_api->register_script( 'kkart-cart-block', $asset_api->get_block_asset_build_path( 'cart' ), $block_dependencies );
		}
	}

	/**
	 * Add body classes.
	 *
	 * @param array $classes Array of CSS classnames.
	 * @return array Modified array of CSS classnames.
	 */
	public static function add_theme_body_class( $classes = [] ) {
		$classes[] = 'theme-' . get_template();
		return $classes;
	}

	/**
	 * Add theme class to admin body.
	 *
	 * @param array $classes String with the CSS classnames.
	 * @return array Modified string of CSS classnames.
	 */
	public static function add_theme_admin_body_class( $classes = '' ) {
		$classes .= ' theme-' . get_template();
		return $classes;
	}

	/**
	 * Returns block-related data for enqueued kkart-block-settings script.
	 *
	 * This is used to map site settings & data into JS-accessible variables.
	 *
	 * @param array $settings The original settings array from the filter.
	 *
	 * @since 2.4.0
	 * @since 2.5.0 returned merged data along with incoming $settings
	 */
	public static function get_kkart_block_data( $settings ) {
		$tag_count      = wp_count_terms( 'product_tag' );
		$product_counts = wp_count_posts( 'product' );
		$page_ids       = [
			'shop'     => kkart_get_page_id( 'shop' ),
			'cart'     => kkart_get_page_id( 'cart' ),
			'checkout' => kkart_get_page_id( 'checkout' ),
			'privacy'  => kkart_privacy_policy_page_id(),
			'terms'    => kkart_terms_and_conditions_page_id(),
		];
		$checkout       = KKART()->checkout();

		// Global settings used in each block.
		return array_merge(
			$settings,
			[
				'currentUserIsAdmin'            => is_user_logged_in() && current_user_can( 'manage_kkart' ),
				'min_columns'                   => kkart_get_theme_support( 'product_blocks::min_columns', 1 ),
				'max_columns'                   => kkart_get_theme_support( 'product_blocks::max_columns', 6 ),
				'default_columns'               => kkart_get_theme_support( 'product_blocks::default_columns', 3 ),
				'min_rows'                      => kkart_get_theme_support( 'product_blocks::min_rows', 1 ),
				'max_rows'                      => kkart_get_theme_support( 'product_blocks::max_rows', 6 ),
				'default_rows'                  => kkart_get_theme_support( 'product_blocks::default_rows', 3 ),
				'thumbnail_size'                => kkart_get_theme_support( 'thumbnail_image_width', 300 ),
				'placeholderImgSrc'             => kkart_placeholder_img_src(),
				'min_height'                    => kkart_get_theme_support( 'featured_block::min_height', 500 ),
				'default_height'                => kkart_get_theme_support( 'featured_block::default_height', 500 ),
				'isLargeCatalog'                => $product_counts->publish > 100,
				'limitTags'                     => $tag_count > 100,
				'hasTags'                       => $tag_count > 0,
				'taxesEnabled'                  => kkart_tax_enabled(),
				'couponsEnabled'                => kkart_coupons_enabled(),
				'shippingEnabled'               => kkart_shipping_enabled(),
				'displayItemizedTaxes'          => 'itemized' === get_option( 'kkart_tax_total_display' ),
				'displayShopPricesIncludingTax' => 'incl' === get_option( 'kkart_tax_display_shop' ),
				'displayCartPricesIncludingTax' => 'incl' === get_option( 'kkart_tax_display_cart' ),
				'checkoutShowLoginReminder'     => 'yes' === get_option( 'kkart_enable_checkout_login_reminder' ),
				'showAvatars'                   => '1' === get_option( 'show_avatars' ),
				'reviewRatingsEnabled'          => kkart_review_ratings_enabled(),
				'productCount'                  => array_sum( (array) $product_counts ),
				'attributes'                    => array_values( kkart_get_attribute_taxonomies() ),
				'isShippingCalculatorEnabled'   => filter_var( get_option( 'kkart_enable_shipping_calc' ), FILTER_VALIDATE_BOOLEAN ),
				'isShippingCostHidden'          => filter_var( get_option( 'kkart_shipping_cost_requires_address' ), FILTER_VALIDATE_BOOLEAN ),
				'wcBlocksAssetUrl'              => plugins_url( 'assets/', __DIR__ ),
				'wcBlocksBuildUrl'              => plugins_url( 'build/', __DIR__ ),
				'restApiRoutes'                 => [
					'/kkart/store' => array_keys( Package::container()->get( RestApi::class )->get_routes_from_namespace( 'kkart/store' ) ),
				],
				'homeUrl'                       => esc_url( home_url( '/' ) ),
				'storePages'                    => [
					'shop'     => self::format_page_resource( $page_ids['shop'] ),
					'cart'     => self::format_page_resource( $page_ids['cart'] ),
					'checkout' => self::format_page_resource( $page_ids['checkout'] ),
					'privacy'  => self::format_page_resource( $page_ids['privacy'] ),
					'terms'    => self::format_page_resource( $page_ids['terms'] ),
				],
				'checkoutAllowsGuest'           => $checkout instanceof \KKART_Checkout && false === filter_var(
					$checkout->is_registration_required(),
					FILTER_VALIDATE_BOOLEAN
				),
				'checkoutAllowsSignup'          => $checkout instanceof \KKART_Checkout && filter_var(
					$checkout->is_registration_enabled(),
					FILTER_VALIDATE_BOOLEAN
				),
				'baseLocation'                  => kkart_get_base_location(),
				'kkartBlocksPhase'        => KKART_BLOCKS_PHASE,
				'hasDarkEditorStyleSupport'     => current_theme_supports( 'dark-editor-style' ),
				'loginUrl'                      => wp_login_url(),

				/*
				 * translators: If your word count is based on single characters (e.g. East Asian characters),
				 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
				 * Do not translate into your own language.
				 */
				'wordCountType'                 => _x( 'words', 'Word count type. Do not translate!', 'kkart' ),
			]
		);
	}

	/**
	 * Format a page object into a standard array of data.
	 *
	 * @param WP_Post|int $page Page object or ID.
	 * @return array
	 */
	protected static function format_page_resource( $page ) {
		if ( is_numeric( $page ) && $page > 0 ) {
			$page = get_post( $page );
		}
		if ( ! is_a( $page, '\WP_Post' ) ) {
			return [
				'id'        => 0,
				'title'     => '',
				'permalink' => false,
			];
		}
		return [
			'id'        => $page->ID,
			'title'     => $page->post_title,
			'permalink' => get_permalink( $page->ID ),
		];
	}

	/**
	 * Get the file modified time as a cache buster if we're in dev mode.
	 *
	 * @param string $file Local path to the file.
	 * @return string The cache buster value to use for the given file.
	 */
	protected static function get_file_version( $file ) {
		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\Kkart\Blocks\Package::get_path() . $file ) ) {
			return filemtime( \Automattic\Kkart\Blocks\Package::get_path() . $file );
		}
		return \Automattic\Kkart\Blocks\Package::get_version();
	}

	/**
	 * Queues a block script in the frontend.
	 *
	 * @since 2.3.0
	 * @since 2.6.0 Changed $name to $script_name and added $handle argument.
	 * @since 2.9.0 Made it so scripts are not loaded in admin pages.
	 *
	 * @param string $script_name  Name of the script used to identify the file inside build folder.
	 * @param string $handle       Optional. Provided if the handle should be different than the script name. `kkart-` prefix automatically added.
	 * @param array  $dependencies Optional. An array of registered script handles this script depends on. Default empty array.
	 */
	public static function register_block_script( $script_name, $handle = '', $dependencies = [] ) {
		$asset_api = Package::container()->get( AssetApi::class );
		$asset_api->register_block_script( $script_name, $handle, $dependencies );
	}

	/**
	 * Registers a style according to `wp_register_style`.
	 *
	 * @since 2.0.0
	 *
	 * @param string $handle Name of the stylesheet. Should be unique.
	 * @param string $src    Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
	 * @param array  $deps   Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
	 * @param string $media  Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like
	 *                       'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
	 */
	protected static function register_style( $handle, $src, $deps = [], $media = 'all' ) {
		$filename = str_replace( plugins_url( '/', __DIR__ ), '', $src );
		$ver      = self::get_file_version( $filename );
		wp_register_style( $handle, $src, $deps, $ver, $media );
	}
}
