<?php
/**
 * Oxygen Builder Integration for Smart Post Show Pro.
 *
 * @link       https://shapedplugin.com/
 *
 * @package    Smart_Post_Show_Pro
 * @author     ShapedPlugin <support@shapedplugin.com>
 */

use SmartPostShow\Blocks\Helper;

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

/**
 * Smart Post Oxygen Element Class.
 *
 * Integrates Smart Post Show Pro with Oxygen page builder.
 *
 * @since 4.0.1
 */
class SmartPostOxygenElement extends OxyEl {

	/**
	 * Get the element name.
	 *
	 * @since 4.0.1
	 * @return string Element name.
	 */
	public function name() {
		return __( 'Saved Templates', 'smart-post-show-pro' );
	}

	/**
	 * Get the element slug.
	 *
	 * @since 4.0.1
	 * @return string Element slug.
	 */
	public function slug() {
		return 'smart-post-templates';
	}

	/**
	 * Get the element icon URL.
	 *
	 * @since 4.0.1
	 * @return string Icon URL.
	 */
	public function icon() {
		return SP_PCP_URL . 'admin/page-builder/oxygen/icon.svg';
	}

	/**
	 * Get the button priority.
	 *
	 * @since 4.0.1
	 * @return int Button priority.
	 */
	public function button_priority() {
		return 9;
	}

	/**
	 * Get the element HTML tag.
	 *
	 * @since 4.0.1
	 * @return string HTML tag name.
	 */
	public function tag() {
		return 'div';
	}

	/**
	 * Enable full CSS for the element.
	 *
	 * @since 4.0.1
	 * @return bool True to enable full CSS.
	 */
	public function enableFullCSS() {
		return true;
	}

	/**
	 * Callback after element initialization.
	 *
	 * Enqueues scripts for the page builder.
	 *
	 * @since 4.0.1
	 * @return void
	 */
	public function afterInit() {
		// Enqueue scripts for page builder.
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_page_builder_scripts' ) );
	}

	/**
	 * Enqueue page builder scripts.
	 *
	 * Registers and enqueues JavaScript files for the page builder functionality.
	 *
	 * @since 4.0.1
	 * @return void
	 */
	public function enqueue_page_builder_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 builder updates.
		wp_add_inline_script(
			'sp_smart_post_builder_script',
			'jQuery(document).ready(function($) {
				// Re-initialize when Oxygen builder updates elements
				$(document).on("oxygen-ajax-element-loaded", function() {
					if (typeof window.init === "function") {
						window.init();
					}
				});
			});'
		);
	}

	/**
	 * Render the element output.
	 *
	 * Displays the Smart Post template content with optional CSS.
	 *
	 * @since 4.0.1
	 * @param array  $options Element options.
	 * @param array  $defaults Default values.
	 * @param string $content Inner content.
	 *
	 * @return void
	 */
	public function render( $options, $defaults, $content ) {
		$template_id = $options['template_id'] ?? '';

		if ( $template_id ) {
			if ( isset( $_GET['action'] ) && strpos( sanitize_key( $_GET['action'] ), 'oxy_render_oxy' ) !== false ) {

				$upload_dir = wp_upload_dir();
				$css_file   = $upload_dir['basedir'] . '/smart-post-show/static/sp-post-' . $template_id . '.css';
				$css_url    = $upload_dir['baseurl'] . '/smart-post-show/static/sp-post-' . $template_id . '.css';

				// Enqueue CSS file if exists.
				if ( file_exists( $css_file ) ) {
					echo '<link rel="stylesheet" href="' . esc_url( $css_url ) . '?v=' . filemtime( $css_file ) . '">';
				}

				$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true );
				if ( ! empty( $dynamic_css ) ) {
					echo '<style>' . $dynamic_css . '</style>';
				}
			}
			echo '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">';
			echo do_shortcode( '[smart_post id="' . $template_id . '"]' );
			echo '</div>';

		} elseif ( isset( $_GET['action'] ) && strpos( sanitize_key( $_GET['action'] ), 'oxy_render_oxy' ) !== false ) {
			// @codingStandardsIgnoreLine
			echo '<div style="
				text-align: center;
				padding: 20px;
				border: 2px dashed #ccc;
				color: #999;
				font-size: 14px;
			">
				Please select a Smart Post Saved Templates
			</div>';
		}
	}

	/**
	 * Render element controls in Oxygen builder.
	 *
	 * Adds template selection dropdown control.
	 *
	 * @since 4.0.1
	 * @return void
	 */
	public function controls() {
		$this->addOptionControl(
			array(
				'type'    => 'dropdown',
				'name'    => esc_html__( 'Select Your Template', 'smart-post-show-pro' ),
				'slug'    => 'template_id',
				'default' => '0',
			)
		)->setValue( Helper::get_save_template_list() )->rebuildElementOnChange();
	}
}

new SmartPostOxygenElement();
