<?php
if (!defined('ABSPATH')) {
    exit;
}

use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Repeater;
use \Sifency\Traits\Sifency_Arg_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_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Button_Icon_Style_Controls;
use Sifency\Traits\Sifency_Award_Controls;

class sifency_addons_Awards_Widget 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_Button_Icon_Style_Controls;
    use Sifency_Award_Controls;
    
    public function get_name() {
        return 'sifency-addons-awards';
    }

    public function get_title() {
        return esc_html__('Sifency Awards', 'sifency-addons');
    }

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

    public function get_categories() {
        return ['sifency-advance-widgets'];
    }

    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-frontend-style' , 'sifency-icon', 'sifency-awards-style' ];
    }

    protected function register_controls() {
        $this->add_sifency_award_controls();
        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Award Title',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Award Date (Meta)',
            'selector' => '.sifency-meta-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Award Content',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_button_style_controls();
        $this->add_sifency_button_icon_style_controls();
        Sifency_Elementor_Parity_Controls::register( $this );
        $this->add_sifency_animation_controls();
    }
    
    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = [
            'sifency-awards-wrapper',
            'sifency-awards--' . $settings['layout_style']
        ];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php
            if ( 'posts' === $settings['source'] ) {
                $this->render_posts_content($settings, $animation_manager);
            } else {
                $this->render_custom_content($settings, $animation_manager);
            }
            ?>
        </div>
        <?php
    }

    private function render_custom_content($settings, Sifency_Animation_Manager $animation_manager) {
        if ( empty($settings['awards_list']) ) { return; }
        
        foreach ($settings['awards_list'] as $index => $item) {
            $oi = $index + 1;

            $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', '#');
            }

            $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) ? 'even-item' : 'odd-item'; }
            $item_classes = ['sifency-item-style', 'sifency-award-item', $parity_class];
            $item_attrs = $animation_manager->get_item_attributes($item_classes);
            ?>
            <div <?php echo wp_kses_post ($item_attrs); ?>>
                <?php if ( ! empty($item['award_year']) ) : ?> <span class="sifency-award-year sifency-meta-style"><?php echo esc_html($item['award_year']); ?></span> <?php endif; ?>
                <?php if ( ! empty($item['award_title']) ) : ?> <h3 class="sifency-award-title sifency-heading-style"><?php echo esc_html($item['award_title']); ?></h3> <?php endif; ?>
                <?php if ( ! empty($item['awarding_body']) ) : ?> <div class="sifency-award-body sifency-content-style"><?php echo esc_html($item['awarding_body']); ?></div>
                <?php endif; ?>

                <?php if ( ! empty( $settings['read_more_text'] ) ) : ?> 
                    <div class="sifency-award-item-link">
                        <a <?php echo wp_kses_post ($this->get_render_attribute_string( $link_key )); ?> class="sa-award-btn sifency-button-style">
                            <?php echo esc_html( $settings['read_more_text'] ); ?>
                            <div class="sifency-button-icon-style"><i class="icon-right"></i></div>
                        </a>
                    </div>
                <?php endif; ?>

            </div>
            <?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'] ?? 'award';
        $query = new \WP_Query( $query_args );

        if ( ! $query->have_posts() ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo wp_kses_post('<div class="sifency-placeholder">' . esc_html__('No posts found for your query.', 'sifency-addons') . '</div>');
            }
            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) ? 'even-item' : 'odd-item'; }
            $item_classes = ['sifency-item-style', 'sifency-award-item', $parity_class];
            $item_attrs = $animation_manager->get_item_attributes($item_classes);
            ?>
            <div <?php echo wp_kses_post ($item_attrs); ?>>
                <span class="sifency-award-year sifency-meta-style"><?php echo get_the_date('Y'); ?></span>
                <h3 class="sifency-award-title">
                    <a href="<?php the_permalink(); ?>" class="sifency-heading-style"><?php the_title(); ?></a>
                </h3>
                <?php if ( has_excerpt() ) : ?>
                    <div class="sifency-award-body sifency-content-style">
                        <?php the_excerpt(); ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php endwhile;
        wp_reset_postdata();
    }
}