<?php
namespace admin\Blocks\Render;

use WP_Query;

class Search {
	public function render_search($attributes, $content)
	{
		$placeholder = isset($attributes['placeholder']) ? $attributes['placeholder'] : 'Search for Topics....';
		$submit_btn_type = isset($attributes['submit_btn_type']) ? $attributes['submit_btn_type'] : 'icon';
		$submit_btn_text = isset($attributes['submit_btn_text']) ? $attributes['submit_btn_text'] : __('Search', 'forumax');
		$submit_btn_align = isset($attributes['submit_btn_align']) ? $attributes['submit_btn_align'] : 'left';
		$submit_btn_icon = isset($attributes['submit_btn_icon']) ? $attributes['submit_btn_icon'] : ['value' => 'fas fa-search', 'library' => 'solid'];
		$is_keywords = isset($attributes['is_keywords']) ? $attributes['is_keywords'] : true;
		$keywords_label = isset($attributes['keywords_label']) ? $attributes['keywords_label'] : 'Popular:';
		$keywords_align = isset($attributes['keywords_align']) ? $attributes['keywords_align'] : 'center';
		$keywords = isset($attributes['keywords']) ? $attributes['keywords'] : [['title' => 'Keyword #1'], ['title' => 'Keyword #2']];
		$search_post_type = isset($attributes['search_post_type']) ? $attributes['search_post_type'] : 'docs';

		// AJAX Search and Highlight are Pro-only features.
		$is_pro_active   = class_exists( 'Forumax_Pro' ) && function_exists( 'bc_fs' ) && bc_fs()->can_use_premium_code();
		$search_highlight = ( $is_pro_active && ! empty( $attributes['search_highlight'] ) ) ? 'true' : 'false';
		$is_ajax_search  = ( $is_pro_active && ! empty( $attributes['is_ajax_search'] ) ) ? 'true' : 'false';

		// Enforce free defaults for Pro-gated attributes.
		if ( ! $is_pro_active ) {
			$search_post_type = 'forum';
			unset( $attributes['wrapper_max_width'] );
		}

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

		$cross_position = $submit_btn_align == 'right' ? 'left' : 'right';

		// Include dependencies.
		if ( ! wp_style_is( 'bbpc-el-widgets', 'registered' ) ) {
			wp_register_style( 'bbpc-el-widgets', FORUMAX_STYLES . 'frontend/el-widgets.css' );
		}
		wp_enqueue_style( 'bbpc-el-widgets' );

		wp_enqueue_script( 'bbpc-frontend-js', FORUMAX_FRONT_ASS . 'js/frontend.js', array( 'jquery' ), FORUMAX_VERSION, true );

		// Localize AJAX data for the search script.
		wp_localize_script( 'bbpc-frontend-js', 'forumax_localize_script', array(
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
			'nonce'   => wp_create_nonce( 'forumax-nonce' ),
		) );

		$wrapper_attributes = get_block_wrapper_attributes([
			'id' => $unique_id,
		]);

		ob_start();
		?>
		<div <?php echo $wrapper_attributes; ?>>
			<div class="bbpc-search-overlay"></div>

			<form action="<?php echo esc_url(home_url('/')) ?>" role="search" method="get" class="bbpc_search_form_wrapper">
				<div class="form-group">
					<div class="input-wrapper <?php echo esc_attr($cross_position); ?>">

						<span class="submit-btn-<?php echo esc_attr($submit_btn_align); ?>">
							<button type="submit">
								<?php
								if ($submit_btn_type == 'icon') {
									// Use inline SVG for editor compatibility (Font Awesome is not loaded in Gutenberg).
									if (is_array($submit_btn_icon) && !empty($submit_btn_icon['value'])) {
										echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="16" height="16" fill="currentColor"><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352a144 144 0 1 0 0-288 144 144 0 1 0 0 288z"/></svg>';
									} else {
										echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="16" height="16" fill="currentColor"><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352a144 144 0 1 0 0-288 144 144 0 1 0 0 288z"/></svg>';
									}
								} else {
									echo esc_html($submit_btn_text);
								}
								?>
							</button>
						</span>

						<input type='search' id="searchInput" autocomplete="off" name="s"
							placeholder="<?php echo esc_attr( $placeholder ); ?>"
							data-post-type="<?php echo esc_attr( $search_post_type ); ?>"
							data-highlight="<?php echo esc_attr( $search_highlight ); ?>"
							data-ajax-search="<?php echo esc_attr( $is_ajax_search ); ?>">

						<!-- Ajax Search Loading Spinner -->
					<span class="spinner" aria-hidden="true">
						<svg class="frmx-spinner-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
							<circle cx="12" cy="12" r="10" stroke-opacity="0.2" />
							<path d="M12 2a10 10 0 0 1 10 10" />
						</svg>
					</span>

						<!-- WPML Language Code -->
						<?php if (defined('ICL_LANGUAGE_CODE')): ?>
							<input type="hidden" name="lang" value="<?php echo esc_attr(ICL_LANGUAGE_CODE); ?>" />
						<?php endif; ?>

						<?php if ( ! empty( $search_post_type ) ) : ?>
							<input type="hidden" id="hidden_post_type" name="post_type" value="<?php echo esc_attr( $search_post_type ); ?>" />
						<?php endif; ?>
					</div>
				</div>
				<div id="bbpc-search-result"></div>

				<?php if ($is_keywords): ?>
					<?php if ( $is_pro_active ): ?>
						<div class="bbpc-search-keyword">
							<div class="bbpc-keywords-wrapper" style="text-align: <?php echo esc_attr($keywords_align); ?>">
								<?php if (!empty($keywords_label)): ?>
									<span class="bbpc-search-keywords-label"><?php echo esc_html($keywords_label); ?></span>
								<?php endif; ?>
								<ul>
									<?php foreach ( $keywords as $keyword ) : ?>
										<?php
										$keyword_url = home_url( '/?s=' . $keyword['title'] );
										if ( ! empty( $search_post_type ) ) {
											$keyword_url .= '&post_type=' . $search_post_type;
										}
										?>
										<li><a href="<?php echo esc_url( $keyword_url ); ?>">
												<?php echo esc_html( $keyword['title'] ); ?> </a></li>
									<?php endforeach; ?>
								</ul>
							</div>
						</div>
					<?php else: ?>
						<?php
						// Show only in Editor (via ServerSideRender or admin page)
						if ((defined('REST_REQUEST') && REST_REQUEST) || is_admin()):
							$upsell_img = FORUMAX_FRONT_ASS . 'img/upsell-search-keywords.png';
							$upgrade_url = admin_url('admin.php?page=forumax-pricing');
							?>
							<div class="bbpc-search-keyword-upsell">
								<img src="<?php echo esc_url($upsell_img); ?>" alt="<?php esc_attr_e('Upgrade to Pro', 'forumax'); ?>">
								<div class="upsell-btn-wrapper bbpc-upgrade-btn">
									<a href="<?php echo esc_url($upgrade_url); ?>" target="_blank" rel="noopener noreferrer">
										<?php esc_html_e('Upgrade to Pro', 'forumax'); ?>
									</a>
								</div>
							</div>
						<?php endif; ?>
					<?php endif; ?>
				<?php endif; ?>
			</form>
		</div>

		<style>
			<?php if (!empty($attributes['color_text'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc_search_form_wrapper .input-wrapper input {
					color:
						<?php echo esc_attr($attributes['color_text']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['color_placeholder'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc_search_form_wrapper .input-wrapper input::placeholder {
					color:
						<?php echo esc_attr($attributes['color_placeholder']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['input_bg_color'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc_search_form_wrapper .input-wrapper input {
					background-color:
						<?php echo esc_attr($attributes['input_bg_color']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['color_icon'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc_search_form_wrapper .input-wrapper button {
					color:
						<?php echo esc_attr($attributes['color_icon']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['search_bg'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc_search_form_wrapper .input-wrapper button {
					background:
						<?php echo esc_attr($attributes['search_bg']); ?>
					;
				}

			<?php endif; ?>

			/* Keywords Styles */
			<?php if (!empty($attributes['color_keywords_label'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc-search-keyword span.bbpc-search-keywords-label {
					color:
						<?php echo esc_attr($attributes['color_keywords_label']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['bbpc_color_keywords'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc-search-keyword ul li a {
					color:
						<?php echo esc_attr($attributes['bbpc_color_keywords']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['bbpc_color_keywords_bg'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc-search-keyword ul li a {
					background:
						<?php echo esc_attr($attributes['bbpc_color_keywords_bg']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['bbpc_color_keywords_hover'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc-search-keyword ul li a:hover {
					color:
						<?php echo esc_attr($attributes['bbpc_color_keywords_hover']); ?>
					;
				}

			<?php endif; ?>
			<?php if (!empty($attributes['bbpc_color_keywords_bg_hover'])): ?>
				#<?php echo esc_attr($unique_id); ?> .bbpc-search-keyword ul li a:hover {
					background:
						<?php echo esc_attr($attributes['bbpc_color_keywords_bg_hover']); ?>
					;
				}

			<?php endif; ?>

			/* Input Border & Border Radius */
			<?php if ( isset( $attributes['input_border_radius'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper input {
					border-radius:
						<?php echo esc_attr( $attributes['input_border_radius'] ); ?>px
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['input_border_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper input {
					border-color:
						<?php echo esc_attr( $attributes['input_border_color'] ); ?>
					;
					border-style: solid;
				}
			<?php endif; ?>

			<?php if ( isset( $attributes['input_border_width'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper input {
					border-width:
						<?php echo esc_attr( $attributes['input_border_width'] ); ?>px
					;
					border-style: solid;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['input_focus_border_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper input:focus {
					border-color:
						<?php echo esc_attr( $attributes['input_focus_border_color'] ); ?>
					;
					outline: none;
				}
			<?php endif; ?>

			<?php if ( isset( $attributes['input_height'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper input {
					height:
						<?php echo esc_attr( $attributes['input_height'] ); ?>px
					;
				}
			<?php endif; ?>

			/* Wrapper Max Width */
			<?php if ( isset( $attributes['wrapper_max_width'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper {
					max-width:
						<?php echo esc_attr( $attributes['wrapper_max_width'] ); ?>px
					;
					margin-left: auto;
					margin-right: auto;
				}
			<?php endif; ?>

			/* Button Hover Styles */
			<?php if ( ! empty( $attributes['btn_hover_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper button:hover {
					color:
						<?php echo esc_attr( $attributes['btn_hover_color'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['btn_hover_bg'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper button:hover {
					background:
						<?php echo esc_attr( $attributes['btn_hover_bg'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( isset( $attributes['btn_border_radius'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .bbpc_search_form_wrapper .input-wrapper button {
					border-radius:
						<?php echo esc_attr( $attributes['btn_border_radius'] ); ?>px
					;
				}
			<?php endif; ?>

			/* Search Results Dropdown */
			<?php if ( ! empty( $attributes['results_bg_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> #bbpc-search-result {
					background:
						<?php echo esc_attr( $attributes['results_bg_color'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['results_border_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> #bbpc-search-result {
					border-color:
						<?php echo esc_attr( $attributes['results_border_color'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['results_text_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> #bbpc-search-result,
				#<?php echo esc_attr( $unique_id ); ?> #bbpc-search-result a,
				#<?php echo esc_attr( $unique_id ); ?> .search-result-item a {
					color:
						<?php echo esc_attr( $attributes['results_text_color'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['results_hover_bg'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .search-result-item:hover {
					background:
						<?php echo esc_attr( $attributes['results_hover_bg'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( isset( $attributes['results_border_radius'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> #bbpc-search-result {
					border-radius:
						<?php echo esc_attr( $attributes['results_border_radius'] ); ?>px
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['highlight_bg_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .frmx-search-highlight {
					background-color:
						<?php echo esc_attr( $attributes['highlight_bg_color'] ); ?>
					;
				}
			<?php endif; ?>

			<?php if ( ! empty( $attributes['highlight_text_color'] ) ) : ?>
				#<?php echo esc_attr( $unique_id ); ?> .frmx-search-highlight {
					color:
						<?php echo esc_attr( $attributes['highlight_text_color'] ); ?>
					;
				}
			<?php endif; ?>
		</style>
		<?php
		return ob_get_clean();
	}
}