<?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_Comment_Count extends Block_Base {

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

	/**
	 * 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() && ! is_singular() ) {
			return $content;
		}

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

		$unique_id               = $attributes['uniqueId'] ?? '';
		$align                   = $attributes['align'] ?? '';
		$additional_id           = $attributes['additionalId'] ?? '';
		$additional_id           = $attributes['additionalId'] ?? '';
		$additional_class        = $attributes['additionalClass'] ?? '';
		$icon_style              = $attributes['iconStyle'] ?? '';
		$label_position          = $attributes['labelPosition'] ?? '';
		$show_label              = $attributes['showCommentLabel'] ?? '';
		$comment_label           = $attributes['commentTextLabel'] ?? '';
		$comment_label           = $show_label ? $comment_label : '';
		$label_global_typography = $attributes['labelGlobalTypography'] ?? '';
		$label_global_class      = $label_global_typography['class'] ?? '';
		$unique_id               = $attributes['uniqueId'] ?? '';
		$align_width             = '' !== $align ? ' align' . $align : '';

		$post_id = get_the_ID();

		$comment_count = $post_id ? get_comments_number( $post_id ) : '';

		$update_comment_text     = $comment_label;
		$is_default_comment_text = in_array( $comment_label, array( 'Comments', 'comments', 'Comment', 'comment' ), true );
		if ( $is_default_comment_text ) {
			if ( 2 > intval( $comment_count ) ) {
				$update_comment_text = preg_replace( '/(s)$/i', '', $comment_label );
			} elseif ( 1 < intval( $comment_count ) ) {
				$update_comment_text = preg_replace( '/(s)$/i', '', $comment_label ) . 's';
			}
		}

		$comment_content = 'afterCount' === $label_position ? $comment_count . ' ' . $update_comment_text : $update_comment_text . ' ' . $comment_count;

		$icon = Helper::get_icon_list( $icon_style );

		ob_start();
		?>
		<div id="<?php echo esc_attr( $additional_id ); ?>" class="sp-smart-post-wrapper <?php echo esc_attr( $additional_class . $align_width ); ?>">
			<div id="<?php echo esc_attr( $unique_id ); ?>" class="sp-smart-comment-count <?php echo esc_attr( $label_global_class ); ?>">
				<span class="sp-smart-comment-count-icon">
					<?php if ( ! empty( $icon ) ) { ?>
						<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">
							<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>
						</svg>
					<?php } ?>
				</span>
				<span><?php echo esc_html( $comment_content ); ?></span>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
