<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

use SmartPostShow\Blocks\Helper;

class Smart_Post_Bricks_Integration extends \Bricks\Element {
	// Element properties
	public $category = 'general';
	public $name     = 'smart-post-addon';
	public $icon     = 'sps-icon-elementor';

	// Return localised element label
	public function get_label() {
		return esc_html__( 'Saved Templates', 'smart-post-show-pro' );
	}

	/**
	 * Enqueue scripts for Bricks builder.
	 *
	 * @return void
	 */
	public function enqueue_scripts() {
		$scripts = array(
			'sp_smart_post_builder_script'      => 'blocks/assets/js/builder-scripts.js',
			'sp_smart_post_news_ticker_script'  => 'blocks/assets/js/news-ticker.min.js',
			'sp_video_and_gallery_slider'       => 'blocks/assets/js/video-and-gallery-slider.min.js',
			'sp_smart_post_smart_search_script' => 'blocks/assets/js/smart-search.min.js',
			'sp_smart_post_smart_toc_script'    => 'blocks/assets/js/table-of-content.min.js',
		);

		foreach ( $scripts as $handle => $path ) {
			wp_enqueue_script(
				$handle,
				SP_PCP_URL . $path,
				array( 'jquery' ),
				SMART_POST_SHOW_PRO_VERSION,
				true
			);
		}

		// Add inline script to reinitialize after Bricks builder updates.
		wp_add_inline_script(
			'sp_smart_post_builder_script',
			'jQuery(document).ready(function($) {
				// Re-initialize when Bricks builder updates elements
				$(document).on("bricksAjaxRender", function() {
					if (typeof window.init === "function") {
						window.init();
					}
				});
			});'
		);
	}

	/**
	 * Enqueue styles for Bricks builder.
	 *
	 * @return void
	 */
	public function enqueue_styles() {
		// Enqueue other required styles.
		wp_enqueue_style( 'pcp-font-awesome' );
		wp_enqueue_style( 'pcp_swiper' );
		wp_enqueue_style( 'pcp-bxslider' );
		wp_enqueue_style( 'pcp-likes' );
		wp_enqueue_style( 'pcp-popup' );
		wp_enqueue_style( 'pcp_fonttello_icon' );
		wp_enqueue_style( 'pcp-style' );
	}

	public function set_controls() {
		$this->controls['template_id'] = array(
			'type'        => 'select',
			'label'       => esc_html__( 'Select Template', 'smart-post-show-pro' ),
			'options'     => Helper::get_save_template_list(),
			'clearable'   => false,
			'default'     => '0',
			'pasteStyles' => true,
			'inline'      => true,
		);
		$this->controls['separator']   = array(
			'type' => 'separator',
		);
		$this->controls['help']        = array(
			'type'    => 'info',
			'content' => 'Pick a Template from your saved ones. Or create a template from: <strong><i>Dashboard > Smart Post > Saved Templates</i></strong>',
		);
	}

	/**
	 * Check if currently rendering in Bricks Builder.
	 *
	 * @return bool
	 */
	private function is_bricks_builder() {
		// Check if Bricks builder is active via URL parameter.
		return isset( $_GET['bricks'] );
	}

	/**
	 * Render the element output on the frontend.
	 *
	 * Called by Bricks Builder when rendering the element
	 * on the page — both in the editor and on the frontend.
	 *
	 * @return void
	 */
	public function render() {

		// Get the selected template ID from element settings.
		$template_id = isset( $this->settings['template_id'] ) ? (int) $this->settings['template_id'] : 0;

		// Show a placeholder if no template has been selected.
		if ( empty( $template_id ) ) {
			echo '<div style="
				text-align: center;
				padding: 20px;
				border: 2px dashed #ccc;
				color: #999;
				font-size: 14px;
			">' . esc_html__( 'Please select a Smart Post Saved Templates', 'smart-post-show-pro' ) . '</div>';
			return;
		}

		$is_bricks_builder = $this->is_bricks_builder();

		$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true );

		// Execute the Smart Post Show shortcode.
		$shortcode_output = do_shortcode( '[smart_post id="' . $template_id . '"]' );

		// Output the wrapping element tag (Bricks handles root attributes).
		echo '<div ' . $this->render_attributes( '_root' ) . '>';

		echo '<style>' . $dynamic_css . '</style>';
		echo '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">';
		echo $shortcode_output;
		echo '</div>';

		echo '</div>';
	}
}
