<?php
namespace admin\Blocks\Render;

use WP_Query;

class Forums {
	public function render_forums( $attributes, $content ) {
			
		if ( ! wp_script_is( 'bbpc-ajax', 'registered' ) ) {
			wp_register_script( 'bbpc-ajax', FORUMAX_FRONT_ASS . 'js/ajax.js', array( 'jquery' ), FORUMAX_VERSION, true );
		}
		if ( ! wp_style_is( 'bbpc-el-widgets', 'registered' ) ) {
			wp_register_style( 'bbpc-el-widgets', FORUMAX_STYLES . 'frontend/el-widgets.css' );
		}

		wp_enqueue_script( 'bbpc-ajax' );
		wp_enqueue_style( 'bbpc-el-widgets' );

			
		$settings          = $attributes;
		$ppp               = ! empty( $attributes['ppp'] ) ? absint( $attributes['ppp'] ) : 5;
		$ppp2              = ! empty( $attributes['ppp2'] ) ? absint( $attributes['ppp2'] ) : 10;
		$order             = ! empty( $attributes['order'] ) ? $attributes['order'] : 'ASC';
		$orderby           = ! empty( $attributes['orderby'] ) ? $attributes['orderby'] : 'date';
		$columns           = ! empty( $attributes['columns'] ) ? absint( $attributes['columns'] ) : 3;
		$forum_image_size  = ! empty( $attributes['forum_image_size'] ) ? $attributes['forum_image_size'] : 'full';
		$show_forum_image  = isset( $attributes['show_forum_image'] ) ? (bool) $attributes['show_forum_image'] : true;
		$show_post_count   = isset( $attributes['show_post_count'] ) ? (bool) $attributes['show_post_count'] : true;
		$show_reply_count  = isset( $attributes['show_reply_count'] ) ? (bool) $attributes['show_reply_count'] : false;
		$show_excerpt      = isset( $attributes['show_excerpt'] ) ? (bool) $attributes['show_excerpt'] : false;
		$is_more_btn       = isset( $attributes['is_more_btn'] ) ? (bool) $attributes['is_more_btn'] : true;
		$more_txt          = ! empty( $attributes['more_txt'] ) ? $attributes['more_txt'] : __( 'More Communities', 'bbp-core' );
		$less_txt          = ! empty( $attributes['less_txt'] ) ? $attributes['less_txt'] : __( 'Less Communities', 'bbp-core' );
		$no_posts_message  = ! empty( $attributes['no_posts_message'] ) ? $attributes['no_posts_message'] : __( 'No forums found.', 'bbp-core' );
		$enable_card_hover = isset( $attributes['enable_card_hover'] ) ? (bool) $attributes['enable_card_hover'] : true;
		$mobile_columns    = ! empty( $attributes['mobile_columns'] ) ? absint( $attributes['mobile_columns'] ) : 1;
		$hide_image_mobile = isset( $attributes['hide_image_mobile'] ) ? (bool) $attributes['hide_image_mobile'] : false;

		$unique_id = uniqid( 'bbpc-forums-' );

		// Enforce free defaults when Pro is not active.
		$is_pro_active = function_exists( 'forumax_is_premium' ) && forumax_is_premium();
		if ( ! $is_pro_active ) {
			$columns            = 3;
			$mobile_columns     = 1;
			$hide_image_mobile  = false;
		}

			
		$sanitize_css = function ( $value ) {
			return preg_match( '/^[a-zA-Z0-9#(),.\s%-]+$/', $value ) ? $value : '';
		};

			
		$query_args = array(
			'post_type'      => 'forum',
			'posts_per_page' => $ppp,
			'order'          => $order,
			'orderby'        => $orderby,
		);

		// Filter by IDs (Pro feature).
		if ( $is_pro_active && ! empty( $attributes['forum_filter_ids'] ) ) {
			$ids = array_map( 'absint', array_filter( explode( ',', $attributes['forum_filter_ids'] ) ) );
			if ( ! empty( $ids ) ) {
				$query_args['post__in'] = $ids;
			}
		}

		$forums = new WP_Query( $query_args );

		
		$query_args2                  = $query_args;
		$query_args2['posts_per_page'] = $ppp2;
		$query_args2['offset']         = $ppp;
		$forums2                       = new WP_Query( $query_args2 );

		
		$hover_class = $enable_card_hover ? ' wow fadeInRight' : '';

		
		$svg_minus = '<svg fill="currentColor" width="16px" height="16px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="icon line-color icon_minus"><line x1="19" y1="12" x2="5" y2="12" style="fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2;"></line></svg>';
		$svg_plus  = '<svg fill="currentColor" width="16px" height="16px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="icon line-color icon_plus"><path d="M5,12H19M12,5V19" style="fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2;"></path></svg>';

		ob_start();
		?>
		<div id="<?php echo esc_attr( $unique_id ); ?>">
			<?php if ( $forums->have_posts() ) : ?>
				<div class="communities-boxes">
					<?php
					$delay = 0.1;
					while ( $forums->have_posts() ) :
						$forums->the_post();
						$item_id = get_the_ID();
						?>
						<div class="com-box<?php echo esc_attr( $hover_class ); ?>" data-wow-delay="<?php echo esc_attr( $delay ); ?>s">
						<?php $delay += 0.1; ?>
							<?php if ( $show_forum_image && get_the_post_thumbnail( get_the_ID(), $forum_image_size ) ) : ?>
								<div class="icon-container">
									<?php the_post_thumbnail( $forum_image_size ); ?>
								</div>
							<?php endif; ?>
							<div class="com-box-content">
								<h3 class="title">
									<a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
								</h3>
								<?php if ( $show_excerpt ) : ?>
									<p class="bbpc-forum-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?></p>
								<?php endif; ?>
								<?php if ( $show_post_count || $show_reply_count ) : ?>
									<div class="bbpc-forum-meta">
										<?php if ( $show_post_count ) : ?>
											<span class="total-post"><?php bbp_forum_topic_count( $item_id ); ?> <?php esc_html_e( 'Posts', 'bbp-core' ); ?></span>
										<?php endif; ?>
										<?php if ( $show_post_count && $show_reply_count ) : ?>
											<span class="bbpc-meta-sep"> Â· </span>
										<?php endif; ?>
										<?php if ( $show_reply_count ) : ?>
											<span class="total-reply"><?php bbp_forum_reply_count( $item_id ); ?> <?php esc_html_e( 'Replies', 'bbp-core' ); ?></span>
										<?php endif; ?>
									</div>
								<?php endif; ?>
							</div>
						</div>
					<?php endwhile; ?>
					<?php wp_reset_postdata(); ?>
				</div>

				<?php if ( $is_more_btn && $forums2->have_posts() ) : ?>
					<div class="more-communities" data_id="<?php echo esc_attr( $unique_id ); ?>">
						<a href="#more-category" class="collapse-btn-wrap" data-more="<?php echo esc_attr( $more_txt ); ?>" data-less="<?php echo esc_attr( $less_txt ); ?>">
							<?php echo esc_html( $more_txt ); ?>
							<?php echo wp_kses_post( $svg_minus ); ?>
							<?php echo wp_kses_post( $svg_plus ); ?>
						</a>

						<div class="collapse-wrap" id="more-category" data_id="<?php echo esc_attr( $unique_id ); ?>">
							<div class="communities-boxes">
								<?php
								while ( $forums2->have_posts() ) :
									$forums2->the_post();
									$item_id = get_the_ID();
									?>
									<div class="com-box">
										<?php if ( $show_forum_image && get_the_post_thumbnail( get_the_ID(), $forum_image_size ) ) : ?>
											<div class="icon-container">
												<?php the_post_thumbnail( $forum_image_size ); ?>
											</div>
										<?php endif; ?>
										<div class="com-box-content">
											<h3 class="title">
												<a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
											</h3>
											<?php if ( $show_excerpt ) : ?>
												<p class="bbpc-forum-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?></p>
											<?php endif; ?>
											<?php if ( $show_post_count || $show_reply_count ) : ?>
												<div class="bbpc-forum-meta">
													<?php if ( $show_post_count ) : ?>
														<span class="total-post"><?php bbp_forum_topic_count( $item_id ); ?> <?php esc_html_e( 'Posts', 'bbp-core' ); ?></span>
													<?php endif; ?>
													<?php if ( $show_post_count && $show_reply_count ) : ?>
														<span class="bbpc-meta-sep"> Â· </span>
													<?php endif; ?>
													<?php if ( $show_reply_count ) : ?>
														<span class="total-reply"><?php bbp_forum_reply_count( $item_id ); ?> <?php esc_html_e( 'Replies', 'bbp-core' ); ?></span>
													<?php endif; ?>
												</div>
											<?php endif; ?>
										</div>
									</div>
								<?php endwhile; ?>
								<?php wp_reset_postdata(); ?>
							</div>
						</div>
					</div>
				<?php endif; ?>

			<?php else : ?>
				<div class="alert alert-warning"><?php echo esc_html( $no_posts_message ); ?></div>
			<?php endif; ?>
		</div>

		<?php
		
		$styles = '';
		$id     = '#' . esc_attr( $unique_id );

		
		if ( $columns ) {
			$styles .= "{$id} .communities-boxes { display: grid; grid-template-columns: repeat({$columns}, 1fr); margin: 0; width: 100%; }";
			$styles .= "{$id} .communities-boxes .com-box { width: 100%; flex: none; margin-bottom: 0; }";
		}

		// Card gap.
		if ( ! empty( $settings['card_gap'] ) ) {
			$gap     = absint( $settings['card_gap'] );
			$styles .= "{$id} .communities-boxes { gap: {$gap}px !important; }";
		}

		// Card background.
		if ( ! empty( $settings['card_bg_color'] ) ) {
			$color   = $sanitize_css( $settings['card_bg_color'] );
			$styles .= "{$id} .com-box { background-color: {$color} !important; }";
		}

		// Card border radius.
		if ( ! empty( $settings['card_border_radius'] ) ) {
			$radius  = absint( $settings['card_border_radius'] );
			$styles .= "{$id} .com-box { border-radius: {$radius}px !important; }";
		}

		// Title color.
		if ( ! empty( $settings['title_color'] ) ) {
			$color   = $sanitize_css( $settings['title_color'] );
			$styles .= "{$id} .com-box-content .title a { color: {$color} !important; }";
		}

		// Title hover color.
		if ( ! empty( $settings['title_hover_color'] ) ) {
			$color   = $sanitize_css( $settings['title_hover_color'] );
			$styles .= "{$id} .com-box-content .title a:hover { color: {$color} !important; }";
		}

		// Content color.
		if ( ! empty( $settings['content_color'] ) ) {
			$color   = $sanitize_css( $settings['content_color'] );
			$styles .= "{$id} .com-box-content .total-post, {$id} .com-box-content .total-reply, {$id} .com-box-content .bbpc-forum-excerpt, {$id} .com-box-content .bbpc-forum-meta { color: {$color} !important; }";
		}

		// More text color.
		if ( ! empty( $settings['more_text_color'] ) ) {
			$color   = $sanitize_css( $settings['more_text_color'] );
			$styles .= "{$id} .collapse-btn-wrap { color: {$color} !important; }";
			$styles .= "{$id} .more-communities .collapse-btn-wrap svg path, {$id} .more-communities .collapse-btn-wrap svg line { stroke: {$color} !important; }";
		}

		// Button color.
		if ( ! empty( $settings['btn_color'] ) ) {
			$color   = $sanitize_css( $settings['btn_color'] );
			$styles .= "{$id} .collapse-btn-wrap { color: {$color} !important; }";
		}

		// Button background.
		if ( ! empty( $settings['btn_bg_color'] ) ) {
			$color   = $sanitize_css( $settings['btn_bg_color'] );
			$styles .= "{$id} .collapse-btn-wrap { background-color: {$color} !important; }";
		}

		// Button border radius.
		if ( ! empty( $settings['btn_border_radius'] ) ) {
			$radius  = absint( $settings['btn_border_radius'] );
			$styles .= "{$id} .collapse-btn-wrap { border-radius: {$radius}px !important; }";
		}

		// Title font size.
		if ( ! empty( $settings['title_font_size'] ) ) {
			$size    = absint( $settings['title_font_size'] );
			$styles .= "{$id} .com-box-content .title a { font-size: {$size}px !important; }";
		}

		// Meta font size.
		if ( ! empty( $settings['meta_font_size'] ) ) {
			$size    = absint( $settings['meta_font_size'] );
			$styles .= "{$id} .com-box-content .total-post, {$id} .com-box-content .total-reply, {$id} .com-box-content .bbpc-forum-excerpt, {$id} .com-box-content .bbpc-forum-meta { font-size: {$size}px !important; }";
		}

		// Hover animation disabled.
		if ( ! $enable_card_hover ) {
			$styles .= "{$id} .com-box:hover { transform: none !important; box-shadow: none !important; }";
		}

		// Responsive: mobile columns.
		if ( $mobile_columns || $hide_image_mobile ) {
			$styles .= "@media (max-width: 768px) {";
			if ( $mobile_columns ) {
				$styles .= "{$id} .communities-boxes { grid-template-columns: repeat({$mobile_columns}, 1fr) !important; }";
				$styles .= "{$id} .communities-boxes .com-box { flex: none !important; }";
			}
			if ( $hide_image_mobile ) {
				$styles .= "{$id} .icon-container { display: none !important; }";
			}
			$styles .= "}";
		}

		// Output styles.
		if ( ! empty( $styles ) ) {
			echo '<style>' . $styles . '</style>';
		}

		return ob_get_clean();
	}
}