<?php
/**
 * Custom Comments
 *
 * @package Wira
 * @since 1.0.0
 */

namespace Wira;

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

if ( ! class_exists( 'Custom_Comments' ) ) {

	/**
	 * Custom Comments.
	 *
	 * @since 1.0.0
	 */
	class Custom_Comments {

		/**
		 * Constructor.
		 *
		 * Registers hooks.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			$this->load_hooks();
		}

		/**
		 * Register hooks
		 *
		 * @since 1.0.0
		 * @return void
		 */
		private function load_hooks() {
			// Hook into comment callback.
			add_filter( 'wp_list_comments_args', array( $this, 'custom_comment_callback_args' ) );
		}

		/**
		 * Attach custom callback function to comments
		 *
		 * @param array $args Comment list arguments.
		 * @return array Modified arguments.
		 */
		public function custom_comment_callback_args( $args ) {
			$args['callback'] = array( $this, 'render_comment' );
			return $args;
		}

		/**
		 * Render custom comment structure
		 *
		 * @param WP_Comment $comment Comment object.
		 * @param array      $args    Arguments.
		 * @param int        $depth   Comment depth.
		 */
		public function render_comment( $comment, $args, $depth ) {
			$GLOBALS['comment'] = $comment;

			$tag       = ( 'div' === $args['style'] ) ? 'div' : 'li';
			$add_below = ( 'div' === $args['style'] ) ? 'comment' : 'div-comment';
			$has_child = ! empty( $args['has_children'] ) ? 'parent' : '';
			$is_ping   = in_array( $comment->comment_type, array( 'pingback', 'trackback' ), true );
			?>
			
			<<?php echo esc_html( $tag ); ?> <?php comment_class( "item {$has_child}" ); ?> id="comment-<?php comment_ID(); ?>">
				<?php if ( 'div' !== $args['style'] ) : ?>
					<div id="div-comment-<?php comment_ID(); ?>">
				<?php endif; ?>

				<div class="d-md-flex mb-5 position-relative single-comment-wrap comments-item <?php echo esc_attr( $is_ping ? 'yes-ping' : '' ); ?>">
					<?php if ( ! $is_ping ) : ?>
						<div class="thumb comments-img me-3 mb-3">
							<?php
							if ( $args['avatar_size'] != 0 ) {
								echo wp_kses_post( get_avatar( $comment, 60 ) );
							}
							?>
						</div>
					<?php endif; ?>

					<div class="content comments-text">
						<h6 class="mb-1"><?php echo esc_html( get_comment_author() ); ?></h6>
						<span class="comments-meta theme-text-small"><?php echo esc_html( get_comment_date() ); ?></span>

						<?php if ( '0' === $comment->comment_approved ) : ?>
							<em class="comment-awaiting-moderation">
								<?php esc_html_e( 'Your comment is awaiting moderation.', 'wira' ); ?>
							</em>
						<?php endif; ?>

						<div class="comment-content theme-body-content mt-2">
							<?php echo wp_kses_post( get_comment_text() ); ?>
						</div>

						<div class="reply mt-3">
							<?php
							comment_reply_link(
								array_merge(
									$args,
									array(
										'depth'      => $depth,
										'max_depth'  => $args['max_depth'],
										'reply_text' => esc_html__( 'Reply', 'wira' ),
									)
								)
							);
							?>
						</div>
					</div>
				</div>

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

}
