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

/**
 * Pagination block for Smart Post Show.
 *
 * Handles the pagination logic for Smart Post Show blocks.
 */
class Post_Featured_Image extends Block_Base {

	/**
	 * Set_block_properties
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name = 'post-featured-image';
		$this->styles     = array( 'sp_smart_post_blocks_google_fonts' );
	}

	/**
	 * Renders the content area for the featured image block.
	 *
	 * @param array $attributes The block attributes.
	 * @return string
	 */
	private static function content_area( $attributes, $img_url ) {
		$show_caption = $attributes['enableCaption'] ?? false;
		$img_id       = attachment_url_to_postid( $img_url );

		$image_id = get_post_thumbnail_id();

		if ( $img_id && 0 !== $img_id ) {
			$image_id = $img_id;
		}
		$image_caption = wp_get_attachment_caption( $image_id );

		if ( ! $image_caption ) {
			return '';
		}

		ob_start();
		?>
		<div class="sp-smart-post-featured-content">
			<?php if ( $show_caption ) { ?>
			<div class="sp-smart-post-featured-caption">
				<span><?php echo esc_html( $image_caption ); ?></span>
			</div>
			<?php } ?>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Content area function
	 *
	 * @return string
	 */
	private static function image_gallery( $attributes, $gallery, $featured_image_url, $show_srcset, $content ) {
		$array_style    = $attributes['imageGalleryNavArrowStyle'] ?? '';
		$image_alt_text = $attributes['imageAltText'] ?? '';
		$lazy_loading   = $attributes['lazyLoading'] ?? false;
		$layout_type    = $attributes['layoutType'] ?? '';
		$overlay_type   = $attributes['imageOverlayType'] ?? '';
		$image_size     = $attributes['imageSize'] ?? '';

		wp_enqueue_script( 'sp_video_and_gallery_slider' );
		$attachment_id = attachment_url_to_postid( $featured_image_url );
		$attachment_id = ( 0 !== $attachment_id ) ? $attachment_id : get_post_thumbnail_id();
		$alt_text      = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
		$image_srcset  = wp_get_attachment_image_srcset( $attachment_id );
		if ( $image_alt_text ) {
			$alt_text = $alt_text ? $alt_text : $image_alt_text;
		}
		$post_permalink = get_permalink( get_the_ID() );

		$post_href = ! is_single() ? ( 'href="' . esc_url( $post_permalink ) . '"' ) : '';

		$attachment_size   = ( 'custom' !== $image_size || ! empty( $image_size ) ) ? $image_size : 'full';
		$attachment        = wp_get_attachment_image_src( $attachment_id, $attachment_size );
		$attachment_width  = $attachment[1] ?? '';
		$attachment_height = $attachment[2] ?? '';

		ob_start();
		?>
		<div class="sp-slider-wrapper">
			<div class="sp-slider-track">
				<div class="sp-slide" >
					<a class="sp-slide-item-wrapper" target="_blank" <?php echo $post_href; //phpcs:ignore ?>>
						<img
							alt="<?php echo esc_attr( $alt_text ); ?>"
							src="<?php echo esc_url( $featured_image_url ); ?>"
							loading="<?php echo $lazy_loading ? 'lazy' : 'eager'; ?>"
							<?php if ( $show_srcset && $image_srcset ) : ?>
								srcset="<?php echo esc_attr( $image_srcset ); ?>"
							<?php endif; ?>
							width="<?php echo esc_attr( $attachment_width ); ?>"
							height="<?php echo esc_attr( $attachment_height ); ?>"
						/>
						<?php
						if ( 'background' === $layout_type ) {
							echo '<div class="sp-smart-image-featured-overlay overlay-' . esc_attr( $overlay_type ) . '"></div>';
							echo '<div class="sp-smart-post-featured-content-area-wrapper">';
							// @codingStandardsIgnoreLine
							echo $content;
							echo '</div>';
							// @codingStandardsIgnoreLine
							echo self::content_area( $attributes, $featured_image_url );
						}
						if ( 'normal' === $layout_type ) {
							// @codingStandardsIgnoreLine
							echo self::content_area( $attributes, $featured_image_url );
						}
						?>
					</a>
				</div>
				<?php
				foreach ( $gallery as $img_id => $img ) {
					$srcset         = $img['srcset'] ?? '';
					$gallery_attach = wp_get_attachment_image_src( $img_id, $attachment_size );
					?>
					<div class="sp-slide" >
						<a class="sp-slide-item-wrapper" target="_blank" href="<?php echo ! is_single() ? esc_url( $post_permalink ) : ''; ?>">
							<img
								alt="<?php echo esc_attr( $img['alt'] ); ?>"
								src="<?php echo esc_url( $img['url'] ); ?>"
								loading="<?php echo $lazy_loading ? 'lazy' : 'eager'; ?>"
								<?php if ( $srcset ) : ?>
									srcset="<?php echo esc_attr( $srcset ); ?>"
								<?php endif; ?>
								width="<?php echo esc_attr( $gallery_attach[1] ); ?>"
								height="<?php echo esc_attr( $gallery_attach[2] ); ?>"
							/>
							<?php
							if ( 'background' === $layout_type ) {
								echo '<div class="sp-smart-image-featured-overlay overlay-' . esc_attr( $overlay_type ) . '"></div>';
								echo '<div class="sp-smart-post-featured-content-area-wrapper">';
								// @codingStandardsIgnoreLine
								echo $content;
								echo '</div>';
								// @codingStandardsIgnoreLine
								echo self::content_area( $attributes, $img['url'] );
							}
							if ( 'normal' === $layout_type ) {
								// @codingStandardsIgnoreLine
								echo self::content_area( $attributes, $img['url'] );
							}
							?>
						</a>
					</div>
				<?php } ?>
			</div>
		</div>
		<div class="sp-smart-image-gallery-nav-arrow">
			<i class="sp-prev">
			<?php
			// @codingStandardsIgnoreLine
			echo Helper::get_arrow_icon( $array_style ); ?></i>
			<i class="sp-next">
			<?php
			// @codingStandardsIgnoreLine
			echo Helper::get_arrow_icon( $array_style ); ?></i>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Content area function
	 *
	 * @return string
	 */
	private static function video_part( $video_meta, $featured_image_url, $attributes, $content ) {

		$layout_type   = $attributes['layoutType'] ?? '';
		$overlay_type  = $attributes['imageOverlayType'] ?? '';
		$video_url     = $video_meta['url'] ?? '';
		$video_caption = $video_meta['caption'] ?? '';

		wp_enqueue_script( 'sp_video_and_gallery_slider' );

		ob_start();
		?>
		<div class="sp-smart-video-player" data-image_url="<?php echo esc_url( $featured_image_url ); ?>" data-video_url="<?php echo esc_url( $video_url ); ?>" data-video_caption="<?php echo esc_attr( $video_caption ); ?>">
			<?php
			if ( 'background' === $layout_type ) {
				echo '<div class="sp-smart-image-featured-overlay overlay-' . esc_attr( $overlay_type ) . '"></div>';
				echo '<div class="sp-smart-post-featured-content-area-wrapper">';
				// @codingStandardsIgnoreLine
				echo $content;
				echo '</div>';
				// @codingStandardsIgnoreLine
				echo self::content_area( $attributes, $featured_image_url );
			}
			if ( 'normal' === $layout_type ) {
				// @codingStandardsIgnoreLine
				echo self::content_area( $attributes, $featured_image_url );
			}
			?>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Block render function.
	 *
	 * @param  array  $attributes attributes.
	 * @param  string $content  Content to be rendered.
	 * @param  array  $blocks   Array of block list.
	 * @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;
		}

		$image_id           = get_post_thumbnail_id();
		$post_id            = get_the_ID();
		$image_gallery      = get_post_meta( $post_id, '_sp_gallery_ids', true );
		$video_meta         = get_post_meta( $post_id, '_sp_video_meta', true );
		$image_size         = $attributes['imageSize'] ?? '';
		$placeholder_img    = SP_PCP_URL . 'public/assets/img/placeholder.png';
		$featured_image_url = get_the_post_thumbnail_url( $post_id, $image_size )
		? get_the_post_thumbnail_url( $post_id, $image_size )
		: $placeholder_img;

		$unique_id               = $attributes['uniqueId'] ?? '';
		$align                   = $attributes['align'] ?? '';
		$additional_id           = $attributes['additionalId'] ?? '';
		$additional_id           = $attributes['additionalId'] ?? '';
		$additional_class        = $attributes['additionalClass'] ?? '';
		$unique_id               = $attributes['uniqueId'] ?? '';
		$layout_type             = $attributes['layoutType'] ?? '';
		$show_gallery            = $attributes['showImageGallery'] ?? '';
		$show_video              = $attributes['showFeaturedVideo'] ?? '';
		$show_srcset             = $attributes['imageSrcset'] ?? '';
		$image_alt_text          = $attributes['imageAltText'] ?? '';
		$overlay_type            = $attributes['imageOverlayType'] ?? '';
		$show_nav_arrow_on_hover = $attributes['showImageGalleryNavArrowHover'] ?? '';
		$image_hover_effect      = $attributes['imageHoverEffect'] ?? '';
		$video_url               = $video_meta['url'] ?? '';
		$lazy_loading            = $attributes['lazyLoading'] ?? false;

		$post_permalink = get_permalink( $post_id );
		$image_srcset   = $show_srcset ? 'srcset="' . esc_attr( wp_get_attachment_image_srcset( $image_id ) ) . '"' : '';

		$gallery = array();
		if ( ! empty( $image_gallery ) ) {
			foreach ( $image_gallery as $id ) {
				$url               = wp_get_attachment_image_src( $id, 'large' );
				$alt_text          = get_post_meta( $id, '_wp_attachment_image_alt', true );
				$image_srcset_list = $show_srcset ? wp_get_attachment_image_srcset( $id ) : '';
				if ( $url ) {
					$gallery[ $id ] = array(
						'url'    => esc_url( $url[0] ),
						'alt'    => $alt_text,
						'srcset' => $image_srcset_list,
					);
				}
			}
		}

		$alt_text = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
		if ( $alt_text ) {
			$image_alt_text = $alt_text;
		}

		$align_width = '' !== $align ? ' align' . $align : '';
		$post_href   = ! is_single() ? ( 'href="' . esc_url( $post_permalink ) . '"' ) : '';

		$attachment_size   = ( 'custom' !== $image_size || ! empty( $image_size ) ) ? $image_size : 'full';
		$attachment        = wp_get_attachment_image_src( $image_id, $attachment_size );
		$attachment_width  = $attachment[1] ?? '';
		$attachment_height = $attachment[2] ?? '';

		ob_start();
		?>
		<div id="<?php echo esc_attr( $additional_id ); ?>" class="<?php echo esc_attr( $additional_class . $align_width ); ?>">
			<div id="<?php echo esc_attr( $unique_id ); ?>" class="sp-smart-post-wrapper sp-smart-post-featured<?php echo 'background' === $layout_type ? ' sp-smart-post-featured-bg' : ''; ?>">
				<div class="sp-smart-post-featured-wrapper">
					<div class="sp-smart-post-featured-image sp-img-<?php echo esc_attr( $image_hover_effect ); ?> <?php echo $show_nav_arrow_on_hover ? ' sp-smart-post-featured-image-nav-on-hover' : ''; ?>">
						<?php
						if ( $show_video && '' !== $video_url ) {
							// @codingStandardsIgnoreLine
							echo self::video_part( $video_meta, $featured_image_url, $attributes, $content );
						} elseif ( $gallery && $show_gallery ) {
							// @codingStandardsIgnoreLine
							echo self::image_gallery( $attributes, $gallery, $featured_image_url, $show_srcset, $content );
						} else {
							?>
							<a target="_blank" <?php echo $post_href;// phpcs:ignore ?> >
								<img alt="<?php echo esc_attr( $image_alt_text ); ?>" src="<?php echo esc_url( $featured_image_url ); ?>" 
								<?php
									// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
									echo $image_srcset; // Escape done while defining the variable.
								?>
								loading="<?php echo $lazy_loading ? 'lazy' : 'eager'; ?>" width="<?php echo esc_attr( $attachment_width ); ?>" height="<?php echo esc_attr( $attachment_height ); ?>"
								/>
								<?php
								if ( 'background' === $layout_type ) {
									echo '<div class="sp-smart-image-featured-overlay overlay-' . esc_attr( $overlay_type ) . '"></div>';
									echo '<div class="sp-smart-post-featured-content-area-wrapper">';
									// @codingStandardsIgnoreLine
									echo $content;
									echo '</div>';
									// @codingStandardsIgnoreLine
									echo self::content_area( $attributes, $featured_image_url );
								}
								if ( 'normal' === $layout_type ) {
									// @codingStandardsIgnoreLine
									echo self::content_area( $attributes, $featured_image_url );
								}
								?>
							</a>
						<?php } ?>
						
					</div>
					
				</div>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
