<?php
// Hooks.
add_action('bbpc-resolved-topics', 'forumax_solve_topic_callback', 10, 2);

// functions
function forumax_solve_topic_callback($content = '', $topic_id = 0)
{

	if (forumax_get_opt('is_solved_topics', true)) {

		if (empty($topic_id)) {
			$topic_id = bbp_get_topic_id($topic_id);
		}

		$open = '';
		$close = '';
		$resolved = '';
		$has_best_answer = '';

		$obj = features\forumax_solve_topic();

		if ($obj->is_solved($topic_id) || $obj->has_best_answer($topic_id)) {
			$open = '<div class="solved-topic-bar">';
			$close = '</div>';
		}

		if ($obj->is_solved($topic_id)) {
			$resolved = '<span class="accepted-ans-mark badge"><i class="icon_check_alt"></i> ' . esc_html__('Resolved', 'forumax') . '</span>';
		}

		$content = $content . $open . $resolved . $has_best_answer . $close;

		$allowed_html = [
			'div' => ['class' => []],
			'span' => ['class' => []],
			'i' => ['class' => []],
		];

		echo wp_kses($content, $allowed_html);
	}
}

// Forumax Social Share Hook - Triggered manually in content-single-topic-lead.php
// add_action( 'bbp_theme_after_topic_content', 'forumax_topic_social_share' );

function forumax_topic_social_share()
{
	if ( ! forumax_get_opt( 'is_social_links', true ) ) {
		return '';
	}

	$is_copy_link = forumax_get_opt('is_copy_link');
	$copy_link_text_success = forumax_get_opt('copy_link_text_success', esc_html__('Copied!', 'forumax'));
	$copy_link_label = forumax_get_opt('copy_link_label', esc_html__('Or copy link', 'forumax'));

	if (is_user_logged_in()):
		?>
		<a href="#" class="forumax-share-btn modal-toggle" data-id="forumax_share_modal">
			<i class="social_share_square"></i>
			<?php echo esc_html(forumax_get_opt('share_btn_label', esc_html__('Share this Topic', 'forumax'))) ?>
		</a>

		<div class="forumax-modal" id='forumax_share_modal'>
			<div class="forumax-modal-overlay modal-toggle"></div>
			<div class="forumax-modal-dialog" role="document">
				<div class="forumax-modal-content">
					<a class="close forumax-close">
						<i class="icon_close"></i>
					</a>
					<div class="forumax-share-wrap">
						<h2> <?php the_title(); ?> </h2>
						<div class="social-links">
							<a href="mailto:?subject=<?php echo rawurlencode(bbp_get_topic_title(bbp_get_topic_id())); ?>&amp;body=<?php echo rawurlencode(bbp_get_topic_permalink(bbp_get_topic_id())); ?>"
								target="_blank">
								<i class="icon_mail"></i>
							</a>
							<a
								href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(bbp_get_topic_permalink(bbp_get_topic_id())); ?>">
								<i class="social_facebook_circle"></i>
							</a>
							<a
								href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo urlencode(bbp_get_topic_permalink(bbp_get_topic_id())); ?>">
								<i class="social_linkedin_square"></i>
							</a>
							<a
								href="https://x.com/intent/tweet?url=<?php echo urlencode(bbp_get_topic_permalink(bbp_get_topic_id())); ?>&amp;text=<?php echo urlencode(bbp_get_topic_title(bbp_get_topic_id())); ?>">
								<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M12.6.75h2.454l-5.36 6.142L16 15.25h-4.937l-3.867-5.07-4.425 5.07H.316l5.733-6.57L0 .75h5.063l3.495 4.633L12.601.75Zm-.86 13.028h1.36L4.323 2.145H2.865z"/></svg>
							</a>
						</div>

						<?php
						if ($is_copy_link):
							?>
							<p> <?php echo esc_html($copy_link_label); ?> </p>
							<div class="copy-url-wrap">
								<input readonly type="text"
									value="<?php echo esc_url(bbp_get_topic_permalink(bbp_get_topic_id())); ?>"
									class="word-wrap">
								<div class="share-this-doc" title="<?php esc_attr_e('Copy Link', 'forumax'); ?>">
									<img src="<?php echo FORUMAX_FRONT_ASS . 'img/clone.svg'; ?>"
										alt="<?php esc_attr_e('Clipboard Icon', 'forumax'); ?>">
								</div>
								<div class="forumax-link-copied-wrap">
									<i class="icon_check_alt2"></i>
									<?php echo esc_attr($copy_link_text_success) ?>
									<i class="icon_close close-notice"></i>
								</div>
							</div>
						<?php
						endif;
						?>

					</div>
				</div>
			</div>
		</div>
	<?php endif;
}
