<?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_Job_Responsibilities 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-job-responsibilities';
	}

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

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

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

	public function get_keywords() {
		return [ 'job', 'responsibilities', 'career', 'meta', 'dynamic', 'sifency' ];
	}

	public function get_style_depends() {
	    return [ 'sifency-typo-style' ];
    }
	
	protected function register_controls() {
		$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::TEXTAREA,
				'default'     => '<ul><li>Health Insurance</li><li>Paid Time Off</li><li>401(k) Matching</li></ul>',
				'description' => esc_html__( 'This content will be shown in the editor when the dynamic field is empty. HTML is allowed.', 'sifency-addons' ),
				'separator'   => 'before',
			]
		);
        
		$this->add_responsive_control(
			'align',
			[
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
					'center' => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-text-align-center' ],
					'right'  => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
				],
				'default'   => 'left',
				'selectors' => [
					'{{WRAPPER}} .sifency-meta-style' => 'text-align: {{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 Responsibility',
            'selector' => '.sifency-meta-style',
			'target_children' => true,
        ]);
		$this->add_sifency_animation_controls();
	}

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

		$job_responsibilities_raw = sifency_get_field( 'career_responsibilities' );
		$display_text = '';

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

		if ( empty( $display_text ) ) {
			return;
		}
		
		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

		?>
		<div class="sifency-meta-item" <?php echo $animation_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
			<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; ?>

				<div class="sifency-meta-style">
					<?php
					echo wp_kses_post( $display_text );
					?>
				</div>
			</div>
		</div>
		<?php
	}
}