<?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 Reading Time Block.
 */
class Post_Like_Count extends Block_Base {



	/**
	 * Block properties
	 */
	protected function set_block_properties() {
		$this->block_name = 'post-like-count';
		$this->styles     = array(
			'sp_smart_post_blocks_css',
			'sp_smart_post_blocks_style',
			'sp_smart_post_blocks_google_fonts',
		);
	}


	/**
	 * Render block
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content Block content.
	 * @param array  $blocks Inner blocks.
	 * 
	 * @return string        Rendered HTML.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {

		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return '<p style="opacity:0.5; margin:0; padding:8px 0;">Like Count will appear here</p>';
		}

		if ( ! is_singular() ) {
			return '';
		}
		$post_id                   = get_the_ID();
		$unique_id                 = $attributes['uniqueId'] ?? '';
		$advanced_additional_class = $attributes['advancedAdditionalClass'] ?? '';
		$advanced_additional_id    = $attributes['advancedAdditionalID'] ?? '';
		$show_icon                 = $attributes['showIcon'] ?? true;
		$icon_source               = $attributes['iconSource'] ?? 'loveOutline';
		$label_show                = $attributes['labelShow'] ?? true;
		$label_text                = $attributes['labelText'] ?? 'Likes';
		$label_position            = $attributes['labelPosition'] ?? 'after';
		$content_global_typography = $attributes['globalContentTypography'] ?? '';
		$content_global_class      = $content_global_typography['class'] ?? '';
		$count                     = get_post_meta( $post_id, '_post_like_count', true );
		$count                     = ! empty( $count ) ? (int) $count : 0;

		$wrapper_classes = array(
			'smart-post-like-block-wrapper',
			'sp-smart-post-like-count-' . $label_position,
		);

		$like_options  = \SP_PCP_User_Like::get_pcp_likes_block_button( $post_id );
		$button_class  = $like_options['button_class'] ?? '';
		$button_nonce  = $like_options['nonce'] ?? '';
		$title         = $like_options['title'] ?? '';
		$wrapper_class = implode( ' ', array_filter( $wrapper_classes ) );
		$icon          = Helper::get_icon_list( $icon_source );

		$update_like_text     = $label_text;
		$is_default_like_text = in_array( $label_text, array( 'Likes', 'likes', 'Like', 'like' ), true );
		if ( $is_default_like_text ) {
			if ( 1 === $count ) {
				$update_like_text = preg_replace( '/(s)$/i', '', $label_text );
			} elseif ( 1 < $count ) {
				$update_like_text = preg_replace( '/(s)$/i', '', $label_text ) . 's';
			}
		}

		ob_start();
		?>
		<div id="<?php echo esc_attr( $advanced_additional_id ); ?>" class="sp-smart-post-like-count-block <?php echo esc_attr( $advanced_additional_class ); ?>">
			<div id="<?php echo esc_attr( $unique_id ); ?>" class="<?php echo esc_attr( $wrapper_class ); ?> pcpl-wrapper sp-smart-post-wrapper">
				<div class="sp-smart-post-like-count-wrapper pcpl-button <?php echo esc_attr( $content_global_class ); ?> <?php echo esc_attr( $button_class ); ?>" data-nonce="<?php echo esc_attr( $button_nonce ); ?>" title="<?php echo esc_attr( $title ); ?>" data-post-id="<?php echo esc_attr( $post_id ); ?>" data-single="true">
					<?php
					if ( $show_icon ) :

						?>
							<span class="sp-smart-post-like-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>
							<?php

					endif;
					?>

					<?php if ( $label_show && 'before' === $label_position ) : ?>
						<span class="sp-smart-post-like-count-label">
							<?php echo esc_html( $update_like_text ); ?>
						</span>
					<?php endif; ?>

					<span class="sp-smart-post-like-count-value">
						<?php echo esc_html( $count ); ?>

					</span>

					<?php if ( $label_show && 'after' === $label_position ) : ?>
						<span class="sp-smart-post-like-count-label">
							<?php echo esc_html( $update_like_text ); ?>
						</span>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
