<?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;
}

/**
 * Thumbnail Slider Two Block Class.
 */
class Thumbnail_Slider_Two extends Block_Base {
	/**
	 * Set_block_properties
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name     = 'thumbnail-slider-two';
		$this->scripts        = array( 'pcp_swiper', 'sp_smart_post_blocks_script_js' );
		$this->styles         = array( 'pcp_swiper', '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', 'Slider', 'Thumbnails Slider' );
	}
	/**
	 * Render Thumbnail Slider block.
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content Block content.
	 * @param array  $blocks Inner blocks.
	 * @return string $content Rendered HTML.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {
		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return $content;
		}

		if ( 'valid' !== $this->is_license_activated() ) {
			return $this->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;
		$unique_id             = $attributes['uniqueId'] ?? '';
		$sp_block_id           = $attributes['spBlockId'] ?? ''; // Not changeable id.
		$block_name            = $attributes['blockName'] ?? '';
		$layout                = $attributes['thumbnailSliderTwoLayout'] ?? '';
		$animation_effect      = $attributes['thumbnailTwoAnimationEffect'] ?? '';
		$arrow_on_hover        = $attributes['navArrowVisibilityOnHover'] ?? '';
		$arrow_style           = $attributes['carouselArrowStyle'] ?? '';
		$infinite_loop         = $attributes['infiniteLoop'] ?? false;
		$post_thumbnail_pos    = $attributes['thumbnailTwoPosition'] ?? 'bottom';
		$show_nav_arrow        = $attributes['carouselNavArrow'] ?? false;
		$thumb_alignment       = $attributes['thumbnailTwoAlignment'] ?? '';
		$progress_pos          = $attributes['thumbnailProgressPosition'] ?? '';
		$preloader_enable      = $attributes['thumbnailTwoGeneralPreloader'] ?? false;
		$post_not_found        = ! empty( $attributes['noResultFoundResult'] ) ? $attributes['noResultFoundResult'] : 'No post found';
		$preloader_svg         = SP_PCP_URL . 'public/assets/img/preloader.svg';

		$thumbs_data              = $attributes['thumbnailsData'] ?? '';
		$main_data                = $attributes['carouselData'] ?? '';
		$thumbs_per_slide         = $attributes['thumbnailItemsPerSlide'] ?? 3;
		$align                    = $attributes['align'] ?? '';
		$align_class              = isset( $align ) ? 'align' . $align : '';
		$sp_user_additional_class = $attributes['additionalCssClass'] ?? '';
		$category_position        = $attributes['catTabCategoryPosition'] ?? '';

		$background_layout_class = 'sp-smart-post-background-layout';

		$query_attr = ( ! empty( $attributes['postQuery'] ) && isset( $attributes['postQuery'] ) ) ? (array) json_decode( $attributes['postQuery'] ) : $attributes;

		$thumb_posts = $this->get_cached_post_query_result( $query_attr, $unique_id, $sp_block_id );
		$thumb_posts = $thumb_posts[0];

		// Some content part hide (title part, meta data part).
		$hide_part = array();
		switch ( $layout ) {
			case 'thumbnail-slider-two-layout-one':
			case 'thumbnail-slider-two-layout-two':
					$hide_part = array( 'feature_image', 'taxonomy' );
				break;
			case 'thumbnail-slider-two-layout-three':
			case 'thumbnail-slider-two-layout-four':
			case 'thumbnail-slider-two-layout-five':
					$hide_part = array( 'readMoreButton', 'metadata', 'excerpt', 'socialShare' );
				break;
		}

		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 ); ?> <?php echo esc_attr( $sp_user_additional_class ); ?>">
			<div class="sp-smart-post-show-pro-pre-query">
				<?php
				if ( empty( $thumb_posts ) ) {
					Template_Part::no_posts_found_text( $post_not_found );}
				Template_Part::preloader_front( $preloader_enable, $preloader_svg );
				?>
			</div>
		<div class="sp-smart-post-block-wrapper sp-smart-post-thumbnail-slider-two sp-smart-thumbnail-slider-two sp-smart-md-10px <?php echo esc_attr( $layout ); ?> sp-thumbnail-<?php echo esc_attr( $post_thumbnail_pos ); ?>">
			<!-- Main Swiper -->
			<div class="sp-smart-post-thumbnail-slide-two sp-cat-position-<?php echo esc_attr( $category_position ); ?>">
				<div class="swiper swiper-container sp-swiper-<?php echo esc_attr( $animation_effect ); ?> sp-smart-post-swiper <?php echo esc_attr( $layout ); ?> <?php echo esc_attr( $background_layout_class ); ?>" data-swiper-options='<?php echo esc_attr( $main_data ); ?>'>
					<div class="swiper-wrapper">
						<?php foreach ( $thumb_posts as $data ) : ?>
							<div class="swiper-slide sp-slide-item">
								<?php Template::template_one( $attributes, $data ); ?>
							</div>
						<?php endforeach; ?>
					</div>

					<?php if ( $show_nav_arrow ) : ?>
						<?php Template_part::swiper_nav_arrow( $arrow_on_hover, $arrow_style ); ?>
					<?php endif; ?>
				</div>
			</div>

			<!-- Thumbnails Swiper -->
			<?php
				$thumb_classes = 'sp-smart-post-thumbnail-two-thumb';
			if ( ! in_array( $layout, array( 'thumbnail-slider-two-layout-one', 'thumbnail-slider-two-layout-five' ), true ) ) {
				$thumb_classes .= ' thumbnails-' . $post_thumbnail_pos;
			}
			if ( 'thumbnail-slider-two-layout-two' === $layout ) {
				$thumb_classes .= ' progress-bar-' . $progress_pos;
			}
			if ( 'thumbnail-slider-two-layout-one' === $layout ) {
				$thumb_classes .= ' thumbs-align-' . $thumb_alignment;
			}
			?>
			<div class="<?php echo esc_attr( $thumb_classes ); ?>">
				<div class="swiper swiper-container sp-smart-post-swiper2 <?php echo esc_attr( $background_layout_class ); ?>" data-swiper-options='<?php echo esc_attr( $thumbs_data ); ?>'>
					<div class="swiper-wrapper">
						<?php foreach ( $thumb_posts as $index => $data ) : ?>
							<div class="swiper-slide sp-slide-item-thumb">
								<?php
								$attributes['thumbIndex'] = ( 'thumbnail-slider-two-layout-one' === $layout ) ? $index + 1 : '';
									Template::thumb_template( $attributes, $data, $hide_part );
								?>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			</div>
		</div>
		<?php
		// Moved modal rendering to footer.
				add_action(
					'wp_footer',
					function () use ( $attributes, $thumb_posts ) {
						Template_Part::modal_lightbox( $attributes, $thumb_posts );
					},
					20
				);
		?>
		</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
