<?php
/**
 * The template file for displaying the comments and comment form for the theme.
 */

/*
 * If the current post is protected by a password and
 * the visitor has not yet entered the password we will
 * return early without loading the comments.
*/
if ( post_password_required() ) {
	return;
}

if ( $comments ) {
	?>

	<div id="comments" class="comments">

		<?php
		$comments_number = absint( get_comments_number() );
		?>

		<div class="comments-header uk-width-1-1">

			<h2 class="comments-title">
    			<?php

                if ( ! have_comments() ) {
    				esc_html_e( 'Leave a comment', 'eisai' );
    			} else {
                    if ( comments_open() ) {
                        esc_html_e( 'Join the conversation', 'eisai' );
                    } else {
                        esc_html_e( 'Discussion', 'eisai' );
                    }
    			}

    			?>
			</h2><!-- .comments-title -->

		</div><!-- .comments-header -->

		<div class="comments-inner uk-width-1-1">

			<?php
			wp_list_comments(
				array(
					'walker'      => new Eisai_Walker_Comment(),
					'avatar_size' => 80,
					'style'       => 'div',
				)
			);

			$comment_pagination = paginate_comments_links(
				array(
					'echo'      => false,
					'end_size'  => 0,
					'mid_size'  => 0,
					'next_text' => __( 'Newer Comments', 'eisai' ) . ' <span aria-hidden="true">&rarr;</span>',
					'prev_text' => '<span aria-hidden="true">&larr;</span> ' . __( 'Older Comments', 'eisai' ),
				)
			);

			if ( $comment_pagination ) {
				$pagination_classes = '';

				// If we're only showing the "Next" link, add a class indicating so.
				if ( false === strpos( $comment_pagination, 'prev page-numbers' ) ) {
					$pagination_classes = ' only-next';
				}
				?>

				<nav class="comments-pagination pagination<?php echo $pagination_classes; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?>" aria-label="<?php esc_attr_e( 'Comments', 'eisai' ); ?>">
					<?php echo wp_kses_post( $comment_pagination ); ?>
				</nav>

				<?php
			}
			?>

		</div><!-- .comments-inner -->

	</div><!-- comments -->

	<?php
}

if ( comments_open() || pings_open() ) {

	comment_form(
		array(
			'class_form'         => 'uk-width-1-1',
			'title_reply_before' => '<h2 id="reply-title" class="comments-title">',
			'title_reply_after'  => '</h2>',
            'class_submit'       => 'uk-button uk-button-secondary',
		)
	);

} elseif ( is_single() ) {
	?>

	<div id="respond" class="comment-respond">

		<p class="comments-closed uk-h5"><?php esc_html_e( 'Comments are closed.', 'eisai' ); ?></p>

	</div><!-- #respond -->

	<?php
}
