<?php

/**
 * The template for displaying reviews/comments
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/global/reviews.php.
 *
 * HOWEVER, on occasion MovieEngine will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @note Please do not delete our CSS classes when you customize, as it may break functionality.
 * @package Movie_Engine
 * @subpackage Movie_Engine/templates
 * @version 1.0.0
 * @since 1.0.0
 */

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

global $wp_rewrite;

if (post_password_required()) {
    return;
}

if (is_singular() && comments_open() && get_option('thread_comments')) {
    wp_enqueue_script('comment-reply');
}
$movie_engine_commenter = wp_get_current_commenter();
?>
<div id="comments" class="movie-engine-reviews-area">
    <?php
    $movie_engine_total_comments = get_comments_number();

    if (!isset($comments)) {
        $movie_engine_reviews = get_comments(array(
            'post_id' => get_the_ID(),
            'status'  => 'approve',
            'number'  => 5,
            'include_unapproved' => is_user_logged_in() ? array(get_current_user_id()) : array(),
        ));
    } else {
        $movie_engine_reviews = array_slice($comments, 0, 5);
    }

    if ($movie_engine_total_comments > 0 || !empty($movie_engine_reviews)) : ?>
        <div class="movie-engine-reviews-header">
            <h3 class="movie-engine-reviews-title">
                <?php
                if (1 === (int)$movie_engine_total_comments) {
                    echo esc_html_x('1 Review', 'comments title', 'cine-movie-engine');
                } else {
                    printf(
                        /* translators: %1$s: number of reviews formatted */
                        esc_html(_nx('%1$s Review', '%1$s Reviews', $movie_engine_total_comments, 'comments title', 'cine-movie-engine')),
                        esc_html(number_format_i18n($movie_engine_total_comments))
                    );
                }
                ?>
            </h3>

            <?php if ($movie_engine_total_comments > 0) :
                $movie_engine_reviews_link = $wp_rewrite->using_permalinks() ? trailingslashit(get_permalink()) . 'reviews/' : add_query_arg('reviews', '1', get_permalink());
            ?>
                <a href="<?php echo esc_url($movie_engine_reviews_link); ?>" class="movie-engine-btn me-btn-outline">
                    <?php esc_html_e('View All Reviews', 'cine-movie-engine'); ?>
                </a>
            <?php endif; ?>
        </div>

        <ol class="movie-engine-review-list">
            <?php
            wp_list_comments(array(
                'style'       => 'ol',
                'short_ping'  => true,
                'avatar_size' => 50,
                'callback'    => ['MovieEngine\Frontend\TemplateHooks', 'comment_callback']
            ), $movie_engine_reviews);
            ?>
        </ol>

        <?php
        if (!comments_open()) :
        ?>
            <p class="no-comments"><?php esc_html_e('Reviews are closed.', 'cine-movie-engine'); ?></p>
    <?php endif;

    endif;
    ?>

    <div class="movie-engine-review-form-wrapper">
        <?php
        $movie_engine_require_name_email = get_option('require_name_email');
        $movie_engine_aria_req = ($movie_engine_require_name_email ? " aria-required='true'" : '');
        $movie_engine_commenter = wp_get_current_commenter();

        comment_form(array(
            'title_reply'       => esc_html__('Leave a Review', 'cine-movie-engine'),
            'title_reply_to'    => sprintf(
                /* translators: %s: name of the person being replied to */
                esc_html__('Leave a Reply to %s', 'cine-movie-engine'),
                '%s'
            ),
            'cancel_reply_link' => esc_html__('Cancel', 'cine-movie-engine'),
            'label_submit'      => esc_html__('Submit Review', 'cine-movie-engine'),
            'class_submit'      => 'movie-engine-btn movie-engine-btn-primary',
            'submit_button'     => '<button name="%1$s" type="submit" id="%2$s" class="%3$s">%4$s</button>',
            'submit_field'      => '<div class="form-submit me-form-submit">%1$s %2$s</div>',
            'comment_field'     => '<div class="comment-form-comment me-form-group"><textarea id="comment" name="comment" cols="45" rows="5" aria-required="true" required="required" placeholder="' . esc_html__('Write your review here...', 'cine-movie-engine') . '"></textarea></div>',
            'must_log_in' => wp_kses_post(
                '<p class="must-log-in">' . sprintf(
                    /* translators: %s: login URL */
                    esc_html__('You must be <a href="%s">logged in</a> to post a review.', 'cine-movie-engine'),
                    esc_url(wp_login_url(get_permalink()))
                ) . '</p>'
            ),

            'logged_in_as'      => '<p class="logged-in-as">' . sprintf(
                /* translators: 1: profile URL, 2: user display name, 3: logout URL */
                wp_kses(__('Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>', 'cine-movie-engine'), array('a' => array('href' => array(), 'title' => array()))),
                esc_url(admin_url('profile.php')),
                esc_html(wp_get_current_user()->display_name),
                esc_url(wp_logout_url(get_permalink()))
            ) . '</p>',
            'class_form'        => 'movie-engine-comment-form',
            'fields'            => array(
                'author' => '<div class="comment-form-author me-form-group"><label for="author">' . esc_html__('Name', 'cine-movie-engine') . ($movie_engine_require_name_email ? ' <span class="required">*</span>' : '') . '</label><input id="author" name="author" type="text" value="' . esc_attr($movie_engine_commenter['comment_author']) . '" size="30" ' . $movie_engine_aria_req . ' placeholder="' . esc_html__('Enter your name', 'cine-movie-engine') . '" /></div>',
                'email'  => '<div class="comment-form-email me-form-group"><label for="email">' . esc_html__('Email', 'cine-movie-engine') . ($movie_engine_require_name_email ? ' <span class="required">*</span>' : '') . '</label><input id="email" name="email" type="email" value="' . esc_attr($movie_engine_commenter['comment_author_email']) . '" size="30" ' . $movie_engine_aria_req . ' placeholder="' . esc_html__('Enter your email', 'cine-movie-engine') . '" /></div>',
                'url'    => '<div class="comment-form-url me-form-group"><label for="url">' . esc_html__('Website', 'cine-movie-engine') . '</label><input id="url" name="url" type="url" value="' . esc_attr($movie_engine_commenter['comment_author_url']) . '" size="30" placeholder="' . esc_html__('Your website (optional)', 'cine-movie-engine') . '" /></div>',
            ),
        ));
        ?>
    </div>

</div>