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

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\Plugin as ElementorPlugin;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Product_Slider_Controls;

class Sifency_Widget_Products_Slider extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Addons_Slider_Controls;
    use Sifency_Product_Slider_Controls;

    public function get_name() {
        return 'products-slider-sifency';
    }

    public function get_title() {
        return __('Sifency Products Slider', 'sifency-addons');
    }

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

    public function get_categories() {
        return ['sifency-addons-woo-widgets'];
    }
    
    public function get_script_depends() {
        return $this->get_carousel_script_depends();
    }

    public function get_style_depends() {
        return [ 'swiper', 'sifency-swiper-style', 'sifency-frontend-style', 'sifency-products-slider-style', 'sifency-icon' ];
    }

    protected function register_controls() {
        if ( ! class_exists('woocommerce') ) {
            $this->start_controls_section('woo_notice', [ 'label' => 'Notice' ]);
            $this->add_control('woo_notice_control', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . esc_html__('WooCommerce is not installed or active.', 'sifency-addons') . '</div>' ]);
            $this->end_controls_section();
            return;
        }

        $this->add_sifency_product_slider_controls();
        $this->add_sifency_animation_controls();
    }

    protected function render() {
        if ( ! class_exists('woocommerce') ) {
            if ( ElementorPlugin::$instance->editor->is_edit_mode() ) { echo '<div class="sifency-placeholder">' . esc_html__('WooCommerce is not active.', 'sifency-addons') . '</div>'; }
            return;
        }
        
        $settings = $this->get_settings_for_display();
        
        if ( 'yes' !== ($settings['show_sale_flash'] ?? 'yes') ) {
            remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
        }
        if ( 'yes' !== ($settings['show_rating'] ?? 'yes') ) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
        }
        if ( 'yes' !== ($settings['show_price'] ?? 'yes') ) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
        if ( 'yes' !== ($settings['show_add_to_cart'] ?? 'yes') ) {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        }

        // Manager is instantiated correctly
        $animation_manager = new Sifency_Animation_Manager($settings);
        
        $args = [ 'post_type' => 'product', 'posts_per_page' => $settings['posts_per_page'], 'orderby' => $settings['orderby'], 'order' => $settings['order'] ];
        if ('sale' === $settings['source']) {
            $args['post__in'] = array_merge( [0], wc_get_product_ids_on_sale() );
        } elseif ('featured' === $settings['source']) {
                $args['tax_query'][] = [ 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN' ];
        }
        $products_query = new \WP_Query($args);

        if ( ! $products_query->have_posts() ) { 
            if ( ElementorPlugin::$instance->editor->is_edit_mode() ) { echo '<div class="sifency-placeholder">' . esc_html__('No products found.', 'sifency-addons') . '</div>'; }
            if ( 'yes' !== ($settings['show_add_to_cart'] ?? 'yes') ) { add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); }
            return;
        }

        $wrapper_classes = ['sifency-products-slider-wrapper', 'sifency-carousel-wrapper', 'swiper-container'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        
        $slider_options = $this->get_sifency_swiper_options($settings, $products_query->post_count);
        ?>
        <div <?php 
            
            echo wp_kses_post( $wrapper_attrs ); ?> data-swiper-options="<?php echo esc_attr(wp_json_encode($slider_options)); ?>">
            <div class="swiper-wrapper">
                
                <?php 
                $post_index = 0;
                $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);
                while ( $products_query->have_posts() ) : $products_query->the_post(); ?>
                    <?php
                    $slide_attrs = $animation_manager->get_item_attributes(['swiper-slide']);
                    ?>
                    <div <?php echo wp_kses_post($slide_attrs); ?>>
                        <?php wc_get_template_part( 'content', 'product' ); ?>
                    </div>
                <?php endwhile; ?>
            </div>

            <?php if ( 'yes' === ($settings['show_arrows'] ?? 'yes') ) : ?>
                 <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
        wp_reset_postdata();

        // The logic to add actions back is correct.
        if ( 'yes' !== ($settings['show_sale_flash'] ?? 'yes') ) {
            add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
        }
        if ( 'yes' !== ($settings['show_rating'] ?? 'yes') ) {
            add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
        }
        if ( 'yes' !== ($settings['show_price'] ?? 'yes') ) {
            add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
        if ( 'yes' !== ($settings['show_add_to_cart'] ?? 'yes') ) {
            add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        }
    }
}