<?php

/**
 * Single Topic Lead Content Part
 *
 * @package bbPress
 * @subpackage Theme
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

do_action( 'bbp_template_before_lead_topic' );

$topic_tag_list    = bbp_get_topic_tag_list(
	get_the_ID(),
	array(
		'before' => '',
		'after'  => '',
		'sep'    => ', ',
		'none'   => '',
	)
);
$has_topic_tags    = ! empty( $topic_tag_list );
$action_btns_class = $has_topic_tags ? 'has-topic-tags' : 'no-topic-tags';
?>
<div class="main-post">
	<div class="topic-content-wrapper">

		<div class="forum-post-top">
			<div class="bbp-author-wrap">
				<div class="author-avatar bbp-author-link">
					<?php
					echo bbp_get_topic_author_link(
						array(
							'post_id' => get_the_ID(),
							'size'    => 40,
							'type'    => 'avatar',
						)
					);
					?>
				</div>

				<div class="forum-post-author">
					<div class="topic-author author-flex author-gap">
						<?php
						echo bbp_get_topic_author_link(
							array(
								'post_id' => get_the_ID(),
								'type'    => 'name',
							)
						);
						?>
						<div class="author-badge badge <?php echo esc_attr( sanitize_title( forumax_get_bbp_user_role() ) ); ?>">
							<?php forumax_bbp_user_role_icon(); ?>
							<span><?php echo esc_html( forumax_get_bbp_user_role() ); ?></span>
						</div>
					</div>

					<div class="forum-author-meta meta">
						<span title="<?php bbp_topic_post_date( get_the_ID() ); ?>">
							<?php bbp_topic_post_date( get_the_ID(), true ); ?>
						</span>
						<span class="dot"></span>
						<a href="#topic-<?php bbp_topic_id(); ?>-replies" class="replies-count">
							<?php
							forumax_topic_reply_count();
							esc_html_e( ' Replies', 'forumax' );
							?>
						</a>
					</div>
				</div>
			</div>

			<div class="action-button-container">
				<?php
				if ( is_user_logged_in() ) :
					bbp_topic_subscription_link(
						array(
							'before'      => '',
							'after'       => '',
							'unsubscribe' => esc_html__( 'Subscribed', 'forumax' ),
						)
					);
				else :
					if ( get_option( '_bbp_enable_subscriptions' ) == 1 && forumax_get_opt( 'is_topic_subscription' ) == 1 ) :
						?>
						<span id="subscription-toggle">
							<a class="subscription-login-btn">
								<?php echo esc_html( forumax_get_opt( 'logout_subscription_btn' ) ); ?>
							</a>
						</span>
						<span class="subscription-login-modal">
							<span class="subscription-login-modal-inner">
								<span class="subscription-login-modal-close">&times;</span>
								<h2><?php echo esc_html( forumax_get_opt( 'subscription_modal_title' ) ); ?></h2>
								<p><?php echo esc_html( forumax_get_opt( 'subscription_modal_subtitle' ) ); ?></p>
								<div class="frmx-subscription-login-form">
									<form method="post" action="<?php bbp_wp_login_action( array( 'context' => 'login_post' ) ); ?>" class="bbp-login-form">
										<fieldset class="bbp-form">
											<legend><?php esc_html_e( 'Log In', 'forumax' ); ?></legend>

											<div class="bbp-username">
												<label for="frmx-subscription-user-login"><?php esc_html_e( 'Username', 'forumax' ); ?>: </label>
												<input type="text" name="log" value="" size="20" maxlength="100" id="frmx-subscription-user-login" placeholder="<?php esc_attr_e( 'Username or Email', 'forumax' ); ?>" autocomplete="off" />
											</div>

											<div class="bbp-password">
												<label for="frmx-subscription-user-pass"><?php esc_html_e( 'Password', 'forumax' ); ?>: </label>
												<input type="password" name="pwd" value="" size="20" id="frmx-subscription-user-pass" placeholder="<?php esc_attr_e( 'Password', 'forumax' ); ?>" autocomplete="off" />
											</div>

											<div class="bbp-remember-me">
												<input type="checkbox" name="rememberme" value="forever" id="frmx-subscription-rememberme" />
												<label for="frmx-subscription-rememberme"><?php esc_html_e( 'Keep me signed in', 'forumax' ); ?></label>
											</div>

											<?php do_action( 'login_form' ); ?>

											<div class="bbp-submit-wrapper">
												<button type="submit" name="user-submit" id="frmx-subscription-user-submit" class="button submit user-submit fill-brand"><?php esc_html_e( 'Log In', 'forumax' ); ?></button>
												<?php bbp_user_login_fields(); ?>
											</div>
										</fieldset>
									</form>
								</div>
							</span>
						</span>
						<?php
					endif;
				endif;
				?>

				<?php
				/**
				 * Topic admin links dropdown (⋯ three-dot menu).
				 * Placed next to Subscribe for quick access.
				 */
				if ( is_user_logged_in() && function_exists( 'forumax_get_topic_admin_links' ) ) {
					echo forumax_get_topic_admin_links(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				}
				?>
			</div>
		</div>

		<div class="q-title">
			<span class="question-icon" title="Question">
				<?php echo esc_html( forumax_get_opt( 'topic_title_prefix', 'Q:' ) ); ?>
			</span>
			<h1>
				<?php do_action( 'bbp_theme_before_topic_title' ); ?>
				<?php
				the_title();
				do_action( 'bbpc-resolved-topics' );
				?>
			</h1>
		</div>

		<div class="forum-post-content">
			<?php do_action('bbp_theme_after_topic_author_details'); ?>
			<div class="topic-content-layout">
				<?php
				$topic_post = get_post();
				$forum_id   = $topic_post ? bbp_get_topic_forum_id( $topic_post->ID ) : 0;
				$forum_vote = $forum_id ? get_post_meta( $forum_id, 'bbp_voting_forum_enable_topics', true ) : '';

				// Note: despite the option id name, this switcher is used as an enable flag in the voting frontend.
				$topic_voting_enabled = ! empty( forumax_get_opt( 'is_voting_disabled_topics', 0 ) );
				$voting_feature_on    = (bool) forumax_get_opt( 'is_votes', true );

				$show_vote_column = $voting_feature_on
					&& (bool) has_action( 'bbp_voting_cpt' )
					&& ( ( ! empty( $forum_vote ) && $forum_vote !== 'false' ) || ( empty( $forum_vote ) && $topic_voting_enabled ) );
				?>
				<?php if ( $show_vote_column ) : ?>
					<div class="topic-vote-column">
						<?php
						/**
						 * Render only the voting UI in the left column.
						 */
						do_action( 'bbp_voting_cpt', $topic_post );
						?>
					</div>
				<?php endif; ?>

				<div class="topic-content-body">
					<div class="content editor-content">
						<?php do_action('bbp_theme_before_topic_content'); ?>
						<?php bbp_topic_content(); ?>
					</div>

					<div class="action-button-container action-btns <?php echo esc_attr( $action_btns_class ); ?>">
						<?php
						if ( has_action( 'bbp_theme_after_topic_content', 'bbp_voting_buttons' ) ) {
							remove_action( 'bbp_theme_after_topic_content', 'bbp_voting_buttons' );
						}
						?>
						<?php do_action('bbp_theme_after_topic_content'); ?>
						<?php if ( $has_topic_tags ) : ?>
							<div class="taxonomy forum-post-tags">
								<?php
								echo wp_kses_post(
									'<span class="tags-label">' . esc_html__( 'Tags:', 'forumax' ) . '</span> <span class="tags">' . $topic_tag_list . '</span>'
								);
								?>
							</div>
						<?php endif; ?>

						<div class="frmx-action-btns-right">
							<?php
							bbp_topic_favorite_link(
								array(
									'before' => '',
								)
							);
							?>
							<?php
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							echo forumax_topic_social_share();
							?>
						</div>
					</div>
				</div>
			</div>
		</div>

	</div>
</div>

<?php
do_action( 'bbp_template_after_lead_topic' );
