<?php
/**
 * The template for displaying comments
 *
 * @package Author_Portfolio
 */

if (post_password_required()) {
	return;
}

// 1. Settings Check
$awt_settings = get_option('awt_global_settings', []);
$single_slug = $awt_settings['awt_blog_single_template_id'] ?? '';
$template_id = function_exists('rswpthemes_awt_get_block_id_by_slug') ? rswpthemes_awt_get_block_id_by_slug($single_slug) : false;
$has_awt_template = !empty($template_id);

// 2. Default Arguments
$list_args = array(
	'style' => 'ol',
	'short_ping' => true,
	'avatar_size' => 50,
);

$form_args = array();
$leave_reply_label = __('Leave a Reply', 'author-portfolio');

// 3. Conditional Logic for AWT
if ($has_awt_template) {

	// List Args
	if (function_exists('rswpthemes_awt_custom_comment_list')) {
		$list_args['callback'] = 'rswpthemes_awt_custom_comment_list';
		$list_args['style'] = 'div';
		$list_args['avatar_size'] = 64;
	}

	// Form Args
	if (function_exists('rswpthemes_awt_get_comment_form_args')) {
		$form_args = rswpthemes_awt_get_comment_form_args();
	}

	// Get Label from Global (passed from render.php)
	if (isset($GLOBALS['awt_label_leave_reply'])) {
		$leave_reply_label = $GLOBALS['awt_label_leave_reply'];
	}
}
?>

<div id="comments" class="comments-area <?php echo $has_awt_template ? 'awt-styled-comments' : ''; ?>">

	<?php if (have_comments()): ?>

		<?php if (!$has_awt_template): ?>
			<h2 class="comments-title">
				<?php
				$comments_number = get_comments_number();
				if ('1' === $comments_number) {
					printf(_x('One Reply to &ldquo;%s&rdquo;', 'comments title', 'author-portfolio'), get_the_title());
				} else {
					printf(
						_nx(
							'%1$s Reply to &ldquo;%2$s&rdquo;',
							'%1$s Replies to &ldquo;%2$s&rdquo;',
							$comments_number,
							'comments title',
							'author-portfolio'
						),
						number_format_i18n($comments_number),
						get_the_title()
					);
				}
				?>
			</h2>
		<?php endif; ?>

		<div class="<?php echo $has_awt_template ? 'space-y-6' : 'comment-list-wrapper'; ?>">
			<?php wp_list_comments($list_args); ?>
		</div>

		<?php if (get_comment_pages_count() > 1 && get_option('page_comments')): ?>
			<nav class="navigation comment-navigation" role="navigation">
				<div class="nav-previous">
					<?php previous_comments_link(esc_html__('&larr; Older Comments', 'author-portfolio')); ?>
				</div>
				<div class="nav-next"><?php next_comments_link(esc_html__('Newer Comments &rarr;', 'author-portfolio')); ?>
				</div>
			</nav>
		<?php endif; ?>

	<?php endif; // have_comments() ?>

	<div class="mt-12">
		<?php if ($has_awt_template): ?>
			<h3 class="text-3xl font-black mb-8 text-navy"><?php echo esc_html($leave_reply_label); ?></h3>
		<?php endif; ?>

		<?php comment_form($form_args); ?>
	</div>

</div>