<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
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_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Blog_Controls;
use Sifency\Traits\Sifency_Image_Style_Controls;

class Sifency_Addons_Widget_Blog extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Addons_Slider_Controls;
    use Sifency_Arg_Controls;
    use Sifency_Core_Text_Style;
    use Sifency_Button_Style_Controls;
    use Sifency_Item_Style_Controls;
    use Sifency_Blog_Controls;
    use Sifency_Image_Style_Controls;

    public function get_name() {
        return 'sifency-addons-blog-post';
    }
    
    public function get_title() {
        return esc_html__('Sifency Blog', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-posts-grid';
    }
    
    public function get_categories() {
        return ['sifency-advance-widgets'];
    }

    public function get_script_depends() {
        return $this->get_carousel_script_depends();
    }

    public function get_style_depends() {
        $dependencies = $this->get_carousel_style_depends();
        $dependencies[] = 'sifency-blog-style';
        return $dependencies;
    }
    protected function register_controls() {
        $carousel_condition = [
            'condition' => [
                'layout_style' => 'carousel'
            ]
        ];
        $this->add_sifency_blog_controls();
        $this->add_sifency_arg_controls();
        $this->add_sifency_carousel_controls( $carousel_condition );
        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Blog Title',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Blog Date (Meta)',
            'selector' => '.sifency-meta-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Blog Excerpt',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_button_style_controls();
        $this->add_sifency_image_style_controls();
        Sifency_Elementor_Parity_Controls::register( $this );
        $this->add_sifency_animation_controls();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $query_args = $this->get_sifency_query_args();
        $query_args['post_type'] = $settings['query_post_type'] ?? 'post';
        
        $query = new \WP_Query( $query_args );
        if ( ! $query->have_posts() ) {
            return;
        }
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = [ 'sifency-addons-blog-wrapper' ];
        if ( 'carousel' === $settings['layout_style'] ) {
            $wrapper_classes[] = 'sifency-addons-blog--carousel';
            $post_count = $query->post_count;
            $slider_options = $this->get_sifency_swiper_options( $settings, $post_count );
            
            $this->add_render_attribute('wrapper', 'data-swiper-options', wp_json_encode( $slider_options ) );
        } else {
            $wrapper_classes[] = 'sifency-addons-blog-grid';
        }
        $animation_wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs); ?>>
            <?php if ( 'carousel' === $settings['layout_style'] ) : ?>
                <div class="swiper-container">
                    <div class="swiper-wrapper">
            <?php endif; ?>

            <?php 
            $post_index = 0;
            while( $query->have_posts() ) : $query->the_post(); 
                $post_index++;
                $parity_class = ( $post_index % 2 !== 0 ) ? 'odd-item' : 'even-item';
                $item_classes = ['sifency-item-style', 'sifency-addons-blog-item', $parity_class];
                $animation_item_attrs = $animation_manager->get_item_attributes($item_classes);
            ?>
                <?php if ( 'carousel' === $settings['layout_style'] ) : ?>
                    <div class="swiper-slide">
                <?php endif; ?>
                
                    <article <?php echo wp_kses_post ($animation_item_attrs); ?>>
                        <?php if ( has_post_thumbnail() ) : ?>
                            <?php if ( 'yes' === $settings['show_thumbnail'] )  : ?>
                            <div class="sifency-addons-blog-image sifency-image-container">
                                <a href="<?php the_permalink(); ?>" class="sifency-image img"><?php the_post_thumbnail('medium_large'); ?></a>
                                <div class="sifency-image-overlay"></div>
                            </div>
                            <?php endif; ?>
                        <?php endif; ?>
                        <div class="sifency-addons-blog-content">

                            <?php if ( 'yes' === $settings['show_date'] )  : ?>
                            <div class="sifency-addons-blog-meta sifency-meta-style"><?php echo get_the_date(); ?></div>
                            <?php endif; ?>

                            <h3 class="sifency-addons-blog-title sifency-heading-style">
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>

                             <?php if ( 'yes' === $settings['show_excerpt'] )  : ?>
                            <div class="sifency-addons-blog-excerpt sifency-content-style"><?php the_excerpt(); ?></div>
                            <?php endif; ?>

                            <?php if ( 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_text'] ) ) : ?>
                                <a href="<?php the_permalink(); ?>" class="sifency-button-style">
                                    <?php echo esc_html( $settings['read_more_text'] ); ?> <i class="icon-right"></i>
                                </a>
                            <?php endif; ?>
                        </div>
                    </article>

                <?php if ( 'carousel' === $settings['layout_style'] ) : ?>
                    </div>
                <?php endif; ?>
            <?php endwhile; ?>

            <?php if ( 'carousel' === $settings['layout_style'] ) : ?>
                    </div>

                    <?php if ( ! empty($settings['show_arrows']) && 'yes' === $settings['show_arrows'] ) : ?>
                        <div class="sifency-slider-nav-container">
                        <div class="swiper-button-prev sifency-slider-nav-arrow sifency-slider-nav-prev"><i class="icon-left"></i></div>
                        <div class="swiper-button-next sifency-slider-nav-arrow sifency-slider-nav-next"><i class="icon-right"></i></div>
                        </div>
                    <?php endif; ?>

                    <?php if ( ! empty( $settings['show_pagination'] ) ) : ?>
                        <div class="swiper-pagination"></div>
                    <?php endif; ?>

                </div>
            <?php endif; ?>
        </div>
        <?php
        wp_reset_postdata();
    }
}