<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Plugin;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
/** Widget reveal frame*/
class Sifency_Addons_Reveal_Frame extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Flex_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-reveal-frame',
			'title'          => 'Sifency Reveal Frame',
			'icon'           => 'eicon-expand',
			'category'       => array( 'sifency-addons-widgets' ),
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-reveal-frame-script' ),
			'style_depends'  => array( 'sifency-sun-style' ),
		);
	}

	protected function register_controls() {

		// ─────────────────────────────────────────────
		// SECTION 1 — Base Content (Top Frame)
		// ─────────────────────────────────────────────
		$this->start_controls_section( 'section_base', array( 'label' => '1. Base Content (Top)' ) );

		$this->add_control(
			'base_frame_enabled',
			array(
				'label'        => esc_html__( 'Enable Base Frame', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Off', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'base_source',
			array(
				'label'     => esc_html__( 'Source', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'template' => esc_html__( 'Template', 'sifency-addons' ),
					'editor'   => esc_html__( 'Editor', 'sifency-addons' ),
				),
				'default'   => 'editor',
				'condition' => array( 'base_frame_enabled' => 'yes' ),
			)
		);

		$this->add_control(
			'base_template',
			array(
				'label'     => esc_html__( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT2,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array(
					'base_frame_enabled' => 'yes',
					'base_source'        => 'template',
				),
			)
		);

		$this->add_control(
			'base_editor',
			array(
				'label'     => esc_html__( 'Content', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => '<h1 style="color:white; text-align:center;">Initial Section</h1>',
				'condition' => array(
					'base_frame_enabled' => 'yes',
					'base_source'        => 'editor',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'label'    => esc_html__( 'Base Flexbox', 'sifency-addons' ),
				'selector' => '.sa-reveal-frame-base-layer',
			)
		);
				$this->add_responsive_control(
					'base_frame_width',
					array(
						'label'      => esc_html__( 'Width', 'sifency-addons' ),
						'type'       => Controls_Manager::SLIDER,
						'size_units' => array( 'px', '%', 'em', 'rem', 'vw', 'custom' ),
						'range'      => array(
							'px' => array(
								'min' => 0,
								'max' => 1000,
							),
							'%'  => array(
								'min' => 0,
								'max' => 100,
							),
						),
						'default'    => array(
							'unit' => '%',
							'size' => 100,
						),
						'selectors'  => array(
							'{{WRAPPER}} .sa-reveal-frame-reveal-layer' => 'width: {{SIZE}}{{UNIT}};',
						),
					)
				);

		$this->add_responsive_control(
			'base_frame_height',
			array(
				'label'      => esc_html__( 'Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em', 'rem', 'vh', 'custom' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 1000,
					),
					'%'  => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sa-reveal-frame-reveal-layer' => 'min-height: {{SIZE}}{{UNIT}};', // Using min-height based on your CSS
				),
			)
		);
		$this->end_controls_section();

		// ─────────────────────────────────────────────
		// SECTION 2 — Circle (Portal Shape)
		// ─────────────────────────────────────────────
		$this->start_controls_section( 'section_circle', array( 'label' => '2. Circle (Portal)' ) );

		$this->add_control(
			'circle_frame_enabled',
			array(
				'label'        => esc_html__( 'Enable Circle Frame', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Off', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'circle_color',
			array(
				'label'     => esc_html__( 'Circle Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => array( '{{WRAPPER}} .sa-reveal-frame-circle' => 'background-color: {{VALUE}};' ),
				'condition' => array( 'circle_frame_enabled' => 'yes' ),
			)
		);

		$this->end_controls_section();

		// ─────────────────────────────────────────────
		// SECTION 3 — Revealed Content (Bottom Frame)
		// ─────────────────────────────────────────────
		$this->start_controls_section( 'section_reveal', array( 'label' => '3. Reveal Content (Bottom)' ) );

		$this->add_control(
			'reveal_frame_enabled',
			array(
				'label'        => esc_html__( 'Enable Reveal Frame', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Off', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'reveal_source',
			array(
				'label'     => esc_html__( 'Source', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'template' => esc_html__( 'Template', 'sifency-addons' ),
					'editor'   => esc_html__( 'Editor', 'sifency-addons' ),
				),
				'default'   => 'editor',
				'condition' => array( 'reveal_frame_enabled' => 'yes' ),
			)
		);

		$this->add_control(
			'reveal_template',
			array(
				'label'     => esc_html__( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT2,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array(
					'reveal_frame_enabled' => 'yes',
					'reveal_source'        => 'template',
				),
			)
		);

		$this->add_control(
			'reveal_editor',
			array(
				'label'     => esc_html__( 'Content', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => '<h2 style="color:black; text-align:center;">Revealed Section</h2>',
				'condition' => array(
					'reveal_frame_enabled' => 'yes',
					'reveal_source'        => 'editor',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'reveal',
				'label'    => esc_html__( 'Revealed Flexbox', 'sifency-addons' ),
				'selector' => '.sa-reveal-frame-reveal-layer',
			)
		);
				$this->add_sifency_addons_flex_alignment_controls(
					array(
						'prefix'   => 'inner',
						'label'    => esc_html__( 'Inner Flexbox', 'sifency-addons' ),
						'selector' => '.sa-reveal-frame-reveal-inner',
					)
				);
						$this->add_responsive_control(
							'inner_frame_width',
							array(
								'label'      => esc_html__( 'Width', 'sifency-addons' ),
								'type'       => Controls_Manager::SLIDER,
								'size_units' => array( 'px', '%', 'em', 'rem', 'vw', 'custom' ),
								'range'      => array(
									'px' => array(
										'min' => 0,
										'max' => 1000,
									),
									'%'  => array(
										'min' => 0,
										'max' => 100,
									),
								),
								'default'    => array(
									'unit' => '%',
									'size' => 100,
								),
								'selectors'  => array(
									'{{WRAPPER}} .sa-reveal-frame-reveal-inner' => 'width: {{SIZE}}{{UNIT}};',
								),
							)
						);

		$this->add_responsive_control(
			'inner_frame_height',
			array(
				'label'      => esc_html__( 'Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em', 'rem', 'vh', 'custom' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 1000,
					),
					'%'  => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sa-reveal-frame-reveal-inner' => 'min-height: {{SIZE}}{{UNIT}};', // Using min-height based on your CSS
				),
			)
		);
		$this->end_controls_section();
	}

	/**
	 * Safely render an Elementor template by ID.
	 * Returns empty string — never triggers a fatal or a PHP notice —
	 * when the ID is empty or the template does not exist.
	 *
	 * @param  string|int $template_id  The Elementor template post ID.
	 * @return string                   Rendered HTML or empty string.
	 */
	private function sifency_render_template( $template_id ) {
		$template_id = absint( $template_id );

		if ( empty( $template_id ) ) {
			return '';
		}

		// Verify the post exists and is published.
		$post = get_post( $template_id );
		if ( ! $post || 'publish' !== $post->post_status ) {
			return '';
		}

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builder output
		return \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id );
	}

	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$base_enabled   = 'yes' === $settings['base_frame_enabled'];
		$circle_enabled = 'yes' === $settings['circle_frame_enabled'];
		$reveal_enabled = 'yes' === $settings['reveal_frame_enabled'];
		?>
		<div class="sa-reveal-frame-main" id="<?php echo esc_attr( $wrapper_id ); ?>">

			<?php if ( $base_enabled ) : ?>
			<!-- Frame 1: Base content layer -->
			<div class="sa-reveal-frame-base-layer">
				<?php
				if ( 'template' === $settings['base_source'] ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo $this->sifency_render_template( $settings['base_template'] );
				} else {
					echo wp_kses_post( $settings['base_editor'] );
				}
				?>
			</div>
			<?php endif; ?>

			<?php if ( $circle_enabled ) : ?>
			<!-- Frame 2: Portal circle -->
			<div class="sa-reveal-frame-circle-wrapper">
				<div class="sa-reveal-frame-circle"></div>
			</div>
			<?php endif; ?>

			<?php if ( $reveal_enabled ) : ?>
			<!-- Frame 3: Revealed content layer -->
			<div class="sa-reveal-frame-reveal-layer<?php echo $circle_enabled ? '' : ' sa-reveal-no-clip'; ?>">
				<div class="sa-reveal-frame-reveal-inner">
					<?php
					if ( 'template' === $settings['reveal_source'] ) {
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						echo $this->sifency_render_template( $settings['reveal_template'] );
					} else {
						echo wp_kses_post( $settings['reveal_editor'] );
					}
					?>
				</div>
			</div>
			<?php endif; ?>

		</div>
		<?php
	}
}
