<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Parity_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Career_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Addons_Widget_Career extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Query_Arg_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Career_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-addons-career',
			'title'         => 'Sifency Career',
			'icon'          => 'eicon-posts-ticker',
			'category'      => 'sifency-advance-widgets',
			'keywords'      => array( 'career', 'jobs', 'posts', 'listing' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-career-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_career_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Career Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-career-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style_',
				'label'    => esc_html__( 'Career Heading', 'sifency-addons' ),
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style_',
				'label'          => 'Career Content',
				'selector'       => '.sifency-content-style',
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'bg', 'radius', 'tabs', 'transition', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'meta_style_',
				'label'          => 'Career Meta',
				'selector'       => '.sifency-meta-style',
				'hover_selector' => '.sifency-career-item:hover .sifency-meta-style',
				'exclude'        => array( 'stroke', 'shadow', 'tabs', 'transition', 'alignment' ),
				'color_only'     => true,
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector' => '.sifency-meta-style i, .sifency-meta-style svg',
				'selector' => '.sifency-career-item:hover .sifency-meta-style i, .sifency-career-item:hover .sifency-meta-style svg',
				'label'    => 'Meta Icon Style',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'show_dimensions' => true,
				'hover_selector'  => '.sifency-career-item:hover .sifency-button-style',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selectors' => array(
					'image'     => '.sifency-career-img img',
					'container' => '.sifency-career-img img',
				),
				'exclude'   => array( 'tabs', 'transition' ),
			)
		);
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array(
			'sifency-career-wrapper',
			'sifency-career-layout-' . $settings['layout_style'],
		);

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		$this->render_sifency_section_header_elements( $settings );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<div class="sifency-career-grid">
				<?php
				if ( 'posts' === $settings['source'] ) {
					$this->render_posts_content( $settings, $animation_manager );
				} else {
					$this->render_custom_content( $settings, $animation_manager );
				}
				?>
			</div>
		</div>
		<?php
	}

	private function render_single_item( $settings, $data, $link_key, $animation_manager, $index ) {
		$oi             = $index + 1;
		$parity_class   = 'odd-item';
		$parity_pattern = $settings['parity_pattern'] ?? 'standard';
		if ( 'every_third' === $parity_pattern ) {
			$parity_class = ( $oi % 3 === 0 ) ? 'even-item' : 'odd-item'; } elseif ( 'custom' === $parity_pattern ) {
			$even_positions = array_map( 'intval', explode( ',', $settings['custom_even_positions'] ?? '' ) );
			$parity_class   = in_array( $oi, $even_positions ) ? 'even-item' : 'odd-item'; } else {
				$parity_class = ( $oi % 2 !== 0 ) ? 'odd-item' : 'even-item'; }

			$item_classes = array( 'sifency-career-item', $parity_class );
			$item_attrs   = $animation_manager->get_item_attributes( $item_classes );
			?>
		<article <?php echo wp_kses_post( $item_attrs ); ?>>
			
			<?php if ( ! empty( $data['image_html'] ) ) : ?>
				<div class="sifency-career-img">
					<?php echo wp_kses_post( $data['image_html'] ); ?>
				</div>
			<?php endif; ?>

			<div class="sifency-career-content">
				<div class="sifency-career-item__header">
					<h3 class="sifency-career-item__title">
						<a <?php echo wp_kses( $this->get_render_attribute_string( $link_key ), array() ); ?> class="sifency-heading-style">
							<?php echo esc_html( $data['title'] ); ?>
						</a>
					</h3>
					
					<div class="sifency-career-item__meta">
						<?php if ( $data['location'] ) : ?>
							<span class="sifency-career-item__location sifency-meta-style">
								<?php Icons_Manager::render_icon( $settings['location_icon'], array( 'aria-hidden' => 'true' ) ); ?>
								<?php echo esc_html( $data['location'] ); ?>
							</span>
						<?php endif; ?>
						<?php if ( $data['salary'] ) : ?>
							<span class="sifency-career-item__location sifency-meta-style">
								<?php Icons_Manager::render_icon( $settings['salary_icon'], array( 'aria-hidden' => 'true' ) ); ?>
								<?php echo esc_html( $data['salary'] ); ?>
							</span>
						<?php endif; ?>
						
						<?php if ( $data['job_type'] ) : ?>
							<span class="sifency-career-item__type sifency-meta-style">
								<?php Icons_Manager::render_icon( $settings['type_icon'], array( 'aria-hidden' => 'true' ) ); ?>
								<?php echo esc_html( $data['job_type'] ); ?>
							</span>
						<?php endif; ?>
					</div>
				</div>

				<?php if ( ! empty( $data['excerpt'] ) ) : ?>
					<div class="sifency-career-item__excerpt sifency-content-style">
						<?php echo wp_kses_post( $data['excerpt'] ); ?>
					</div>
				<?php endif; ?>
			</div>

			<?php if ( ! empty( $settings['read_more_text'] ) ) : ?>
			<div class="sifency-career-item__footer">
				<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?> class="sifency-career-item__button sifency-button-style">
					<span class="btn-text"><?php echo esc_html( $settings['read_more_text'] ); ?></span>
					<?php Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</a>
			</div>
			<?php endif; ?>
		</article>
		<?php
	}

	private function render_custom_content( $settings, Sifency_Addons_Animation_Manager $animation_manager ) {
		if ( empty( $settings['career_list'] ) ) {
			return; }

		foreach ( $settings['career_list'] as $index => $item ) {
			$link_key = 'link_' . $index;
			$this->remove_render_attribute( $link_key );
			if ( ! empty( $item['link']['url'] ) ) {
				$this->add_link_attributes( $link_key, $item['link'] );
			} else {
				$this->add_render_attribute( $link_key, 'href', '#' );
			}

			$image_html = '';
			if ( ! empty( $item['career_image']['id'] ) ) {
				$image_html = Group_Control_Image_Size::get_attachment_image_html( $item, 'thumbnail', 'career_image' );
			}

			$data = array(
				'title'      => $item['title'],
				'location'   => $item['location'],
				'job_type'   => $item['job_type'],
				'salary'     => $item['salary'],
				'excerpt'    => $item['excerpt'],
				'image_html' => $image_html,
			);

			$this->render_single_item( $settings, $data, $link_key, $animation_manager, $index );
		}
	}

	private function render_posts_content( $settings, Sifency_Addons_Animation_Manager $animation_manager ) {
		$query_args              = method_exists( $this, 'get_sifency_addons_query_args' ) ? $this->get_sifency_addons_query_args() : array();
		$query_args['post_type'] = $settings['query_post_type'] ?? 'career';
		$query                   = new \WP_Query( $query_args );

		if ( ! $query->have_posts() ) {
			return; }

		$index = 0;
		while ( $query->have_posts() ) :
			$query->the_post();
			$meta = get_post_meta( get_the_ID(), 'career_single_metabox', true ) ?: array();

			$link_key   = 'post_link_' . $index;
			$apply_link = $meta['career_apply_link'] ?? get_permalink();
			$this->add_render_attribute( $link_key, 'href', $apply_link );

			$excerpt = '';
			if ( 'yes' === $settings['show_excerpt'] && has_excerpt() ) {
				$excerpt = wp_trim_words( get_the_excerpt(), $settings['excerpt_length']['size'] ?? 20 );
			}

			$image_html = '';
			if ( has_post_thumbnail() ) {
				$image_html = get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
			}

			$data = array(
				'title'      => get_the_title(),
				'location'   => sifency_get_field( 'career_location' ),
				'job_type'   => sifency_get_field( 'career_job_type' ),
				'salary'     => sifency_get_field( 'career_salary' ),
				'excerpt'    => $excerpt,
				'image_html' => $image_html,
			);

			$this->render_single_item( $settings, $data, $link_key, $animation_manager, $index );
			++$index;
		endwhile;
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
		wp_reset_postdata();
	}
}
