<?php

function aurora_lite_post_comments() {

    if ( ! comments_open() && ! get_comments_number() ) {
        return;
    }
    ?>

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

        <?php if ( have_comments() ) : ?>
            <h2 class="comments-title">
                <?php
                printf(
                    _n(                        
                    /* translators: %1$s = comment count, %2$s = post title */
                        '%1$s comment on "%2$s"',
                        '%1$s comments on "%2$s"',
                        get_comments_number(),
                        'aurora-lite'
                    ),
                    number_format_i18n( get_comments_number() ),
                    '<span>' . esc_html( get_the_title() ) . '</span>'
                );
                ?>
            </h2>

            <ol class="comment-list">
                <?php
                wp_list_comments([
                    'style'       => 'ol',
                    'short_ping'  => true,
                    'avatar_size'=> 50,
                    'callback'    => 'aurora_lite_comment_callback',
                ]);
                ?>
            </ol>

            <?php the_comments_navigation(); ?>

        <?php endif; ?>

        <?php
        if ( ! comments_open() && get_comments_number() ) :
            ?>
            <p class="no-comments">
                <?php esc_html_e( 'Comments are closed.', 'aurora-lite' ); ?>
            </p>
            <?php
        endif;
        ?>

        <?php aurora_lite_comment_form(); ?>

    </div>

    <?php
}

function aurora_lite_comment_callback( $comment, $args, $depth ) {
    if ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) {
        return;
    }
    
    $is_unapproved = '0' === $comment->comment_approved;
    $is_comment_author = false;
    $commenter = wp_get_current_commenter();
    
    if ( $is_unapproved && ! empty( $commenter['comment_author_email'] ) && ! empty( $comment->comment_author_email ) ) {
        $is_comment_author = ( strtolower( trim( $commenter['comment_author_email'] ) ) === strtolower( trim( $comment->comment_author_email ) ) );
    }
    
    if ( $is_unapproved && ! $is_comment_author && ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
        return;
    }
    ?>
    <li <?php comment_class( 'aurora-lite-comment' ); ?> id="comment-<?php comment_ID(); ?>">
        <article class="comment-body">

            <div class="comment-meta comment-header">
                <?php 
                echo get_avatar( $comment, 150 ); 
                ?>
                <div class="author-details">
                    <span class="comment-author">
                        <?php comment_author_link(); ?>
                    </span>

                    <time datetime="<?php comment_time( 'c' ); ?>">
                        <?php comment_date(aurora_lite_get_date_format()); ?>
                    </time>

                    <?php if ( $is_unapproved ) : ?>
                        <?php if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) : ?>
                            <em class="comment-awaiting-moderation admin-notice">
                                <?php esc_html_e( '[Pending Moderation]', 'aurora-lite' ); ?>
                            </em>
                        <?php elseif ( $is_comment_author ) : ?>
                            <em class="comment-awaiting-moderation">
                                <?php esc_html_e( 'Your comment is awaiting moderation.', 'aurora-lite' ); ?>
                            </em>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>

            <div class="comment-content">
                <?php comment_text(); ?>
            </div>

            <div class="comments-actions comment-footer">
                <?php
                    comment_reply_link(array_merge(
                        $args,
                        [
                            'depth'     => $depth,
                            'max_depth' => $args['max_depth'],
                            'before'    => '',
                            'after'     => '',
                        ]
                    ));

                    edit_comment_link(
                        esc_html__( 'Edit', 'aurora-lite' ),
                        ' | ',
                        ''
                    );
                ?>
            </div>

        </article>
    </li>
    <?php
}

function aurora_lite_comment_form() {

    $commenter = wp_get_current_commenter();

    $req = get_option('require_name_email');

    $required_attr = $req ? 'required aria-required="true"' : '';
    $required_mark = $req ? ' <span class="required">*</span>' : '';

    $fields = [

        'author' =>
            '<p class="comment-form-author">
                <label for="author">' . __('Name', 'aurora-lite') . $required_mark . '</label>
                <input id="author" name="author" type="text"
                    placeholder="Name"
                    value="' . esc_attr($commenter['comment_author']) . '"
                    ' . $required_attr . ' />
            </p>',

        'email' =>
            '<p class="comment-form-email">
                <label for="email">' . __('Email', 'aurora-lite') . $required_mark . '</label>
                <input id="email" name="email" type="email"
                    placeholder="Email"
                    value="' . esc_attr($commenter['comment_author_email']) . '"
                    ' . $required_attr . ' />
            </p>',

        'url' =>
            '<p class="comment-form-url">
                <label for="url">' . __('Website', 'aurora-lite') . '</label>
                <input id="url" name="url" type="url"
                    placeholder="Website"
                    value="' . esc_attr($commenter['comment_author_url']) . '" />
            </p>',
    ];

    comment_form([

        'fields' => $fields,

        'comment_notes_before' => '<p class="comment-notes">'
            . esc_html__('Your email address will not be published.', 'aurora-lite')
            . ( $req ? ' <span class="req-note">' . esc_html__('Required fields are marked *', 'aurora-lite') . '</span>' : '' )
            . '</p>',

        'cookies' => '<p class="comment-form-cookies-consent">'
            . '<input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes" '
            . checked( empty( $commenter['comment_author_email'] ), false, false )
            . ' />'
            . '<label for="wp-comment-cookies-consent">'
            . esc_html__('Save my name, email, and website in this browser for the next time I comment.', 'aurora-lite')
            . '</label>'
            . '</p>',

        'comment_field' => '<p class="comment-form-comment">'
            . '<label for="comment">' . esc_html__('Comment', 'aurora-lite') . ' <span class="required">*</span></label>'
            . '<textarea id="comment" name="comment" placeholder="' . esc_attr__('Write your comment…', 'aurora-lite') . '" rows="5" required></textarea>'
            . '</p>',

        'class_form'   => 'aurora-lite-comment-form',
        'title_reply'  => esc_html__('Leave a reply', 'aurora-lite'),
        'label_submit' => esc_html__('Post Comment', 'aurora-lite'),

    ]);

}
