<?php
/**
 * Blocks Handler
 *
 * @package NemtlyBooking
 */

namespace NemtlyBooking\Core;

/**
 * Blocks class
 */
class Blocks {

    /**
     * Initialize blocks
     */
    public function __construct() {
        add_action( 'init', [ $this, 'register_blocks' ] );
    }

    /**
     * Register blocks
     */
    public function register_blocks() {

        // Register Event Date Block
        register_block_type(
            'nemtly/event-date',
            [
                'render_callback' => [ $this, 'render_event_date' ],
            ]
        );

        // Register Booking Trigger Block (formerly Book Now Button)
        register_block_type(
            'nemtly/booking-trigger',
            [
                'render_callback' => [ $this, 'render_booking_trigger' ],
            ]
        );

        // Register Event Location Block
        register_block_type(
            'nemtly/event-location',
            [
                'render_callback' => [ $this, 'render_event_location' ],
            ]
        );

        // Register Event Price Block
        register_block_type(
            'nemtly/price',
            [
                'render_callback' => [ $this, 'render_event_price' ],
            ]
        );

        // Register Query Variation script
        $variation_asset_path = NEMTLY_BOOKING_DIR . 'assets/js/blocks/events-loop.asset.php';
        if ( file_exists( $variation_asset_path ) ) {
            $variation_asset = require $variation_asset_path;
            wp_register_script(
                'nemtly-events-loop',
                NEMTLY_BOOKING_URL . 'assets/js/blocks/events-loop.js',
                $variation_asset['dependencies'],
                $variation_asset['version'],
                true
            );
            add_action(
                'enqueue_block_editor_assets',
                function () {
					wp_enqueue_script( 'nemtly-events-loop' );
				}
            );
        }
    }


    /**
     * Render Event Date Block
     *
     * @since NEMTLY_BOOKING_SINCE
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block content.
     * @param \WP_Block $block      Block instance.
     * @return string
     */
    public function render_event_date( $attributes, $content, $block ) {
        $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : 0;

        if ( empty( $post_id ) || get_post_type( $post_id ) !== 'nemtly_event' ) {
            return '';
        }

        $event_date = get_post_meta( $post_id, 'nemtly_event_date', true );

        if ( empty( $event_date ) ) {
            return '';
        }

        $date           = new \DateTime( $event_date );
        $formatted_date = $date->format( 'M j, Y' ); // TODO: Use attributes for format

        $wrapper_attributes = get_block_wrapper_attributes(
            [
                'class' => 'nemtly-event-date',
            ]
        );

        return sprintf(
            '<div %1$s><time datetime="%2$s">%3$s</time></div>',
            $wrapper_attributes,
            esc_attr( $event_date ),
            esc_html( $formatted_date )
        );
    }

    /**
     * Render Booking Trigger Block
     *
     * @since NEMTLY_BOOKING_SINCE
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block content.
     * @param \WP_Block $block      Block instance.
     * @return string
     */
    public function render_booking_trigger( $attributes, $content, $block ) {
        $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : 0;

        if ( empty( $post_id ) || get_post_type( $post_id ) !== 'nemtly_event' ) {
            return '';
        }

        $button_text = isset( $attributes['text'] ) ? $attributes['text'] : __( 'Book Now', 'nemtly-booking' );
        $class       = isset( $attributes['className'] ) ? $attributes['className'] : '';

        $wrapper_attributes = get_block_wrapper_attributes(
            [
                'class'         => 'nemtly-booking-trigger ' . $class,
                'data-event-id' => $post_id,
            ]
        );

        return sprintf(
            '<button %1$s>%2$s</button>',
            $wrapper_attributes,
            esc_html( $button_text )
        );
    }

    /**
     * Render Event Location Block
     *
     * @since NEMTLY_BOOKING_SINCE
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block content.
     * @param \WP_Block $block      Block instance.
     * @return string
     */
    public function render_event_location( $attributes, $content, $block ) {
        $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : 0;

        if ( empty( $post_id ) || get_post_type( $post_id ) !== 'nemtly_event' ) {
            return '';
        }

        $location = get_post_meta( $post_id, 'nemtly_event_location', true );

        if ( empty( $location ) ) {
            return '';
        }

        $show_icon = isset( $attributes['showIcon'] ) ? $attributes['showIcon'] : true;

        $wrapper_attributes = get_block_wrapper_attributes(
            [
                'class' => 'nemtly-event-location',
            ]
        );

        $icon_html = '';
        if ( $show_icon ) {
            $icon_html = '<span class="dashicons dashicons-location" aria-hidden="true"></span> ';
        }

        return sprintf(
            '<div %1$s>%2$s%3$s</div>',
            $wrapper_attributes,
            $icon_html,
            esc_html( $location )
        );
    }

    /**
     * Render Event Price Block
     *
     * @since NEMTLY_BOOKING_SINCE
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block content.
     * @param \WP_Block $block      Block instance.
     * @return string
     */
    public function render_event_price( $attributes, $content, $block ) {
        $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : 0;

        if ( empty( $post_id ) || get_post_type( $post_id ) !== 'nemtly_event' ) {
            return '';
        }

        $price    = get_post_meta( $post_id, 'nemtly_event_price', true );
        $currency = get_option( 'nemtly_currency_symbol', '$' );

        if ( empty( $price ) && '0' !== $price ) {
            return '';
        }

        // Check if free
        if ( '0' === $price ) {
            $price_display = __( 'Free', 'nemtly-booking' );
        } else {
            $price_display = $currency . number_format_i18n( $price, 2 );
        }

        $wrapper_attributes = get_block_wrapper_attributes(
            [
                'class' => 'nemtly-event-price',
            ]
        );

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