<?php

if (!function_exists('tdf_string')) {
    return;
}

if (post_password_required()) {
    return;
}

$myhomeCommenter = wp_get_current_commenter();
$myhomeRequired = get_option('require_name_email');
$myhomeAriaReq = ($myhomeRequired ? " aria-required='true'" : '');
?>
<div class="myhome-comments">
    <?php

    if (get_comments_number() > 0) : ?>
        <div class="myhome-comments__list">
            <h4 class="myhome-comments__label">
                <?php
                $commentsNumber = get_comments_number();
                echo esc_html($commentsNumber . ' ' . tdf_string($commentsNumber === 1 ? 'comment' : 'comments'));
                ?>
            </h4>

            <?php wp_list_comments(
                ['style' => 'div',
                    'short_ping' => true,
                    'avatar_size' => 85,
                    'callback' => static function ($comment, $args, $depth) {

                        $mhComment = get_comment();
                        if ($mhComment instanceof WP_Comment && !empty($mhComment->user_id) && class_exists(\Tangibledesign\Framework\Core\App::class)) {
                            $mhUser = tdf_user_factory()->create((int)$mhComment->user_id);
                            if ($mhUser) {
                                $mhUserImage = $mhUser->getImageUrl('myhome_100_100');
                            } else {
                                $mhUserImage = false;
                            }
                        } else {
                            $mhUserImage = false;
                        }
                        ?>

                        <div <?php comment_class(empty($args['has_children']) ? 'myhome-comment' : 'myhome-comment myhome-comment--parent'); ?>
                                id="myhome-comment-<?php comment_ID(); ?>">
                            <div class="myhome-comment__inner">
                                <div class="myhome-comment__top">
                                    <div class="myhome-comment__left">
                                        <div class="myhome-comment__user">
                                            <div
                                                <?php if (!empty($mhUserImage)) : ?>
                                                    class="myhome-comment__avatar"
                                                <?php else : ?>
                                                    class="myhome-comment__avatar myhome-comment__avatar--no-image"
                                                <?php endif; ?>
                                            >
                                                <?php if ($mhUserImage) : ?>
                                                    <img
                                                            class="lazyload"
                                                            src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAQAAAA3fa6RAAAADklEQVR42mNkAANGCAUAACMAA2w/AMgAAAAASUVORK5CYII="
                                                            data-src="<?php echo esc_url($mhUserImage); ?>"
                                                            alt="<?php echo esc_attr(get_comment_author()); ?>"
                                                    >
                                                <?php else : ?>
                                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
                                                        <!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.-->
                                                        <path d="M304 128a80 80 0 1 0 -160 0 80 80 0 1 0 160 0zM96 128a128 128 0 1 1 256 0A128 128 0 1 1 96 128zM49.3 464H398.7c-8.9-63.3-63.3-112-129-112H178.3c-65.7 0-120.1 48.7-129 112zM0 482.3C0 383.8 79.8 304 178.3 304h91.4C368.2 304 448 383.8 448 482.3c0 16.4-13.3 29.7-29.7 29.7H29.7C13.3 512 0 498.7 0 482.3z"/>
                                                    </svg>
                                                <?php endif; ?>
                                            </div>

                                            <div class="myhome-comment__user-data">
                                                <?php comment_author_link(); ?>

                                                <?php if ($comment->comment_approved) : ?>
                                                    <div class="myhome-comment__date">
                                                        <?php echo esc_html(get_comment_date()); ?>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>

                                        <?php if ($comment->comment_approved) : ?>
                                            <div class="myhome-comment__reply">
                                                <?php comment_reply_link(array_merge($args, [
                                                    'reply_text' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M205 34.8c11.5 5.1 19 16.6 19 29.2v64H336c97.2 0 176 78.8 176 176c0 113.3-81.5 163.9-100.2 174.1c-2.5 1.4-5.3 1.9-8.1 1.9c-10.9 0-19.7-8.9-19.7-19.7c0-7.5 4.3-14.4 9.8-19.5c9.4-8.8 22.2-26.4 22.2-56.7c0-53-43-96-96-96H224v64c0 12.6-7.4 24.1-19 29.2s-25 3-34.4-5.4l-160-144C3.9 225.7 0 217.1 0 208s3.9-17.7 10.6-23.8l160-144c9.4-8.5 22.9-10.6 34.4-5.4z"/></svg>',
                                                    'depth' => $depth,
                                                    'max_depth' => $args['max_depth'],
                                                    'before' => '',
                                                    'after' => ''
                                                ]));
                                                ?>
                                            </div>
                                        <?php endif; ?>
                                    </div>

                                    <?php if (!$comment->comment_approved) : ?>
                                    <div class="myhome-comment__moderate">
                                        <div class="myhome-comment__moderate__inner">
                                            <?php echo esc_html(tdf_string('comment_awaiting_moderation')); ?>
                                        </div>
                                    </div>
                                </div>
                                <?php endif; ?>
                            </div>

                            <div class="myhome-comment__text">
                                <?php comment_text(); ?>
                            </div>
                        </div>
                        <?php
                    }]); ?>
        </div>
    <?php
    endif;

    the_comments_navigation();

    if (comments_open()) : ?>
        <div class="myhome-comment-form">
            <?php
            comment_form([
                'logged_in_as' => '',
                'fields' => [
                    'author' =>
                        '<div class="myhome-comment-form__field myhome-comment-form__field--full"><div class="myhome-comment-form__icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M224 256A128 128 0 1 0 224 0a128 128 0 1 0 0 256zm-45.7 48C79.8 304 0 383.8 0 482.3C0 498.7 13.3 512 29.7 512H418.3c16.4 0 29.7-13.3 29.7-29.7C448 383.8 368.2 304 269.7 304H178.3z"/></svg></div><input id="author" name="author" ' .
                        'placeholder="' . esc_attr(tdf_string('name')) . ($myhomeRequired ? '*' : '') . '" type="text" ' .
                        'value="' . esc_attr($myhomeCommenter['comment_author']) . '" size="30" ' . $myhomeAriaReq . ' /></div>',
                    'email' =>
                        '<div class="myhome-comment-form__field"><div class="myhome-comment-form__icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M256 64C150 64 64 150 64 256s86 192 192 192c17.7 0 32 14.3 32 32s-14.3 32-32 32C114.6 512 0 397.4 0 256S114.6 0 256 0S512 114.6 512 256v32c0 53-43 96-96 96c-29.3 0-55.6-13.2-73.2-33.9C320 371.1 289.5 384 256 384c-70.7 0-128-57.3-128-128s57.3-128 128-128c27.9 0 53.7 8.9 74.7 24.1c5.7-5 13.1-8.1 21.3-8.1c17.7 0 32 14.3 32 32v80 32c0 17.7 14.3 32 32 32s32-14.3 32-32V256c0-106-86-192-192-192zm64 192a64 64 0 1 0 -128 0 64 64 0 1 0 128 0z"/></svg></div>' .
                        '<input id="email" name="email" placeholder="' . esc_attr(tdf_string('email')) . ($myhomeRequired ? '*'
                            : '') .
                        '" type="text" value="' . esc_attr($myhomeCommenter['comment_author_email']) .
                        '" size="30" ' . $myhomeAriaReq . ' /></div>',
                    'url' =>
                        '<div class="myhome-comment-form__field"><div class="myhome-comment-form__icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M352 256c0 22.2-1.2 43.6-3.3 64H163.3c-2.2-20.4-3.3-41.8-3.3-64s1.2-43.6 3.3-64H348.7c2.2 20.4 3.3 41.8 3.3 64zm28.8-64H503.9c5.3 20.5 8.1 41.9 8.1 64s-2.8 43.5-8.1 64H380.8c2.1-20.6 3.2-42 3.2-64s-1.1-43.4-3.2-64zm112.6-32H376.7c-10-63.9-29.8-117.4-55.3-151.6c78.3 20.7 142 77.5 171.9 151.6zm-149.1 0H167.7c6.1-36.4 15.5-68.6 27-94.7c10.5-23.6 22.2-40.7 33.5-51.5C239.4 3.2 248.7 0 256 0s16.6 3.2 27.8 13.8c11.3 10.8 23 27.9 33.5 51.5c11.6 26 20.9 58.2 27 94.7zm-209 0H18.6C48.6 85.9 112.2 29.1 190.6 8.4C165.1 42.6 145.3 96.1 135.3 160zM8.1 192H131.2c-2.1 20.6-3.2 42-3.2 64s1.1 43.4 3.2 64H8.1C2.8 299.5 0 278.1 0 256s2.8-43.5 8.1-64zM194.7 446.6c-11.6-26-20.9-58.2-27-94.6H344.3c-6.1 36.4-15.5 68.6-27 94.6c-10.5 23.6-22.2 40.7-33.5 51.5C272.6 508.8 263.3 512 256 512s-16.6-3.2-27.8-13.8c-11.3-10.8-23-27.9-33.5-51.5zM135.3 352c10 63.9 29.8 117.4 55.3 151.6C112.2 482.9 48.6 426.1 18.6 352H135.3zm358.1 0c-30 74.1-93.6 130.9-171.9 151.6c25.5-34.2 45.2-87.7 55.3-151.6H493.4z"/></svg></div><input id="url" name="url" placeholder="' . esc_attr(tdf_string('website')) . '" ' .
                        'type="text" value="' . esc_attr($myhomeCommenter['comment_author_url']) . '" size="30" /></div>',
                    'cookies' => '<p class="comment-form-cookies-consent">
                            <input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes">
                            <label for="wp-comment-cookies-consent"><span>' . esc_html(tdf_string('comment_cookie')) . '</span></label>
                        </p>',
                ],
                'comment_notes_before' => '',
                'title_reply' => esc_html(tdf_string('add_comment')),
                'title_reply_to' => esc_html(tdf_string('add_comment_to')) . ' %s',
                'comment_field' => '<div class="myhome-comment-form__text"><textarea id="comment" name="comment" ' .
                    'placeholder="' . esc_attr(tdf_string('comment_placeholder')) . '" aria-required="true"></textarea></div>',
                'submit_field' => '<div class="myhome-comment-form__submit">%1$s %2$s</div>',
                'submit_button' => '<button id="submit" class="myhome-button myhome-button--primary-1"><span>'
                    . esc_html(tdf_string('post_comment')) . '</span></button>'
            ]);
            ?>
        </div>
    <?php endif; ?>
</div>
