<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Parity_Style_Controls;
use Sifency\Traits\Sifency_Products_Controls;
use Sifency\Traits\Sifency_Addons_Numeric_Pagination;
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 extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Products_Controls;
	use Sifency_Addons_Numeric_Pagination;
	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'           => 'sifency-products',
			'title'          => 'Sifency Products Grid',
			'icon'           => 'eicon-products',
			'category'       => 'sifency-addons-woo-widgets',
			'style_depends'  => array( 'sifency-frontend-style', 'sifency-products-style', 'sifency-button-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
			'keywords'       => array( 'products', 'woocommerce', 'shop', 'grid', 'ecommerce', 'sifency' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_products_controls();
		$this->add_sifency_addons_woocommerce_base_controls();
		$this->add_sifency_addons_numeric_pagination_controls();
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_sifency_addons_help_controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! class_exists( 'woocommerce' ) ) {
			return;
		}

		$settings = $this->get_settings_for_display();

		$query_args     = $this->get_sifency_product_query_args( $settings );
		$products_query = new \WP_Query( $query_args );

		if ( ! $products_query->have_posts() ) {
			echo '<div class="elementor-alert elementor-alert-info">' . esc_html__( 'No products found.', 'sifency-addons' ) . '</div>';
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$parity_pattern    = $settings['parity_pattern'] ?? 'standard';
		$custom_positions  = array();
		if ( 'custom' === $parity_pattern && ! empty( $settings['custom_even_positions'] ) ) {
			$custom_positions = array_map( 'intval', explode( ',', $settings['custom_even_positions'] ) );
		}

		$wrapper_classes = array( 'sifency-products-grid-wrapper' );
		$wrapper_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		if ( 'yes' !== ( $settings['show_rating'] ?? 'yes' ) ) {
			remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
		}
		if ( 'yes' !== ( $settings['show_badge'] ?? 'yes' ) ) {
			remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
		}

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<div class="woocommerce sifency-products-grid">
				<?php $this->render_sifency_section_header_elements( $settings ); ?>
				<?php woocommerce_product_loop_start(); ?>
				
				<?php
				while ( $products_query->have_posts() ) :
					$products_query->the_post();
					?>
					<?php
					global $product;

					$current_position = $products_query->current_post + 1;
					$parity_class     = 'odd-item';

					if ( 'every_third' === $parity_pattern && $current_position % 3 === 0 ) {
						$parity_class = 'even-item';
					} elseif ( 'custom' === $parity_pattern && in_array( $current_position, $custom_positions ) ) {
						$parity_class = 'even-item';
					} elseif ( 'standard' === $parity_pattern && $current_position % 2 === 0 ) {
						$parity_class = 'even-item';
					}

					$product_classes   = wc_get_product_class( '', $product );
					$product_classes[] = $parity_class;
					$product_classes[] = 'sifency-item-style';

					$item_attrs = $animation_manager->get_item_attributes( $product_classes );
					echo '<li ' . wp_kses_post( $item_attrs ) . '>';

					do_action( 'woocommerce_before_shop_loop_item' );
					do_action( 'woocommerce_before_shop_loop_item_title' );
					do_action( 'woocommerce_shop_loop_item_title' );
					do_action( 'woocommerce_after_shop_loop_item_title' );
					do_action( 'woocommerce_after_shop_loop_item' );

					echo '</li>';
					?>
				<?php endwhile; ?>
				
				<?php woocommerce_product_loop_end(); ?>
			</div>

			<?php
				$this->render_sifency_addons_numeric_pagination( $products_query, $settings );
			?>
		</div>
		<?php

		if ( 'yes' !== ( $settings['show_rating'] ?? 'yes' ) ) {
			add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
		}
		if ( 'yes' !== ( $settings['show_badge'] ?? 'yes' ) ) {
			add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
		}

		wp_reset_postdata();
	}
}
