<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Eltemplate extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'el-template',
			'title'          => 'Sifency Template',
			'icon'           => 'eicon-document-file',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'template', 'elementor', 'section', 'layout' ),
			'style_depends'  => array( 'sifency-frontend-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array( 'label' => __( 'Template', 'sifency-addons' ) )
		);
		$this->add_control(
			'template_id',
			array(
				'label'       => __( 'Choose Template', 'sifency-addons' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'options'     => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'description' => __( 'A list of your saved Elementor templates.', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render() {
		$settings          = $this->get_settings_for_display();
		$template_id       = $settings['template_id'];
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		if ( empty( $template_id ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				?>
				<div class="sifency-template-placeholder">
					<?php esc_html_e( 'Please select an Elementor template from the panel.', 'sifency-addons' ); ?>
				</div>
				<?php
			}
			return;
		}

		// WP 6.9.1 strictly validates style dependencies at registration time.
		// Elementor's per-post stylesheet (elementor-post-XXXX) declares
		// 'elementor-frontend' as a dependency. If that handle isn't registered
		// yet when get_builder_content_for_display() runs, WP throws a notice.
		// Registering it here (no-op if already registered) silences the notice.
		if ( ! wp_style_is( 'elementor-frontend', 'registered' ) ) {
			$elementor_frontend_url = ELEMENTOR_ASSETS_URL . 'css/frontend.min.css';
			wp_register_style(
				'elementor-frontend',
				$elementor_frontend_url,
				array(),
				ELEMENTOR_VERSION
			);
		}

		$wrapper_classes = array( 'sifency-el-template-container' );
		$wrapper_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id );
			?>
		</div>
		<?php
	}
}
