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

use Elementor\Widget_Base;
use Elementor\Controls_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_Name 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-event-organizer-name';
	}

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

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

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

	public function get_keywords() {
		return [ 'event', 'date', 'start', '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'     => 'WordPress Event Organizer Name',
				'description' => esc_html__( 'This text will be shown in the editor when the dynamic field is empty.', 'sifency-addons' ),
				'separator'   => 'before',
			]
		);
        
		$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}};',
				],
			]
		);

		$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 Name',
            'selector' => '.sifency-meta-style',
			'target_children' => false,
        ]);
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		$organizer_name = sifency_get_field( 'event_organizer_name' );
		$display_text = '';

		if ( ! empty( $organizer_name ) ) {
			$display_text = $organizer_name;
		} 

		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( [] );


		?>
		<?php $this->render_sifency_meta_item( $display_text, $animation_attrs ); ?>
		<?php
	}
}