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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Career_Job_Apply extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() {
		return 'sifency-career-apply-button';
	}

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

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

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

	public function get_keywords() {
		return [ 'career', 'apply', 'button', 'link', 'sifency' ];
	}

	protected function register_controls() {
		
		$this->start_controls_section(
			'section_button_content',
			[
				'label' => esc_html__( 'Button', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'placeholder_text',
			[
				'label'       => esc_html__( 'Placeholder Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Apply Now', 'sifency-addons' ),
				'description' => esc_html__( 'This text is shown in the editor if the dynamic fields are empty.', 'sifency-addons' ),
			]
		);
		
		$this->add_control(
			'selected_icon',
			[
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'skin'      => 'inline',
				'label_block' => false,
			]
		);

		$this->add_control(
			'icon_align',
			[
				'label'   => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'left',
				'options' => [
					'left'  => esc_html__( 'Before', 'sifency-addons' ),
					'right' => esc_html__( 'After', 'sifency-addons' ),
				],
				'condition' => [ 'selected_icon[value]!' => '' ],
			]
		);

		$this->add_responsive_control(
			'icon_spacing',
			[
				'label'      => esc_html__( 'Icon Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [
					'{{WRAPPER}} .sifency-button-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-button-icon-left'  => 'margin-right: {{SIZE}}{{UNIT}};',
				],
				'condition' => [ 'selected_icon[value]!' => '' ],
			]
		);
		
		$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' ],
					'justify' => [ 'title' => esc_html__( 'Justified', 'sifency-addons' ), 'icon' => 'eicon-text-align-justify' ],
				],
				'default'   => '',
				'prefix_class' => 'elementor-align%s-',
			]
		);
		
		$this->end_controls_section();

		// --- Style Tab: Button Styling ---
		$this->start_controls_section(
			'section_button_style',
			[
				'label' => esc_html__( 'Button', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-button',
			]
		);
		$this->start_controls_tabs( 'button_tabs_style' );
		$this->start_controls_tab(
			'button_tab_normal',
			[
				'label' => esc_html__( 'Normal', 'sifency-addons' ),
			]
		);
		$this->add_control(
			'button_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-button' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'button_bg_color',
			[
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-button' => 'background-color: {{VALUE}};' ],
			]
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'button_tab_hover',
			[
				'label' => esc_html__( 'Hover', 'sifency-addons' ),
			]
		);
		$this->add_control(
			'button_hover_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-button:hover' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'button_hover_bg_color',
			[
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-button:hover' => 'background-color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'button_hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-button:hover' => 'border-color: {{VALUE}};' ],
			]
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'      => 'button_border',
				'selector'  => '{{WRAPPER}} .sifency-button',
				'separator' => 'before',
			]
		);
		$this->add_responsive_control(
			'button_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'selectors'  => [ '{{WRAPPER}} .sifency-button' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ],
			]
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'button_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-button',
			]
		);
		$this->add_responsive_control(
			'button_padding',
			[
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [ '{{WRAPPER}} .sifency-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
				'separator'  => 'before',
			]
		);
		$this->end_controls_section();
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$url  = sifency_get_field( 'career_apply_link' );
		$text = sifency_get_field( 'career_apply_button_text' );
		
		if ( ! empty( $url ) ) {
			$display_url  = $url;
			$display_text = ! empty( $text ) ? $text : esc_html__( 'Apply Now', 'sifency-addons' );
		}
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_url  = '#';
			$display_text = $settings['placeholder_text'];
		} else {
			return;
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		$this->add_render_attribute( 'button', 'class', 'sifency-button' );
		$this->add_render_attribute( 'button', 'href', esc_url( $display_url ) );
		$this->add_render_attribute( 'button', 'target', '_blank' ); // Apply links should usually open in a new tab
		$this->add_render_attribute( 'button', 'rel', 'noopener noreferrer' );

		?>
		<div <?php echo $animation_attrs; // phpcs:ignore ?>>
			<a <?php echo wp_kses ( $this->get_render_attribute_string( 'button' ), [] ); ?>>
				<?php if ( 'left' === $settings['icon_align'] && ! empty( $settings['selected_icon']['value'] ) ) : ?>
					<span class="sifency-button-icon-left"><?php Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] ); ?></span>
				<?php endif; ?>
				
				<span class="sifency-button-text"><?php echo esc_html( $display_text ); ?></span>

				<?php if ( 'right' === $settings['icon_align'] && ! empty( $settings['selected_icon']['value'] ) ) : ?>
					<span class="sifency-button-icon-right"><?php Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] ); ?></span>
				<?php endif; ?>
			</a>
		</div>
		<?php
	}
}