<?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_End_Time 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-end-time';
	}

	public function get_title() {
		return esc_html__( 'Event End Time', '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(
			'time_format',
			[
				'label'   => esc_html__( 'Time Format', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'g:i a'  => date( 'g:i a' ), // e.g., 9:30 am
					'g:i A'  => date( 'g:i A' ), // e.g., 9:30 AM
					'H:i'    => date( 'H:i' ),   // e.g., 09:30 (24-hour)
					'custom' => esc_html__( 'Custom', 'sifency-addons' ),
				],
				'default' => 'g:i a',
			]
		);

		$this->add_control(
			'time_format_custom',
			[
				'label'       => esc_html__( 'Custom Time Format', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'g:i a',
				'description' => sprintf(
					'<a href="%s" target="_blank">%s</a>',
					'https://wordpress.org/support/article/formatting-date-and-time/', // Correct URL
					esc_html__( 'Documentation on date and time formatting.', 'sifency-addons' )
				),
				'condition'   => [
					'time_format' => 'custom',
				],
			]
		);
		
		$this->add_control(
			'placeholder_text',
			[
				'label'       => esc_html__( 'Placeholder', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => date( 'g:i a' ),
				'description' => esc_html__( 'This text will be shown in the editor when the dynamic field is empty.', 'sifency-addons' ),
				'separator'   => 'before',
			]
		);
		$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 End Time',
            'selector' => '.sifency-meta-style',
			'target_children' => false,
        ]);
		$this->add_sifency_animation_controls();
	}

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

		$time_string = sifency_get_field( 'event_end_time' );
		$display_text = '';

		if ( ! empty( $time_string ) ) {
			// Use date_i18n() for time formatting as well
			$format = ( 'custom' === $settings['time_format'] ) ? $settings['time_format_custom'] : $settings['time_format'];
			$display_text = date_i18n( $format, strtotime( $time_string ) );
		} 
		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
	}
}