<?php
/**
 * The template for displaying comments.
 *
 * @package Eronex
 * @since 0.0.1
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'eronex_comment_callback' ) ) {
	/**
	 * Custom comment callback with reply link inside .comment-author.
	 *
	 * @since 0.0.1
	 *
	 * @param WP_Comment $comment Comment object.
	 * @param array      $args    wp_list_comments() args.
	 * @param int        $depth   Comment depth.
	 * @return void
	 */
	function eronex_comment_callback( $comment, $args, $depth ) {
		$GLOBALS['comment'] = $comment;

		if ( ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) && ! empty( $args['short_ping'] ) ) {
			?>
			<li id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
				<div class="comment-body">
					<?php esc_html_e( 'Pingback:', 'eronex' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( esc_html__( 'Edit', 'eronex' ), '<span class="edit-link">', '</span>' ); ?>
				</div>
			<?php
			return;
		}

		$tag       = ( 'div' === $args['style'] ) ? 'div' : 'li';
		$add_below = ( 'div' === $args['style'] ) ? 'comment' : 'div-comment';

		$commenter          = wp_get_current_commenter();
		$show_pending_links = ! empty( $commenter['comment_author'] );

		if ( ! empty( $commenter['comment_author_email'] ) ) {
			$moderation_note = __( 'Your comment is awaiting moderation.', 'eronex' );
		} else {
			$moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.', 'eronex' );
		}
		?>
		<<?php echo esc_html( $tag ); ?> <?php comment_class( '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
		<?php if ( 'div' !== $args['style'] ) : ?>
		<div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
		<?php endif; ?>
		<div class="comment-author vcard">
			<?php
			if ( 0 !== $args['avatar_size'] ) {
				echo get_avatar( $comment, $args['avatar_size'] );
			}

			$comment_author = get_comment_author_link( $comment );

			if ( '0' === $comment->comment_approved && ! $show_pending_links ) {
				$comment_author = esc_html( get_comment_author( $comment ) );
			}

			/* translators: %s: Comment author link. */
			printf( '<h6 class="comment-author-name">%s</h6>', wp_kses_post( $comment_author ) );

			if ( '1' === $comment->comment_approved || $show_pending_links ) {
				comment_reply_link(
					array_merge(
						$args,
						array(
							'add_below'  => $add_below,
							'depth'      => $depth,
							'max_depth'  => $args['max_depth'],
							'before'     => ' ',
							'after'      => '',
							'class'      => 'comment-reply-link eronex-btn',
							'reply_text' => __( 'Reply', 'eronex' ),
						)
					)
				);
			}
			?>
		</div>
		<?php if ( '0' === $comment->comment_approved ) : ?>
		<em class="comment-awaiting-moderation"><?php echo esc_html( $moderation_note ); ?></em>
		<br />
		<?php endif; ?>

		<div class="comment-meta commentmetadata">
			<?php
			printf(
				'<a href="%s">%s</a>',
				esc_url( get_comment_link( $comment, $args ) ),
				sprintf(
					/* translators: 1: Comment date, 2: Comment time. */
					esc_html__( '%1$s at %2$s', 'eronex' ),
					get_comment_date( '', $comment ),
					get_comment_time()
				)
			);

			edit_comment_link( esc_html__( '(Edit)', 'eronex' ), ' &nbsp;&nbsp;', '' );
			?>
		</div>

		<?php
		comment_text(
			$comment,
			array_merge(
				$args,
				array(
					'add_below' => $add_below,
					'depth'     => $depth,
					'max_depth' => $args['max_depth'],
				)
			)
		);
		?>

		<?php if ( 'div' !== $args['style'] ) : ?>
		</div>
		<?php endif; ?>
		<?php
	}
}

if ( ! post_type_supports( get_post_type(), 'comments' ) ) {
	return;
}

if ( ! have_comments() && ! comments_open() ) {
	return;
}

if ( post_password_required() ) {
	return;
}

// Comment Reply Script.
if ( comments_open() && get_option( 'thread_comments' ) ) {
	wp_enqueue_script( 'comment-reply' );
}
?>
<div id="eronex-comments" class="eronex-comments-wrapper">

	<?php if ( have_comments() ) : ?>
		<h2 class="title-comments">
			<?php
			$comments_number = get_comments_number();
			if ( '1' === $comments_number ) {
				printf( esc_html_x( '1 Comment', 'comments title', 'eronex' ) );
			} else {
				printf(
					esc_html(
					/* translators: %s: Number of comments. */
						_nx(
							'%s Comment',
							'%s Comments',
							$comments_number,
							'comments title',
							'eronex'
						)
					),
					esc_html( number_format_i18n( $comments_number ) )
				);
			}
			?>
		</h2>

		<?php the_comments_navigation(); ?>

		<ol class="comment-list">
			<?php
			wp_list_comments(
				array(
					'style'       => 'ol',
					'short_ping'  => true,
					'avatar_size' => 42,
					'callback'    => 'eronex_comment_callback',
				)
			);
			?>
		</ol>

		<?php the_comments_navigation(); ?>

	<?php endif; ?>

	<?php
	add_filter(
		'comment_form_fields',
		$eronex_comment_form_fields = function ( $fields ) {
			$author  = $fields['author'] ?? '';
			$email   = $fields['email'] ?? '';
			$url     = $fields['url'] ?? '';
			$comment = $fields['comment'];
			$cookies = $fields['cookies'] ?? '';

			unset( $fields['author'], $fields['email'], $fields['url'], $fields['comment'], $fields['cookies'] );

			$reordered = array();

			if ( $author || $email || $url ) {
				$reordered['eronex_comment_inputs'] = '<div class="eronex-comment-form-inputs">' . $author . $email . $url . '</div>';
			}

			$reordered = array_merge( $reordered, $fields );

			$reordered['comment'] = $comment;

			if ( $cookies ) {
				$reordered['cookies'] = $cookies;
			}

			return $reordered;
		}
	);

	comment_form(
		array(
			'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
			'title_reply_after'  => '</h2>',
			'class_submit'       => 'eronex-btn',
		)
	);

	remove_filter( 'comment_form_fields', $eronex_comment_form_fields );
	?>

</div>
