<?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_Core_Text_Style;
use Sifency\Traits\Sifency_Render_Meta_Trait;

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

	public function get_name() {
		return 'sifency-career-job-deadline';
	}

	public function get_title() {
		return esc_html__( 'Career Job Deadline', 'sifency-addons' );
	}

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

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

	public function get_keywords() {
		return [ 'career', 'job', 'deadline', 'date', '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(
			'date_format',
			[
				'label'   => esc_html__( 'Date Format', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'F j, Y' => date( 'F j, Y' ), // e.g., November 6, 2023
					'Y-m-d'  => date( 'Y-m-d' ),  // e.g., 2023-11-06
					'm/d/Y'  => date( 'm/d/Y' ),  // e.g., 11/06/2023
					'd/m/Y'  => date( 'd/m/Y' ),  // e.g., 06/11/2023
					'D, M j' => date( 'D, M j' ), // e.g., Mon, Nov 6
					'custom' => esc_html__( 'Custom', 'sifency-addons' ),
				],
				'default' => 'F j, Y',
			]
		);

		$this->add_control(
			'date_format_custom',
			[
				'label'       => esc_html__( 'Custom Date Format', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'F j, Y',
				'description' => sprintf(
					'<a href="%s" target="_blank">%s</a>',
					'https://wordpress.org/support/article/formatting-date-and-time/',
					esc_html__( 'Documentation on date and time formatting.', 'sifency-addons' )
				),
				'condition'   => [
					'date_format' => 'custom',
				],
			]
		);

		$this->add_control(
			'placeholder_text',
			[
				'label'       => esc_html__( 'Placeholder', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => date( 'F j, Y' ),
				'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();

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

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

		$date_string = sifency_get_field( 'career_deadline' );
		$display_text = '';

		if ( ! empty( $date_string ) ) {
			$format = ( 'custom' === $settings['date_format'] ) ? $settings['date_format_custom'] : $settings['date_format'];
			$display_text = date_i18n( $format, strtotime( $date_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
	}
}