<?php
/**
 * Abstract base class for Smart Post Show Blocks.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */

namespace SmartPostShow\Blocks;

use SmartPostShow\Blocks\Block_Base;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Post Grid Four Block Class.
 */
class Post_Grid_Four extends Block_Base {
	/**
	 * Set_block_properties
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name     = 'post-grid-four';
		$this->scripts        = array( 'sp_smart_post_blocks_script_js' );
		$this->styles         = array( 'sp_smart_post_blocks_css', 'sp_smart_post_blocks_social_icons_style', 'sp_smart_post_blocks_google_fonts' );
		$this->editor_scripts = array( 'sp_smart_post_blocks_index_js' );
		$this->editor_styles  = array( 'sp_smart_post_blocks_editor_style' );
		$this->keywords       = array( 'Smart post', 'Grid', 'Live Filter', 'Pagination', ' Ajax Live Filter' );
	}
	/**
	 * Render block
	 *
	 * @param  array $attributes attributes.
	 * @param  mixed $content content.
	 * @param  array $blocks blocks.
	 * @return string        Rendered HTML.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {
		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return $content;
		}

		$is_license_activated = $this->is_license_activated();
		if ( 'valid' !== $is_license_activated ) {
			return $is_license_activated;
		}

		global $sp_page_builder_id;

		$page_id = ! empty( $sp_page_builder_id )
			? (int) $sp_page_builder_id
			: (int) get_the_ID();

		$attributes['page_id'] = $page_id;

		$sp_user_additional_class = $attributes['additionalCssClass'] ?? '';
		$sp_block_name            = $attributes['blockName'] ?? '';
		$align                    = $attributes['align'] ?? '';

		unset(
			$attributes['dynamicCss'],
			$attributes['fontListsEditPage'],
			$attributes['fontLists']
		);

		$unique_id   = $attributes['uniqueId'] ?? '';
		$sp_block_id = $attributes['spBlockId'] ?? ''; // Not changeable id.
		$block_name  = $attributes['blockName'] ?? '';
		$layout      = $attributes['postGridLayout'] ?? '';

		$preloader_enable = $attributes['preloaderEnable'] ?? false;
		$preloader_svg    = SP_PCP_URL . 'public/assets/img/preloader.svg';
		$post_not_found   = ! empty( $attributes['noResultFoundResult'] ) ? $attributes['noResultFoundResult'] : 'No post found';

		$json_string = str_replace( 'u0022', '"', $attributes['postQuery'] );
		$query_attr  = ( ! empty( $json_string ) && isset( $json_string ) ) ? (array) json_decode( $json_string ) : $attributes;

		$category_position = $attributes['catTabCategoryPosition'] ?? false;

		$category_position = $category_position ? ' sp-cat-position-' . $category_position : '';

		$post_query     = $this->get_cached_post_query_result( $query_attr, $unique_id, $sp_block_id );
		$total_pages    = $post_query[1];
		$post_query     = $post_query[0];
		$align_class    = isset( $align ) ? 'align' . $align : '';
		$query_attr     = wp_json_encode( $query_attr );
		$block_location = str_replace( 'u0022', '"', $attributes['blockLocation'] );
		$block_location = $block_location ?? '';

		$pagination_type     = $attributes['paginationTypeParent'] ?? '';
		$pagination_position = $attributes['paginationPosition'] ?? 'bottom';

		$pagination_class = 'sp-smart-post-block-with-pagination pagination-' . ( 'navigation' === $pagination_type ? $pagination_position : 'bottom' );

		ob_start();
		?>
			<div class="<?php echo esc_attr( $align_class ); ?>">
			<div id="<?php echo esc_attr( $unique_id ); ?>" class="sp-smart-post-wrapper sp-smart-<?php echo esc_attr( $block_name . $category_position ); ?> <?php echo esc_attr( $sp_user_additional_class ); ?> <?php echo esc_attr( $pagination_class ); ?>" data-query='<?php echo esc_attr( $query_attr ); ?>' data-blockid="<?php echo esc_attr( $sp_block_id ); ?>" data-pageid="<?php echo esc_attr( $attributes['page_id'] ); ?>"  data-location="<?php echo esc_attr( $block_location ); ?>">
				<div class="sp-smart-post-show-pro-pre-query">
					<?php if ( empty( $post_query ) ) { ?>
					<div class="sp-smart-post-no-post sp-d-hidden">
						<h4><?php echo esc_html( $post_not_found ); ?></h4>
					</div>
					<?php } ?>

				</div>
				<div class="sp-smart-show-pro grid-four-container sp-smart-post-items <?php echo esc_attr( $layout ); ?>">
				<?php
				Template::post_grid_four( $attributes, $post_query );
				?>
				</div>
				<?php
				// Pagination.
				if ( $attributes['paginationEnable'] && $total_pages > 1 && ! empty( $blocks->inner_blocks ) ) {
					$pagination_attributes = Template::pagination_attributes( $blocks );
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo Template_Part::pagination_html( $pagination_attributes, $query_attr, $total_pages, $unique_id ); // Escaped in function.
				}
				add_action(
					'wp_footer',
					function () use ( $attributes, $post_query ) {
						Template_Part::modal_lightbox( $attributes, $post_query );
					},
					20
				);
				?>
			</div>
			</div>
		<?php
		return ob_get_clean();
	}
}
