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

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

	/**
	 * Get author comment count
	 *
	 * @param int|null $author_id Author ID.
	 * @return int
	 */
	public static function get_author_comment_count( $author_id = null ) {
		if ( ! $author_id ) {
			$author_id = get_the_author_meta( 'ID' );
		}

		return get_comments(
			array(
				'user_id' => $author_id,
				'status'  => 'approve',
				'count'   => true,
			)
		);
	}

	/**
	 * 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'] ?? '';
		$unique_id                   = $attributes['uniqueId'] ?? '';
		$layout                      = $attributes['layoutType'] ?? '';
		$date_format                 = $attributes['dateFormat'] ?? 'M j, Y';
		$arrow_style                 = $attributes['navIconStyle'] ?? '';
		$title_length                = $attributes['titleLength'] ?? '';
		$show_image                  = $attributes['showImage'] ?? '';
		$show_name                   = $attributes['showName'] ?? '';
		$show_bio                    = $attributes['showBio'] ?? '';
		$show_meta                   = $attributes['showMeta'] ?? '';
		$show_button                 = $attributes['showButton'] ?? '';
		$heading_tag                 = $attributes['headingTag'] ?? '';
		$sub_title_label             = $attributes['subTitleLabel'] ?? '';
		$button_label                = $attributes['buttonLabel'] ?? '';
		$title_type                  = $title_length['unit'] ?? 'words';
		$title_length_num            = $title_length['value'] ?? '7';
		$name_global_typography      = $attributes['nameGlobalTypography'] ?? '';
		$name_global_class           = $name_global_typography['class'] ?? '';
		$bio_global_typography       = $attributes['bioGlobalTypography'] ?? '';
		$bio_global_class            = $bio_global_typography['class'] ?? '';
		$meta_global_typography      = $attributes['metaGlobalTypography'] ?? '';
		$meta_global_class           = $meta_global_typography['class'] ?? '';
		$btn_global_typography       = $attributes['buttonGlobalTypography'] ?? '';
		$btn_global_class            = $btn_global_typography['class'] ?? '';
		$sub_title_global_typography = $attributes['subTitleGlobalTypography'] ?? '';
		$sub_title_global_class      = $sub_title_global_typography['class'] ?? '';
		$meta_post_enable            = $attributes['metaPostEnable'] ?? false;
		$meta_comment_enable         = $attributes['metaCommentEnable'] ?? false;

		$align_width = '' !== $align ? ' align' . $align : '';

		$author_id  = get_the_author_meta( 'ID' );
		$post_count = count_user_posts( $author_id );

		$comment_label           = 'Comments';
		$update_comment_text     = $comment_label;
		$is_default_comment_text = in_array( $comment_label, array( 'Comments', 'comments', 'Comment', 'comment' ), true );
		$comment_count           = self::get_author_comment_count( $author_id );
		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';
			}
		}

		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-author-box">
				<div class="sp-smart-post-author-box-container sp-smart-post-author-box-<?php echo esc_attr( $layout ); ?>">
					<div class="sp-smart-post-author-box-img">
						<?php
						if ( $show_image ) {
							echo get_avatar( get_the_author_meta( 'ID' ), 96 );
						}
						?>
						<?php if ( $show_name ) { ?>
							<<?php echo esc_html( $heading_tag ); ?> class="sp-smart-post-author-box-name <?php echo esc_attr( $name_global_class ); ?>">
								<span class="sp-smart-post-author-box-sub <?php echo esc_attr( $sub_title_global_class ); ?>">
									<?php echo esc_html( $sub_title_label ); ?>
								</span>

								<a href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a>
							</<?php echo esc_html( $heading_tag ); ?>>
						<?php } ?>
					</div>
					<div class="sp-smart-post-author-box-info">
						<?php if ( $show_bio ) { ?>
							<span class="sp-smart-post-author-box-bio <?php echo esc_attr( $bio_global_class ); ?>"><?php echo esc_html( get_the_author_meta( 'description' ) ); ?></span>
						<?php } ?>
						<?php if ( $show_meta && ( $meta_post_enable || $meta_comment_enable ) ) { ?>
							<div class="sp-smart-post-author-box-meta <?php echo esc_attr( $meta_global_class ); ?>">
								<?php if ( $meta_post_enable ) : ?>
								<span><?php echo esc_html( $post_count ); ?> Posts</span>
								<?php endif; ?>
								<?php if ( $meta_comment_enable ) : ?>
								<span><?php echo esc_html( $comment_count ); ?> <?php echo esc_html( $update_comment_text ); ?></span>
								<?php endif; ?>
							</div>
						<?php } ?>
						<?php if ( $show_button ) { ?>
							<div class="sp-smart-post-author-box-link">
								<a href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>"><?php echo esc_html( $button_label ); ?></a>						
							</div>
						<?php } ?>
					</div>
				</div>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
