<?php
/**
 * Plugin blocks.
 *
 * @package HBFWC/Blocks
 */

namespace HBFWC;

defined( 'ABSPATH' ) || exit;

/**
 * Class Blocks.
 */
class Blocks {
	/**
	 * Register all plugin blocks.
	 */
	public static function init() {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		add_filter( 'block_categories_all', array( __CLASS__, 'register_block_category' ), 10, 2 );
		add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_editor_assets' ) );

		$editor_script_handle = 'hbfwc-blocks';
		$editor_script_src    = plugins_url( 'assets/js/blocks.js', HBFWC_FILE );

		wp_register_script(
			$editor_script_handle,
			$editor_script_src,
			array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-block-editor', 'wp-components', 'wp-server-side-render' ),
			time(),
			true
		);

		register_block_type(
			'hbfwc/availability',
			array(
				'api_version'     => 2,
				'supports'        => array(
					'align' => [ 'wide', 'full' ],
				),
				'render_callback' => array( __CLASS__, 'render_availability' ),
				'editor_script'   => $editor_script_handle,
			)
		);

		register_block_type(
			'hbfwc/search-form',
			array(
				'api_version'     => 2,
				'supports'        => array(
					'align' => [ 'wide', 'full' ],
				),
				'attributes'      => array(
					'page' => array(
						'type'    => 'string',
						'default' => '',
					),
				),
				'render_callback' => array( __CLASS__, 'render_search_form' ),
				'editor_script'   => $editor_script_handle,
			)
		);
	}

	/**
	 * Register custom block category.
	 *
	 * @param array $categories Existing categories.
	 * @return array
	 */
	public static function register_block_category( $categories ): array {
		foreach ( $categories as $category ) {
			if ( isset( $category['slug'] ) && 'hbfwc' === $category['slug'] ) {
				return $categories;
			}
		}

		$categories[] = array(
			'slug'  => 'hbfwc',
			'title' => __( 'Hotel Booking for WooCommerce', 'hotel-booking-for-woocommerce' ),
			'icon'  => 'building',
		);

		return $categories;
	}

	/**
	 * Enqueue assets for editor block previews.
	 */
	public static function enqueue_editor_assets() {
		if ( ! is_admin() ) {
			return;
		}

		$version = HBFWC_VERSION;

		wp_enqueue_style( 'hbfwc-editor-fontawesome-6', plugins_url( 'assets/css/libs/font-awesome-6.min.css', HBFWC_FILE ), array(), $version );
		wp_enqueue_style( 'hbfwc-editor-fontawesome-6-brands', plugins_url( 'assets/css/libs/font-awesome-6-brands.min.css', HBFWC_FILE ), array(), $version );
		wp_enqueue_style( 'hbfwc-editor-ionicons', plugins_url( 'assets/css/libs/ionicons.min.css', HBFWC_FILE ), array(), $version );
		wp_enqueue_style( 'hbfwc-editor-themify', plugins_url( 'assets/css/libs/themify-icons.min.css', HBFWC_FILE ), array(), $version );
		wp_enqueue_style( 'hbfwc-editor-daterangepicker', plugins_url( 'assets/css/daterangepicker.css', HBFWC_FILE ), array(), $version );
		wp_enqueue_style(
			'hbfwc-editor',
			plugins_url( 'assets/css/hbfwc.css', HBFWC_FILE ),
			array( 'hbfwc-editor-daterangepicker', 'hbfwc-editor-fontawesome-6', 'hbfwc-editor-fontawesome-6-brands', 'hbfwc-editor-ionicons', 'hbfwc-editor-themify' ),
			$version
		);

		wp_enqueue_style( 'hbfwc-block-editor-preview', plugins_url( 'assets/client/blocks/editor.css', HBFWC_FILE ), array(), $version );
	}

	/**
	 * Render availability block.
	 *
	 * @return string
	 */
	public static function render_availability( $attributes = array(), $content = '', $block = null ): string {
		$wrapper_attributes = function_exists( 'get_block_wrapper_attributes' ) ? get_block_wrapper_attributes() : '';
		$output             = Shortcodes::availability();

		return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output );
	}

	/**
	 * Render search form block.
	 *
	 * @param array $attributes Block attributes.
	 * @return string
	 */
	public static function render_search_form( $attributes = array(), $content = '', $block = null ): string {
		$atts = array(
			'page' => isset( $attributes['page'] ) ? $attributes['page'] : false,
		);
		$wrapper_attributes = function_exists( 'get_block_wrapper_attributes' ) ? get_block_wrapper_attributes() : '';
		$output             = Shortcodes::search_form( $atts );

		return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output );
	}
}