<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Product_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Woocommerce_Base_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;

class Sifency_Widget_Products_Slider extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Product_Slider_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Woocommerce_Base_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'products-slider-sifency',
			'title'          => 'Sifency Products Slider',
			'icon'           => 'eicon-posts-carousel',
			'category'       => 'sifency-addons-woo-widgets',
			'style_depends'  => array( 'swiper', 'sifency-frontend-style', 'sifency-products-style', 'sifency-icon' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_addons_woocommerce_base_controls();
		$this->add_sifency_product_slider_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		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 );
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$args           = $this->get_sifency_product_slider_query_args( $settings );
		$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 = array( '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 );
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?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         = array( '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( array( 'swiper-slide' ) );
					?>
					<div <?php echo wp_kses_post( $slide_attrs ); ?>>
						<?php wc_get_template_part( 'content', 'product' ); ?>
					</div>
				<?php endwhile; ?>
			</div>
			<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
			<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
		</div>
		<?php
		wp_reset_postdata();

		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 );
		}
	}
}
