<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_TB_Separator_Content_Controls;
use Sifency\Traits\Render_Sifency_Addons_TB_Meta;
use Sifency\Traits\Sifency_Addons_Trio_Text_Style_Controls;
class Sifency_Widget_Event_Organizer_Email extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_TB_Separator_Content_Controls;
	use Render_Sifency_Addons_TB_Meta;
	use Sifency_Addons_Trio_Text_Style_Controls;

	protected function get_allowed_template_types() {
		return 'event';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-organizer-email',
			'title'         => 'Event Organizer Email',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-event-components' ),
			'keywords'      => array( 'event', 'organizer', 'email', 'meta', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section( 'section_content', array( 'label' => esc_html__( 'Content Settings', 'sifency-addons' ) ) );
		$this->add_control(
			'placeholder_text',
			array(
				'label'       => esc_html__( 'Placeholder', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'organizer@example.com',
				'description' => esc_html__( 'This text will be shown in the editor when the dynamic field is empty.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'show_icon',
			array(
				'label'        => esc_html__( 'Show Icon', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			)
		);
		$this->add_control(
			'selected_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-envelope',
					'library' => 'fa-solid',
				),
				'condition' => array(
					'show_icon' => 'yes',
				),
			)
		);

		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => 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',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .sifency-meta-item' => 'justify-content: {{VALUE}};',
				),
				'separator' => 'before',
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_icon_style',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_icon' => 'yes' ),
			)
		);
		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-meta-style i' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_responsive_control(
			'icon_size',
			array(
				'label'     => esc_html__( 'Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 6,
						'max' => 100,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-meta-style i' => 'font-size: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'icon_spacing',
			array(
				'label'     => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-meta-style .email-icon' => 'margin-right: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_separator_content_controls();
		$this->add_sifency_addons_trio_text_style_controls();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$email        = sifency_get_field( 'event_organizer_email' );
		$display_text = '';

		if ( ! empty( $email ) ) {
			$display_text = $email;
		} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_text = $settings['placeholder_text'];
		}

		if ( empty( $display_text ) ) {
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$safe_email = antispambot( $display_text );

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-meta-item" <?php echo $animation_attrs; // phpcs:ignore ?>>
				<?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
					<span class="sifency-label-style"><?php echo esc_html( $settings['label_text'] ); ?></span>
				<?php endif; ?>

				<?php if ( 'yes' === $settings['show_separator'] && ! empty( $settings['separator_text'] ) ) : ?>
					<span class="sifency-separator-style"><?php echo esc_html( $settings['separator_text'] ); ?></span>
				<?php endif; ?>

				<span class="sifency-meta-style">
					<a href="mailto:<?php echo esc_attr( $safe_email ); ?>">
						<?php if ( 'yes' === $settings['show_icon'] && ! empty( $settings['selected_icon']['value'] ) ) : ?>
							<span class="email-icon">
								<?php Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>
						<?php endif; ?>
						<?php echo esc_html( $safe_email ); ?>
					</a>
				</span>
		</div>
		<?php
	}
}
