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


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


	/**
	 * Calculate reading time
	 *
	 * @param string $post_content The post content.
	 */
	private function calculate_reading_time( $post_content ) {
		$content_text = wp_strip_all_tags( $post_content );
		$word_count   = str_word_count( $content_text );

		$words_per_minute = 200;
		$minutes          = ceil( $word_count / $words_per_minute );
		return $minutes;
	}

	/**
	 * 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;">⏱️ Reading time will appear here</p>';
		}

		if ( ! is_singular() ) {
			return '';
		}

		global $post;
		if ( ! $post || ! isset( $post->post_content ) ) {
			return '';
		}

		$unique_id                 = $attributes['uniqueId'] ?? '';
		$advanced_additional_class = $attributes['advancedAdditionalClass'] ?? '';
		$advanced_additional_id    = $attributes['advancedAdditionalID'] ?? '';
		$show_icon                 = $attributes['showIcon'] ?? true;
		$icon_source               = $attributes['iconSource'] ?? 'outline';
		$reading_label_show        = $attributes['readingTimeLabelShow'] ?? true;
		$label_text                = $attributes['labelText'] ?? 'Reads';
		$label_position            = $attributes['labelPosition'] ?? 'after';
		$content_global_typography = $attributes['globalContentTypography'] ?? '';
		$content_global_class      = $content_global_typography['class'] ?? '';

		$minutes = $this->calculate_reading_time( $post->post_content );

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

		$wrapper_class = implode( ' ', array_filter( $wrapper_classes ) );
		$icon          = Helper::get_icon_list( $icon_source );

		ob_start();
		?>
		<div id="<?php echo esc_attr( $advanced_additional_id ); ?>" class="sp-smart-post-reading-time-block <?php echo esc_attr( $advanced_additional_class ); ?>">
			<div id="<?php echo esc_attr( $unique_id ); ?>" class="<?php echo esc_attr( $wrapper_class ); ?> sp-smart-post-wrapper">
				<div class="sp-smart-post-reading-time-wrapper <?php echo esc_attr( $content_global_class ); ?> ">
					<?php
					if ( $show_icon ) :

						?>
						<span class="sp-smart-post-reading-time-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 ( $reading_label_show && 'before' === $label_position ) : ?>
						<span class="sp-smart-post-reading-time-label">
							<?php echo esc_html( $label_text ); ?>
						</span>
					<?php endif; ?>

					<span class="sp-smart-post-reading-time-value">
						<?php echo esc_html( $minutes ); ?> Min

					</span>

					<?php if ( $reading_label_show && 'after' === $label_position ) : ?>
						<span class="sp-smart-post-reading-time-label">
							<?php echo esc_html( $label_text ); ?>
						</span>
					<?php endif; ?>

				</div>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
