<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  
use Sifency\Traits\Sifency_Label_Content_Controls_Trait;
use Sifency\Traits\Sifency_Separator_Content_Controls_Trait;
use Sifency\Traits\Sifency_Render_Meta_Trait;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Event_Organizer_Email extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Label_Content_Controls_Trait;
	use Sifency_Separator_Content_Controls_Trait;
	use Sifency_Render_Meta_Trait;
	use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency-organizer-email';
	}

	public function get_title() {
		return esc_html__( 'Event Organizer Email', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-meta-data';
	}

	public function get_categories() {
		return [ 'sifency-event-components' ];
	}

	public function get_keywords() {
		return [ 'event', 'organizer', 'email', 'meta', 'dynamic', 'sifency' ];
	}

	public function get_style_depends() {
	    return [ 'sifency-typo-style' ];
    }
	
	protected function register_controls() {
		// --- Content Tab: Main Settings ---
		$this->start_controls_section(
			'section_content',
			[
				'label' => esc_html__( 'Content Settings', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'placeholder_text',
			[
				'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',
			[
				'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',
			[
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => [
					'value'   => 'fas fa-envelope',
					'library' => 'fa-solid',
				],
				'condition' => [
					'show_icon' => 'yes',
				],
			]
		);
        
		$this->add_responsive_control(
			'align',
			[
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'flex-start' => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
					'center'     => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-text-align-center' ],
					'flex-end'   => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
				],
				'default'   => 'flex-start',
				'selectors' => [
					'{{WRAPPER}} .sifency-meta-item' => 'justify-content: {{VALUE}};',
				],
				'separator' => 'before',
			]
		);

		$this->end_controls_section();

				// Add icon styling controls
		$this->start_controls_section(
			'section_icon_style',
			[
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_icon' => 'yes' ],
			]
		);
		$this->add_control(
			'icon_color',
			[
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-meta-style i' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_responsive_control(
			'icon_size',
			[
				'label'      => esc_html__( 'Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 6, 'max' => 100 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-meta-style i' => 'font-size: {{SIZE}}{{UNIT}};' ],
			]
		);
		$this->add_responsive_control(
			'icon_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-meta-style .email-icon' => 'margin-right: {{SIZE}}{{UNIT}};' ],
			]
		);
		$this->end_controls_section();


		// Call the methods from our reusable traits
		$this->add_sifency_label_content_controls();
		$this->add_sifency_separator_content_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Label Style',
            'selector' => '.sifency-label-style',
			'target_children' => false, 
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'sep_style',
            'label'    => 'Separator Style',
            'selector' => '.sifency-separator-style',
			'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Event Dynamic Organizer Email',
            'selector' => '.sifency-meta-style',
			'target_children' => false,
        ]);
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$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_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

		$safe_email = antispambot( $display_text );
		
		?>
		<div class="sifency-meta-item" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<div class="sifency-meta-item-inner">
				<?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'], [ 'aria-hidden' => 'true' ] ); ?>
							</span>
						<?php endif; ?>
						<?php echo esc_html( $safe_email ); ?>
					</a>
				</span>
			</div>
		</div>
		<?php
	}
}