<?php
/**
 * Server-side rendering of the `rover/listings` block.
 *
 * @package WordPress
 */

add_action( 'block_categories_all', 'rover_idx_register_block_category', 10, 2 );
add_action( 'init', 'rover_idx_block_listings', 5);
add_action( 'enqueue_block_editor_assets', 'rover_idx_enqueue_block_editor_assets' );

function rover_idx_enqueue_block_editor_assets() {
	#	Load block data first
	$rover_idx				= rover_idx_resolve();
	$region					= $rover_idx->get_first_region();

	if (!empty($region)) {
		$rover_content		= $rover_idx->CONTENT->rover_content(
			'get-block-data',
			array(
				'region'  => $region,
				'regions' => implode(',', array_keys($rover_idx->all_selected_regions))
			)
		);

		wp_register_script('rover-idx-block-data', false, array(), null);
		wp_enqueue_script('rover-idx-block-data');
		wp_add_inline_script(
			'rover-idx-block-data',
			sprintf(
				'window.ridx_block_data = %s; window.ridx_security = "%s";',
				$rover_content['the_html'],
				wp_create_nonce(roverKEYS::NONCE_DEF)
			),
			'before'
		);
	}

	// Enqueue other editor assets
	wp_enqueue_script('rover-idx-block-js-listings');
	wp_enqueue_style('rover-idx-block-css');
	}

function rover_idx_register_block_category( $categories, $post ) {
	return array_merge(
		$categories,
		array(
			array(
				'slug'  => 'rover-idx-data-blocks',
				'title' => 'Rover IDX',
				),
			)
		);
	}

function render_block_rover_idx_listings( $attributes, $content ) {

	$the_html					= array();
	$the_html[]					= '<div class="rover-idx-block listings">' . $attributes['message'] . '></div>';
	$the_html[]					= '<input type="hidden" id="wp_security" name="security" value="'.wp_create_nonce(roverKEYS::NONCE_DEF).'" />';

	return implode('', $the_html);
	}

function rover_idx_block_listings() {

#	global						$rover_idx;
	$rover_idx					= rover_idx_resolve();

	$region						= $rover_idx->get_first_region();
	if (empty($region))
		{
		#	Site not yet setup
		return;
		}

	#	Load our Block js

	$dep_asset_file				= include( plugin_dir_path( __FILE__ ) . 'listings/listings.asset.php');

	wp_register_script(
		'rover-idx-block-js-listings',
		ROVER_IDX_PLUGIN_URL . 'blocks/listings/listings.js',
		array_merge(
			$dep_asset_file['dependencies'],
			array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor' )
			),
		$ver = strtotime("Now"),
		false		//	Load in header for editor
		);

#	wp_enqueue_script('rover-idx-block-js-listings') in rover_idx_block_listings()
#	should not be there at all since enqueue_block_editor_assets already handles it
#
#	wp_enqueue_script( 'rover-idx-block-js-listings' );

	wp_register_style(
					'rover-idx-block-css',
					ROVER_IDX_PLUGIN_URL . 'blocks/listings/editor.css',
					array(),
					$ver = strtotime("Now"),	#	roverVERSIONS - JS,
					'all'
					);
	wp_enqueue_style(	'rover-idx-block-css' );

#	register_block_type(
#		__DIR__ . '/listings',
#		);

	register_block_type( 'rover-idx/listings', array(
		'render_callback' => 'rover_idx_render_listings_block',
		'attributes' => array(
			'format' => array(
				'type' => 'string',
				'default' => 'full',
				),
			'cities' => array(
				'type' => 'string',
				'default' => '',
				),
			'prop_types' => array(
				'type' => 'string',
				'default' => '',
				),
			'max_price' => array(
				'type' => 'integer',
				'default' => 0,
				),
			'min_price' => array(
				'type' => 'integer',
				'default' => 0,
				),
			'min_beds' => array(
				'type' => 'integer',
				'default' => 0,
				),
			'min_baths' => array(
				'type' => 'integer',
				'default' => 0,
				),
			'search_panel_layout' => array(
				'type' => 'string',
				'default' => '',
				),
			'listing_layout' => array(
				'type' => 'string',
				'default' => '',
				),
			'status' => array(
				'type' => 'string',
				'default' => '',
				),
			'area_autocomplete_key' => array(
				'type' => 'string',
				'default' => '',
				),
			'area_autocomplete_val' => array(
				'type' => 'string',
				'default' => '',
				),
			'overall_layout' => array(
				'type' => 'string',
				'default' => '',
				),
			'listing_office_mlsid' => array(
				'type' => 'string',
				'default' => '',
				),
			'prioritize_office_listings' => array(
				'type' => 'string',
				'default' => '',
				),
			'listing_date' => array(
				'type' => 'string',
				'default' => '',
				),
			'sort_by' => array(
				'type' => 'string',
				'default' => '',
				),
			'listings_per_page' => array(
				'type' => 'integer',
				'default' => 0,
				),
			'full_page_layout' => array(
				'type' => 'string',
				'default' => '',
				),

			// ADD THESE TWO:
			'area_autocomplete_options' => array(
				'type' => 'array',
				'default' => array(),
				),
			'content' => array(
				'type' => 'string',
				'default' => '',
				),
			),
		) );

	}



// Render callback - builds and executes the shortcode
function rover_idx_render_listings_block( $attributes ) {
	// Build shortcode from attributes
	$shortcode_params = array();

	foreach ( $attributes as $key => $value ) {
		// Skip internal attributes
		if ( in_array( $key, array( 'content', 'area_autocomplete_options', 'overall_layout', 'full_page_layout' ) ) ) {
			continue;
		}

		// Handle area autocomplete specially
		if ( $key === 'area_autocomplete_val' && ! empty( $value ) ) {
			$area_key = ! empty( $attributes['area_autocomplete_key'] ) ? $attributes['area_autocomplete_key'] : 'subdivision';
			$shortcode_params[] = $area_key . '="' . esc_attr( $value ) . '"';
			continue;
		}

		// Skip the key (we handled it above)
		if ( $key === 'area_autocomplete_key' ) {
			continue;
		}

		// Handle other attributes
		if ( is_numeric( $value ) ) {
			if ( $value > 0 ) {
				$shortcode_params[] = $key . '="' . esc_attr( $value ) . '"';
			}
		} else {
			if ( ! empty( $value ) ) {
				$shortcode_params[] = $key . '="' . esc_attr( $value ) . '"';
			}
		}
	}

	$shortcode = '[rover_idx_listings ' . implode( ' ', $shortcode_params ) . ']';

	// Execute the shortcode and return the output
	return do_shortcode( $shortcode );
}
