<?php
// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Sifency\Traits\Sifency_Arg_Controls;
use \Sifency\Traits\Sifency_Animation_Controls;
use \Sifency\Traits\Sifency_Elementor_Parity_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;
use Sifency\Traits\Sifency_Button_Style_Controls;
use Sifency\Traits\Sifency_Item_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Career_Controls;

class Sifency_Addons_Widget_Career extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Arg_Controls;
    use Sifency_Core_Text_Style;
    use Sifency_Button_Style_Controls;
    use Sifency_Item_Style_Controls;
    use Sifency_Career_Controls;

    public function get_name() {
        return 'sifency-addons-career';
    }
    
    public function get_title() {
        return esc_html__('Sifency Career', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-posts-ticker';
    }
    
    public function get_categories() {
        return ['sifency-advance-widgets'];
    }

    public function get_style_depends() {
        return [ 'sifency-typo-style' , 'sifency-frontend-style', 'sifency-career-style', 'sifency-icon' ];
    }
    
    protected function register_controls() {
        $this->add_sifency_career_controls();
        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Career Heading',
            'selector' => '.sifency-heading-style',
            'target_children' => true,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Career Content',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Career Meta',
            'selector' => '.sifency-meta-style',
            'target_children' => false,
        ]);
        $this->add_sifency_button_style_controls();
        Sifency_Elementor_Parity_Controls::register( $this);
        $this->add_sifency_animation_controls();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-career-wrapper'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        $this->add_render_attribute('grid', 'class', 'sifency-career-grid');
        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <div <?php echo wp_kses_post ($this->get_render_attribute_string('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_custom_content($settings, Sifency_Animation_Manager $animation_manager) {
        if ( empty($settings['career_list']) ) { return; }
        
        foreach ($settings['career_list'] as $index => $item) {
            $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 = ['sifency-item-style', 'sifency-career-item', $parity_class];
            $item_attrs = $animation_manager->get_item_attributes($item_classes);

            $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', '#');
            }
            ?>
            <article <?php echo wp_kses_post ($item_attrs); ?>>
                <div class="sifency-career-item__header">
                    <h3 class="sifency-career-item__title sifency-heading-style"><a <?php echo wp_kses ( $this->get_render_attribute_string($link_key), [] ); ?>><?php echo esc_html($item['title']); ?></a></h3>
                    <div class="sifency-career-item__meta">
                        <?php if ($item['location']) : ?>
                            <span class="sifency-career-item__location sifency-meta-style"><?php echo esc_html($item['location']); ?></span>
                        <?php endif; ?>
                        <?php if ($item['job_type']) : ?>
                            <span class="sifency-career-item__type sifency-meta-style"><?php echo esc_html($item['job_type']); ?></span>
                        <?php endif; ?>
                    </div>
                </div>
                <?php if ( ! empty($item['excerpt']) ) : ?>
                    <div class="sifency-career-item__excerpt sifency-content-style">
                        <?php echo wp_kses_post($item['excerpt']); ?>
                    </div>
                <?php endif; ?>
                <?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">
                        <?php echo esc_html($settings['read_more_text']); ?>
                        <i class="icon-right"></i>
                    </a>
                </div>
                <?php endif; ?>
            </article>
            <?php
        }
    }

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

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

        while( $query->have_posts() ) : $query->the_post();
            $oi = $query->current_post + 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 = ['sifency-item-style', 'sifency-career-item', $parity_class];
            $item_attrs = $animation_manager->get_item_attributes($item_classes);

            $meta = get_post_meta(get_the_ID(), 'career_single_metabox', true) ?: [];
            $location = $meta['career_location'] ?? '';
            $job_type = $meta['career_job_type'] ?? '';
            $apply_link = $meta['career_apply_link'] ?? get_permalink();
            ?>
            <article <?php echo wp_kses_post ($item_attrs); ?>>
                <div class="sifency-career-item__header">
                    <h3 class="sifency-career-item__title sifency-heading-style"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="sifency-career-item__meta">
                        <?php if ($location) : ?><span class="sifency-career-item__location sifency-meta-style"><?php echo esc_html($location); ?></span><?php endif; ?>
                        <?php if ($job_type) : ?><span class="sifency-career-item__type sifency-meta-style"><?php echo esc_html($job_type); ?></span><?php endif; ?>
                    </div>
                </div>
                <?php if ( 'yes' === $settings['show_excerpt'] && has_excerpt() ) : ?>
                    <div class="sifency-career-item__excerpt sifency-content-style">
                        <?php echo wp_kses_post( wp_trim_words( get_the_excerpt(), $settings['excerpt_length']['size'] ?? 20 ) ); ?>
                    </div>
                <?php endif; ?>
                <div class="sifency-career-item__footer">
                    <a 
                        <?php if ( ! empty( $apply_link ) ) : ?>
                            href="<?php echo esc_url( $apply_link ); ?>"
                        <?php endif; ?>
                        class="sifency-career-item__button sifency-button-style"
                    >
                        <?php echo esc_html( $settings['read_more_text'] ); ?>
                    </a>
                </div>
            </article>
        <?php endwhile;
        wp_reset_postdata();
    }
}