<?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_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
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;

class Sifency_Widget_Auto_Popup extends Sifency_Addons_Widget_Base {
	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-auto-popup',
			'title'          => 'Sifency Auto Popup',
			'icon'           => 'eicon-banner',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-popup-script' ),
			'style_depends'  => array( 'sifency-popup-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Popup 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' => 'Elementor Template',
				),
			)
		);
		$this->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Choose 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->add_control(
			'custom_content',
			array(
				'label'     => esc_html__( 'Custom Text/HTML', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => '<h2>Subscribe to our Newsletter</h2><p>Get the latest news and updates.</p>',
				'condition' => array( 'content_source' => 'custom' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_settings',
			array( 'label' => esc_html__( 'Popup Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'trigger_delay',
			array(
				'label'       => esc_html__( 'Open Delay (Seconds)', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 60,
				'step'        => 0.5,
				'default'     => 1,
				'description' => 'How long to wait after page load before showing the popup.',
			)
		);
		$this->add_control(
			'show_once',
			array(
				'label'        => esc_html__( 'Show Once Per Session', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => 'Yes',
				'label_off'    => 'No',
				'return_value' => 'yes',
				'default'      => 'no',
				'description'  => 'If Yes, the popup will not show again until the browser is closed and reopened (uses Session Storage).',
			)
		);
		$this->add_control(
			'prevent_scroll',
			array(
				'label'       => esc_html__( 'Prevent Page Scroll', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Stop background scrolling when popup is open.',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Popup Container Style', 'sifency-addons' ),
				'selector' => '.sifency-auto-popup-container',
			)
		);
		$this->start_controls_section(
			'section_style_overlay',
			array(
				'label' => esc_html__( 'Overlay', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'overlay_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => 'rgba(0,0,0,0.7)',
				'selectors' => array( '{{WRAPPER}} .sifency-auto-popup-overlay' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'label'    => esc_html__( 'Close Button', 'sifency-addons' ),
				'selector' => '.sifency-auto-popup-close',
				'exclude'  => array( 'transition' ),
			)
		);

		$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'];
		}

		$this->add_render_attribute(
			'wrapper',
			array(
				'class'               => 'sifency-auto-popup-wrapper',
				'data-delay'          => $settings['trigger_delay'] * 1000,
				'data-show-once'      => $settings['show_once'],
				'data-prevent-scroll' => $settings['prevent_scroll'],
				'data-widget-id'      => $this->get_id(),
			)
		);

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			echo '<div style="background:#eee; padding:20px; text-align:center; border:1px dashed #999;">';
			echo '<i class="eicon-banner" style="font-size:30px; display:block; margin-bottom:10px;"></i>';
			echo '<small>(To edit the content, switch the "Content Source" or "Choose Template")</small>';
			echo '</div>';
		}
		$this->sifency_addons_render_custom_css();
		?>

		<div id="<?php echo esc_attr( $wrapper_id ); ?>" 
		<?php
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor widget rendering attributes
		echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) );
		?>
		>
			<div class="sifency-auto-popup-overlay"></div>
			<div class="sifency-auto-popup-container">
				<button class="sifency-auto-popup-close"><i class="eicon-close"></i></button>
				<div class="sifency-auto-popup-content">
                    <?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-rendered template or WYSIWYG HTML content
					echo $content_html;
					?>
				</div>
			</div>
		</div>
		<?php
	}
}
