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



	/**
	 * Block properties
	 */
	protected function set_block_properties() {
		$this->block_name = 'post-author-meta';
		$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 markup.
	 */
	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;">Author Meta will appear here</p>';
		}

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

		$unique_id      = $attributes['uniqueId'] ?? '';
		$additional_cls = $attributes['advancedAdditionalClass'] ?? '';
		$additional_id  = $attributes['advancedAdditionalID'] ?? '';
		$post_id        = $post->ID;

		$author_style              = $attributes['authorStyle'] ?? 'ShowNameGravatar';
		$show_prefix               = $attributes['showAuthorPrefix'] ?? false;
		$prefix_text               = $attributes['prefixLabelText'] ?? '';
		$meta_icon                 = $attributes['metaUserIcon'] ?? '';
		$content_global_typography = $attributes['globalContentTypography'] ?? '';
		$content_global_class      = $content_global_typography['class'] ?? '';
		$gravatar_size             = isset( $attributes['gravatarSize']['value'] ) ? intval( $attributes['gravatarSize']['value'] ) : 40;
		$icon                      = Helper::get_icon_list( $meta_icon );

		// Get post author.
		$author_id   = get_post_field( 'post_author', $post_id );
		$author_name = get_the_author_meta( 'display_name', $author_id );
		$avatar_url  = get_avatar_url( $author_id, array( 'size' => $gravatar_size ) );

		ob_start();
		?>
		<div class="sp-smart-post-author-meta-block" id="<?php echo esc_attr( $additional_id ); ?>">
			<div class="smart-post-author-block-wrapper sp-smart-post-wrapper <?php echo esc_attr( $unique_id . ' ' . $additional_cls ); ?>" id="<?php echo esc_attr( $unique_id ); ?>">

				<div class="sp-smart-post-author-meta-wrapper <?php echo esc_attr( $content_global_class ); ?> ">

					<?php if ( 'ShowNameIcon' === $author_style ) : ?>
						<span class="sp-smart-post-author-meta-icon">
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">
								<?php
								if ( is_array( $icon['path'] ) && ! empty( $icon['path'] ) ) {
									foreach ( $icon['path'] as $path ) {
										?>
										<path d="<?php echo esc_attr( $path ); ?>" fill="currentColor"></path>
										<?php
									}
								} else {
									?>
									<path d="<?php echo esc_attr( $icon['path'] ); ?>" fill="currentColor"></path>
								<?php } ?>
							</svg>
						</span>
					<?php endif; ?>

					<?php if ( 'ShowNameGravatar' === $author_style || 'showGravatar' === $author_style ) : ?>
						<span class="sp-smart-post-author-meta-icon">
							<img src="<?php echo esc_url( $avatar_url ); ?>" alt="<?php echo esc_attr( $author_name ); ?>" class="sp-smart-post-meta-icon" />
						</span>
					<?php endif; ?>

					<?php if ( $show_prefix && 'showGravatar' !== $author_style ) : ?>
						<span class="sp-smart-post-author-meta-prefix">
							<?php echo esc_html( $prefix_text ); ?>
						</span>
					<?php endif; ?>

					<?php if ( 'showGravatar' !== $author_style ) : ?>
						<span class="sp-smart-post-author-meta-author-name">
							<?php echo esc_html( $author_name ); ?>
						</span>
					<?php endif; ?>

				</div>

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