<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
class Sifency_Widget_Modal extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-widget-modal',
			'title'          => 'Sifency Modal',
			'icon'           => 'eicon-lightbox',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-modal-script' ),
			'style_depends'  => array( 'sifency-modal-style' ),
		);}

	protected function register_controls() {

		// --- Trigger Button Section ---
		$this->start_controls_section(
			'section_trigger',
			array( 'label' => esc_html__( 'Trigger Button', 'sifency-addons' ) )
		);
		$this->add_control(
			'button_text',
			array(
				'label'   => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Open Popup',
			)
		);
		$this->add_control(
			'button_icon',
			array(
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}}' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		// --- Modal Content Section ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Modal Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'content_source',
			array(
				'label'   => esc_html__( 'Content Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'custom',
				'options' => array(
					'custom'   => 'Custom Content',
					'template' => 'Saved Template',
				),
			)
		);
		$this->add_control(
			'custom_content',
			array(
				'label'     => esc_html__( 'Content', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => '<p>This is your modal content. You can add text, images, or shortcodes here.</p>',
				'condition' => array( 'content_source' => 'custom' ),
			)
		);
		$this->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'default'   => '0',
				'condition' => array( 'content_source' => 'template' ),
			)
		);
		$this->end_controls_section();

		// --- Modal Settings Section ---
		$this->start_controls_section(
			'section_settings',
			array( 'label' => esc_html__( 'Modal Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'entrance_animation',
			array(
				'label'   => esc_html__( 'Animation', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'fade',
				'options' => array(
					'fade'        => esc_html__( 'Fade In', 'sifency-addons' ),
					'zoom'        => esc_html__( 'Zoom In', 'sifency-addons' ),
					'slide_up'    => esc_html__( 'Slide Up', 'sifency-addons' ),
					'slide_down'  => esc_html__( 'Slide Down', 'sifency-addons' ),
					'slide_left'  => esc_html__( 'Slide from Right', 'sifency-addons' ),
					'slide_right' => esc_html__( 'Slide from Left', 'sifency-addons' ),
					'flip_x'      => esc_html__( 'Flip X (Vertical)', 'sifency-addons' ),
					'flip_y'      => esc_html__( 'Flip Y (Horizontal)', 'sifency-addons' ),
					'bounce'      => esc_html__( 'Bounce / Elastic', 'sifency-addons' ),
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'   => 'trigger_button_',
				'label'    => esc_html__( 'Trigger Button', 'sifency-addons' ),
				'selector' => '.sifency-modal-trigger-btn',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_sifency_addons_custom_css_controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id   = esc_attr( $context['dom_id'] );
		$settings     = $this->get_settings_for_display();
		$content_html = '';
		if ( 'template' === $settings['content_source'] && ! empty( $settings['template_id'] ) ) {
			$content_html = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $settings['template_id'] );
		} else {
			$content_html = $settings['custom_content'];
		}

		$anim_class = 'sifency-anim-' . $settings['entrance_animation'];
		$this->sifency_addons_render_custom_css();

		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes( array( 'sifency-modal-widget-wrapper' ) );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $animation_wrapper_attrs ); ?>>
			
			<div class="sifency-modal-trigger-btn sifency-addons-primary-button">
				<?php if ( ! empty( $settings['button_icon']['value'] ) ) : ?>
					<span class="sifency-btn-icon">
						<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</span>
				<?php endif; ?>
				<?php if ( ! empty( trim( $settings['button_text'] ) ) ) : ?>
					<span class="sifency-btn-text">
						<?php echo esc_html( $settings['button_text'] ); ?>
					</span>
				<?php endif; ?>
			</div>

			<div class="sifency-modal-overlay-wrapper <?php echo esc_attr( $anim_class ); ?>" aria-hidden="true">
				<div class="sifency-modal-backdrop"></div>
				<div class="sifency-modal-container">
					<button class="sifency-modal-close" aria-label="Close">
						<i class="eicon-close"></i>
					</button>
					<div class="sifency-modal-body">
                        <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-rendered templates or WYSIWYG content
						echo $content_html;
						?>
					</div>
				</div>
			</div>

		</div>
		<?php
	}
}
