<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Comment_Box_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Commentbox extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Comment_Box_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'comment-box',
			'title'          => 'Sifency Comment Box',
			'icon'           => 'eicon-comments',
			'category'       => 'sifency-post-components',
			'keywords'       => array( 'comments', 'comment box', 'post', 'discussion' ),
			'style_depends'  => array( 'sifency-frontend-style', 'sifency-post-single-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_comment_box_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render() {
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			?>
			<div class="sifency-comments-area">
				<?php $this->render_editor_placeholder(); ?>
			</div>
			<?php
			return;
		}
		if ( ! is_singular() ) {
			return;
		}
		$post_id           = get_the_ID();
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( array( 'sifency-comments-area' ) );

		if ( comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}

		$comments = get_comments(
			array(
				'post_id' => $post_id,
				'status'  => 'approve',
			)
		);

		if ( is_user_logged_in() ) {
			$user_id          = get_current_user_id();
			$pending_comments = get_comments(
				array(
					'post_id' => $post_id,
					'status'  => 'hold',
					'user_id' => $user_id,
				)
			);
			$comments         = array_merge( $comments, $pending_comments );
		}

		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<div id="comments" class="comments-area">
				
				<?php
				$comments_number = get_comments_number( $post_id );
				if ( $comments_number > 0 ) :
					?>
					<h2 class="comments-title">
						<?php
						if ( '1' === $comments_number ) {
							printf( esc_html__( 'One Comment', 'sifency-addons' ) );
						} else {
							printf(
								esc_html( /* translators: Count of comments */ _n( '%1$s Comment', '%1$s Comments', $comments_number, 'sifency-addons' ) ),
								'<span class="comments-number">' . esc_html( number_format_i18n( $comments_number ) ) . '</span>'
							);
						}
						?>
					</h2>

					<ol class="comment-list">
						<?php
						wp_list_comments(
							array(
								'style'       => 'ol',
								'short_ping'  => true,
								'avatar_size' => 60,
								'callback'    => array( $this, 'render_custom_comment' ),
								'echo'        => true,
							),
							$comments
						);
						?>
					</ol>

					<?php
					the_comments_navigation();
					?>

				<?php endif; ?>

				<?php
				if ( ! comments_open() && $comments_number > 0 && post_type_supports( get_post_type(), 'comments' ) ) :
					?>
					<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'sifency-addons' ); ?></p>
					<?php
				endif;

				if ( comments_open() ) {
					$comment_form_args = array(
						'class_form'         => 'comment-form',
						'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">',
						'title_reply_after'  => '</h3>',
						'submit_button'      => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
					);
					comment_form( $comment_form_args );
				}
				?>
			</div>
		</div>
		<?php
	}
	public function render_custom_comment( $comment, $args, $depth ) {
		$GLOBALS['comment'] = $comment;
		?>
		<li <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">
			<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-author vcard">
						<?php echo get_avatar( $comment, $args['avatar_size'] ); ?>
						<?php printf( '<b class="fn">%s</b>', get_comment_author_link() ); ?>
						<span class="says"><?php echo esc_html__( 'says:', 'sifency-addons' ); ?></span>
					</div>
					<div class="comment-metadata">
						<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
							<time datetime="<?php comment_time( 'c' ); ?>">
								<?php
								printf(
									/* translators: 1: Comment date, 2: Comment time */
									esc_html__( '%1$s at %2$s', 'sifency-addons' ),
									esc_html( get_comment_date() ),
									esc_html( get_comment_time() )
								);
								?>
							</time>
						</a>
					</div>
				</footer>

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

				<div class="reply">
					<?php
					comment_reply_link(
						array_merge(
							$args,
							array(
								'add_below' => 'div-comment',
								'depth'     => $depth,
								'max_depth' => $args['max_depth'],
							)
						)
					);
					?>
				</div>
			</article>
		<?php
	}
	protected function render_editor_placeholder() {
		?>
		<div id="comments" class="comments-area">
			<h2 class="comments-title">
				<?php
				printf(
					/* translators: %s: Number Of Comments. */
					esc_html( _n( '%1$s Comment', '%1$s Comments', 2, 'sifency-addons' ) ),
					'<span class="comments-number">2</span>'
				);
				?>
			</h2>

			<ol class="comment-list">
				<li class="comment even thread-even depth-1" id="comment-1">
					<article id="div-comment-1" class="comment-body">
						<div class="comment-meta">
							<div class="comment-author vcard">
								<img alt="" src="<?php echo esc_url( get_avatar_url( 'placeholder1@example.com' ) ); ?>" class="avatar avatar-60 photo" height="60" width="60">
								<b class="fn"><?php echo esc_html__( 'John Doe', 'sifency-addons' ); ?></b>
								<span class="says"><?php echo esc_html__( 'says:', 'sifency-addons' ); ?></span>
							</div>
							<div class="comment-metadata">
								<a href="#">
									<time datetime="2023-10-27T10:00:00+00:00"><?php echo esc_html__( 'October 27, 2023 at 10:00 am', 'sifency-addons' ); ?></time>
								</a>
							</div>
						</footer>
						<div class="comment-content">
							<p><?php echo esc_html__( 'This is a demo comment. You can style its appearance using the controls in the Style tab. This helps in visualizing how the comments section will look on the live site.', 'sifency-addons' ); ?></p>
						</div>
						<div class="reply">
							<a rel="nofollow" class="comment-reply-link" href="#"><?php echo esc_html__( 'Reply', 'sifency-addons' ); ?></a>
						</div>
					</article>
					<ol class="children">
						<li id="comment-2" class="comment byuser comment-author-admin bypostauthor odd alt depth-2">
							<article id="div-comment-2" class="comment-body">
								<div class="comment-meta">
									<div class="comment-author vcard">
									<img alt="" src="<?php echo esc_url( get_avatar_url( 'placeholder2@example.com' ) ); ?>" class="avatar avatar-60 photo" height="60" width="60">
										<b class="fn"><?php echo esc_html__( 'Jane Smith (Author)', 'sifency-addons' ); ?></b>
										<span class="says"><?php echo esc_html__( 'says:', 'sifency-addons' ); ?></span>
									</div>
									<div class="comment-metadata">
										<a href="#"><time><?php echo esc_html__( 'October 27, 2023 at 10:05 am', 'sifency-addons' ); ?></time></a>
									</div>
								</footer>
								<div class="comment-content">
									<p><?php echo esc_html__( 'This is a nested reply from the post author to demonstrate different levels of comments.', 'sifency-addons' ); ?></p>
								</div>
								<div class="reply">
									<a rel="nofollow" class="comment-reply-link" href="#"><?php echo esc_html__( 'Reply', 'sifency-addons' ); ?></a>
								</div>
							</article>
						</li>
					</ol>
				</li>
			</ol>

			<div id="respond" class="comment-respond">
				<h3 id="reply-title" class="comment-reply-title">
					<?php echo esc_html__( 'Leave a Reply', 'sifency-addons' ); ?>
				</h3>
				<form action="#" method="post" id="commentform" class="comment-form">
					<p class="comment-notes">
						<span id="email-notes"><?php echo esc_html__( 'Your email address will not be published.', 'sifency-addons' ); ?></span>
					</p>
					<p class="comment-form-comment">
						<label for="comment"><?php echo esc_html__( 'Comment', 'sifency-addons' ); ?></label>
						<textarea id="comment" name="comment" cols="45" rows="8" required></textarea>
					</p>
					<p class="comment-form-author">
						<label for="author"><?php echo esc_html__( 'Name', 'sifency-addons' ); ?></label>
						<input id="author" name="author" type="text" value="" size="30" required>
					</p>
					<p class="comment-form-email">
						<label for="email"><?php echo esc_html__( 'Email', 'sifency-addons' ); ?></label>
						<input id="email" name="email" type="email" value="" size="30" required>
					</p>
					<p class="form-submit">
						<input name="submit" type="submit" id="submit" class="submit" value="<?php echo esc_attr__( 'Post Comment', 'sifency-addons' ); ?>">
					</p>
				</form>
			</div>
		</div>
		<?php
	}

	/**
	 * Render the widget output in the editor for live preview.
	 *
	 * Written as a Backbone JavaScript template. This provides a static HTML structure
	 * so that style controls can be previewed instantly.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		$placeholder_avatar_url = \Elementor\Utils::get_placeholder_image_src();
		?>
		<div class="sifency-comments-area">
			<div id="comments" class="comments-area">
				<h2 class="comments-title">
					<span class="comments-number">2</span> Comments
				</h2>

				<ol class="comment-list">
					<li class="comment even thread-even depth-1" id="comment-1">
						<article id="div-comment-1" class="comment-body">
							<div class="comment-meta">
								<div class="comment-author vcard">
									<img alt="" src="<?php echo esc_url( $placeholder_avatar_url ); ?>" class="avatar avatar-60 photo" height="60" width="60">
									<b class="fn">John Doe</b> <span class="says">says:</span>
								</div>
								<div class="comment-metadata">
									<a href="#"><time>October 27, 2023 at 10:00 am</time></a>
								</div>
							</footer>
							<div class="comment-content">
								<p>This is a demo comment. You can style its appearance using the controls in the Style tab. This helps in visualizing how the comments section will look on the live site.</p>
							</div>
							<div class="reply">
								<a rel="nofollow" class="comment-reply-link" href="#">Reply</a>
							</div>
						</article>
						<ol class="children">
							<li id="comment-2" class="comment byuser comment-author-admin bypostauthor odd alt depth-2">
								<article id="div-comment-2" class="comment-body">
									<div class="comment-meta">
										<div class="comment-author vcard">
										<img alt="" src="<?php echo esc_url( $placeholder_avatar_url ); ?>" class="avatar avatar-60 photo" height="60" width="60">
											<b class="fn">Jane Smith (Author)</b> <span class="says">says:</span>
										</div>
										<div class="comment-metadata">
											<a href="#"><time>October 27, 2023 at 10:05 am</time></a>
										</div>
									</footer>
									<div class="comment-content">
										<p>This is a nested reply from the post author to demonstrate different levels of comments.</p>
									</div>
									<div class="reply">
										<a rel="nofollow" class="comment-reply-link" href="#">Reply</a>
									</div>
								</article>
							</li>
						</ol>
					</li>
				</ol>

				<div id="respond" class="comment-respond">
					<h3 id="reply-title" class="comment-reply-title">Leave a Reply</h3>
					<form action="#" method="post" id="commentform" class="comment-form">
						<p class="comment-notes">
							<span id="email-notes">Your email address will not be published.</span>
						</p>
						<p class="comment-form-comment">
							<label for="comment">Comment</label>
							<textarea id="comment" name="comment" cols="45" rows="8" required></textarea>
						</p>
						<p class="comment-form-author">
							<label for="author">Name</label>
							<input id="author" name="author" type="text" value="" size="30" required>
						</p>
						<p class="comment-form-email">
							<label for="email">Email</label>
							<input id="email" name="email" type="email" value="" size="30" required>
						</p>
						<p class="form-submit">
							<input name="submit" type="submit" id="submit" class="submit" value="Post Comment">
						</p>
					</form>
				</div>
			</div>
		</div>
		<?php
	}
}
