<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Related_Product_Controls;
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_Related_Products extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Related_Product_Controls;
	use Sifency_Addons_Woocommerce_Base_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	protected function get_allowed_template_types() {
		return 'product';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-related-products',
			'title'          => 'Sifency Related Products',
			'icon'           => 'eicon-products',
			'category'       => 'sifency-addons-woo-widgets',
			'style_depends'  => array( 'swiper', 'sifency-frontend-style', 'sifency-icon', 'sifency-tb-woo-style' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_related_product_controls();
		$this->add_sifency_addons_help_controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! $this->is_woocommerce_active() ) {
			$this->render_woocommerce_placeholder( 'WooCommerce not active' );
			return;
		}
		global $product;
		$settings = $this->get_settings_for_display();

		if ( ! $product instanceof WC_Product ) {
			$product = $this->get_preview_product();
		}

		if ( ! $product ) {
			echo '<div class="sifency-placeholder">No product found</div>';
			return;
		}

		$related_ids = wc_get_related_products(
			$product->get_id(),
			$settings['posts_per_page']
		);

		if ( empty( $related_ids ) ) {
			echo '<div class="sifency-placeholder">No related products</div>';
			return;
		}

		$query = new WP_Query(
			array(
				'post_type'      => 'product',
				'post__in'       => $related_ids,
				'posts_per_page' => $settings['posts_per_page'],
				'orderby'        => $settings['orderby'],
				'order'          => $settings['order'],
			)
		);
		?>

		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-related-products-wrapper layout-<?php echo esc_attr( $settings['layout_style'] ); ?> skin-<?php echo esc_attr( $settings['skin_style'] ); ?>">
			<div class="sifency-products">
				<?php
				while ( $query->have_posts() ) :
					$query->the_post();
					$item = wc_get_product( get_the_ID() );
					$this->render_product_skin( $item, $settings );
				endwhile;
				?>
			</div>
		</div>

		<?php
		wp_reset_postdata();
	}

	private function render_product_skin( $product, $settings ) {
		$skin = sanitize_key( $settings['skin_style'] );
		$file = __DIR__ . '/skins/skin-' . $skin . '.php';

		if ( ! file_exists( $file ) ) {
			$file = __DIR__ . '/skins/skin-card.php';
		}

		$data = $this->get_product_data( $product );
		include $file;
	}

	private function get_product_data( $product ) {
		return array(
			'title'  => $product->get_name(),
			'url'    => $product->get_permalink(),
			'image'  => wp_get_attachment_image_url( $product->get_image_id(), 'woocommerce_thumbnail' ),
			'price'  => $product->get_price_html(),
			'rating' => wc_get_rating_html( $product->get_average_rating() ),
		);
	}

	private function get_preview_product() {
		$q = new WP_Query(
			array(
				'post_type'      => 'product',
				'posts_per_page' => 1,
			)
		);
		if ( $q->have_posts() ) {
			$q->the_post();
			return wc_get_product( get_the_ID() );
		}
		return false;
	}
}
